Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2008
    Posts
    6
    Rep Power
    0

    Default Can't connect using jdbc!!!

    Hi

    I'm trying to connect to mySQL via jdbc and I end up with a bunch of exceptions :confused:

    My code is separated in a "Connector" class
    which is imported to my app.

    The source goes like this:


    Connector.java
    Java Code:
    package jdbcConnect;
    
    import java.sql.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    public class Connector {
    
        public String host = null;
        public String port = null;
        public String database = null;
        public String username = null;
        public String password = null;
    
        Connection conn;
    
        public Connector(){
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Connector.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    
        public void mySQL_open(){
            try {
                conn = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?user=" + username + "&password=" + password);
            } catch (SQLException ex) {
                Logger.getLogger(Connector.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    
        public void excecute_SQL(String SQL_Query){
            try {
                Statement st = conn.createStatement();
                ResultSet rs = st.executeQuery(SQL_Query);
                System.out.println(rs.getString("name"));
            } catch (SQLException ex) {
                Logger.getLogger(Connector.class.getName()).log(Level.SEVERE, null, ex);
            }
    
        }
    
    }
    Main.java
    Java Code:
    ....
    
    Connector conn = new Connector();
    
    conn.database = "test";
    conn.host = "localhost";
    conn.port = "3306";
    conn.username = "root";
    conn.password = "rootpswd";
    
    conn.mySQL_open();
    conn.excecute_SQL("select * from users");
    
    ....
    and what I get is this: :(

    Java Code:
    init:
    deps-jar:
    Compiling 1 source file to /home/user/Documents/Netbeans_Projects/jdbcApp/build/classes
    compile:
    run:
    Nov 26, 2008 10:45:31 PM jdbcConnect.Connector excecute_SQL
    SEVERE: null
    java.sql.SQLException
            at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
            at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
            at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
            at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:815)
            at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5528)
            at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5448)
            at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5488)
            at jdbcConnect.Connector.excecute_SQL(Connector.java:48)
            at jdbcapp.JdbcView.<init>(JdbcView.java:96)
            at jdbcapp.JdbcApp.startup(JdbcApp.java:19)
    
    ...
    I just can't figure out what's wrong :confused:

    I really need some help on this.

    any working code snippet or advice ... anything

  2. #2
    Join Date
    Nov 2008
    Posts
    6
    Rep Power
    0

    Smile Silly MEEE!!!

    Well.... I forgot to put a "result.next()" right after the "ResultSet rs = st.executeQuery(SQL_Query);" and so Java was trying to read the row-before-the-first.... oupsy!! :o

    It's amazing when forgetting something so simple can cause you such big trouble.

    The positive thing that came out of this though is that I finally downloaded the new mysql-connector... :D
    Last edited by mikep_bluefish; 11-26-2008 at 10:53 PM.

Similar Threads

  1. how connect
    By herfnai in forum JDBC
    Replies: 1
    Last Post: 08-18-2008, 06:34 AM
  2. connect JDBC to offline database
    By nancyhung in forum JDBC
    Replies: 1
    Last Post: 04-11-2008, 11:04 PM
  3. Replies: 0
    Last Post: 04-01-2008, 10:17 AM
  4. Using JDBC to connect to ORACLE database
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-10-2008, 11:27 AM
  5. Replies: 0
    Last Post: 09-28-2007, 12:56 PM

Posting Permissions

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