Results 1 to 6 of 6
  1. #1
    sysout is offline Member
    Join Date
    Aug 2009
    Posts
    66
    Rep Power
    0

    Default java mysql connection

    Connect.java
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package database;
    import java.sql.*;
    
    /**
     *
     * @author admin
     */
    public class Connect {
        private String driverName = "com.mysql.jdbc.Driver";
        private String jdbcType = "jdbc:mysql";
        private String jdbcUrl = "localhost:3306/database";
        private String username = "root";
        private String password = "";
        private Statement stmt;
        private ResultSet rs;
    
        private Connection conn;
        
        public Connect(){
    
        }
    
        public void init(){
            String currUrl = jdbcType+"://"+jdbcUrl+"?user="+username+"&password="+password;
            try{
                Class.forName(driverName);
                conn = DriverManager.getConnection(currUrl, username, password);
            }
            catch(ClassNotFoundException cnfe){
                System.err.print("Error : "+cnfe);
            }
            catch(SQLException sqle){
                conn = null;
                System.err.print("Error : "+sqle);
            }
        }
        public Connection getConnection(){
            if(conn != null){
                return conn;
            }
            else{
                init();
                return conn;
            }
        }
    }
    Execution.java
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package database;
    
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    /**
     *
     * @author admin
     */
    public class Execution{
        Connection conn;
        Statement stmt;
        ResultSet rs;
    
        public ResultSet getResults(String sql) throws SQLException {
            Connect A = new Connect();
            conn = (Connection) A.getConnection();
            stmt = (Statement) conn.createStatement();
            rs = stmt.executeQuery(sql);
            stmt.close();
            return rs;
        }
        
        public void doExecute(String sql) throws SQLException{
            Connect A = new Connect();
            conn = (Connection) A.getConnection();
            stmt = (Statement) conn.createStatement();
            stmt.executeUpdate(sql);
            stmt.close();
        }
    }
    I use this code to fetch result from database (mysql) :
    Java Code:
    Execution exec = new Execution();
    String sql = "";
    ResultSet rs;
    
    sql = "SELECT * FROM TABLE WHERE colum3 = 'yes'";
    rs = exec.getResults(sql);
    
    if(rs.next()){
            //get results here
    }
    The Error was :
    Error : java.lang.ClassNotFoundException: com.mysql.jdbc.Driverjava.lang.NullPointerExceptio n
    Last edited by sysout; 10-31-2009 at 07:17 AM.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    Add the MySQL driver to program's classpath.

  3. #3
    sysout is offline Member
    Join Date
    Aug 2009
    Posts
    66
    Rep Power
    0

    Default

    Another Error :
    java.lang.ArrayIndexOutOfBoundsException: 1
    at read.ProcessDatabase.doIt(ProcessDatabase.java:53)
    at read.ProcessDatabase.main(ProcessDatabase.java:137 )
    I use Array for rs.next :
    config[0] = rs.getString("col1");
    config[1] = rs.getString("col2");
    config[2] = rs.getString("col3");
    config[3] = rs.getString("col4");
    config[4] = rs.getString("col5");
    Last edited by sysout; 10-31-2009 at 07:59 AM.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    Line 53 of your ProcessDatabase class is the problem. Look at your read method and where you are accesing a value from an array. Basically you are accessing an index which is not part of your array.

  5. #5
    sysout is offline Member
    Join Date
    Aug 2009
    Posts
    66
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    Line 53 of your ProcessDatabase class is the problem. Look at your read method and where you are accesing a value from an array. Basically you are accessing an index which is not part of your array.
    thanks palz, it absolutely works for me (why can't I add REP to you again?)

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    I don't know why you can't add REP. Don't worry about it though. It's not important to me.

Similar Threads

  1. Java and MySQL
    By Abder-Rahman in forum JDBC
    Replies: 6
    Last Post: 04-21-2009, 09:55 AM
  2. MySQL/JDBC Mysql query output
    By thelinuxguy in forum Advanced Java
    Replies: 4
    Last Post: 02-13-2009, 02:57 AM
  3. Replies: 1
    Last Post: 10-29-2008, 06:38 PM
  4. no mysql connection
    By scchia in forum New To Java
    Replies: 12
    Last Post: 07-19-2008, 07:57 PM
  5. java to mysql
    By thamizhisai in forum New To Java
    Replies: 12
    Last Post: 04-28-2008, 07:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •