13 Replies Latest reply: Mar 6, 2007 5:09 AM by 807599 RSS

    mysql connector/j

    807599
      package mysqlTest;
      
      import java.sql.*;
      
      public class NewClass{
        public static void main(String[] args){
          try{
              
            Class.forName("com.mysql.jdbc.Driver").newInstance(  );
            Connection conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/test",
                    "root","****");
            ResultSet cursor = conn.createStatement(  ).executeQuery(
               "INSERT into javatest VALUES('Steve', 'Granet')");
            conn.close(  );
          }
          catch (Exception e){
            System.err.println(e);
          }
        }
      }
      okay im using netbeans, the newest versions of mysql, connector/j, jdk, everything right. and this file compiles fine, but when i run it i get an error...

      init:
      deps-module-jar:
      deps-ear-jar:
      deps-jar:
      compile-single:
      run-main:
      java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
      BUILD SUCCESSFUL (total time: 1 second)


      now i added the connection to netbeans, all worked fine right, i can connect to my database, but when i do it through my own code it doesnt work.
        • 1. Re: mysql connector/j
          807599
          Connection conn = DriverManager.getConnection(
          "jdbc:mysql://localhost:3306/test",
          add '?' at the end of url string, so your url become like this
          "jdbc:mysql://localhost:3306/test?"
          Message was edited by:
          p_epi
          • 2. Re: mysql connector/j
            masijade
            Classpath, classpath, classpath.

            Since you are using netbeans, ensure that the driver jarfile is included as one of the libraries for the project.

            Edit: or if through your own code means from the command line, then ensure that the jarfile is listed under the -cp option.
            • 3. Re: mysql connector/j
              807599
              no that didnt fix it, where am i supposed to put the connector/j driver at? what specific folder for windows? i mean i just have it on my desktop, but i thought netbeans setup the classpath to read it... but maybe it didnt, and i dont want a classpath going to a random folder on my desktop.

              but then why could i access the db through the 'runetime' tab in netbeans?
              • 4. Re: mysql connector/j
                masijade
                You need to add it to the project as a library. It doesn't matter where you have it, as long as it is configured in the project.
                • 5. Re: mysql connector/j
                  807599
                  well its nice that ya'll are helping me out, but how do i add it as a library?
                  • 6. Re: mysql connector/j
                    807599
                    okay im an idiot, i figured out how to add the libary, and set the classpath for the library, but it still didnt fix the error, i recompiled and ran again, and i still get the same error.
                    • 7. Re: mysql connector/j
                      807599
                      okay, after more tinkering i got it right, but now i get this huge ass long error...
                      init:
                      deps-module-jar:
                      deps-ear-jar:
                      deps-jar:
                      compile-single:
                      run-main:
                      com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 
                      
                      ** BEGIN NESTED EXCEPTION ** 
                      
                      java.net.ConnectException
                      MESSAGE: Connection refused: connect
                      
                      STACKTRACE:
                      
                      java.net.ConnectException: Connection refused: connect
                              at java.net.PlainSocketImpl.socketConnect(Native Method)
                              at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
                              at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
                              at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
                              at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
                              at java.net.Socket.connect(Socket.java:519)
                              at java.net.Socket.connect(Socket.java:469)
                              at java.net.Socket.<init>(Socket.java:366)
                              at java.net.Socket.<init>(Socket.java:208)
                              at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:173)
                              at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:267)
                              at com.mysql.jdbc.Connection.createNewIO(Connection.java:2739)
                              at com.mysql.jdbc.Connection.<init>(Connection.java:1553)
                              at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
                              at java.sql.DriverManager.getConnection(DriverManager.java:582)
                              at java.sql.DriverManager.getConnection(DriverManager.java:185)
                              at mysqlTest.NewClass.main(NewClass.java:10)
                      
                      
                      ** END NESTED EXCEPTION **
                      
                      
                      
                      Last packet sent to the server was 16 ms ago.
                      BUILD SUCCESSFUL (total time: 3 seconds)
                      • 8. Re: mysql connector/j
                        masijade
                        Stupid question, but did you remove the "?" from the URL that p_epi suggested you add. If you did not remove it, then remove it now. That is only needed if you are passing the user and password as a part of the URL, which you are not.
                        • 9. Re: mysql connector/j
                          807599
                          yeah i just removed it and tried again, same error
                          • 10. Re: mysql connector/j
                            masijade
                            A few more stupid questions, but they need to be asked.

                            1. Is the DB currently running?
                            2. Is it using port 3306 (I assume it is since that is the default)?
                            3. In the configuration (the mysql.users table) is there an entry "root@localhost" or "root@%" or "root@<localip/hostname>"?
                            If the entry is "root@<localip/hostname>" exists, then change the connection url from "localhost" to the actual ip.
                            If none of the above is present, then add the "root@localhost".
                            If the above suggestion does not work, and the "root@<localip/hostname>" entry does not exist, add it and follow the first suggesstion.
                            The last suggesstion above is because I am not sure if the "root@localhost" option only allows access through the "sock" file, or if it also allows TCP access.
                            • 11. Re: mysql connector/j
                              800308
                              but when i do it through my own code
                              ... ummm... could you post your code?

                              keith.
                              • 12. Re: mysql connector/j
                                masijade
                                He says in his follow up thread

                                http://forum.java.sun.com/thread.jspa?threadID=5144497&tstart=0

                                that he has it working, but does not say what change he had to make.

                                It all had to do with classpath and then (probably) connection parameters (username, password, mysql config).
                                • 13. Re: mysql connector/j
                                  807599
                                  you need to have the jar file in your classpath which has com.mysql.jdbc package and a Driver class within tht package