1 2 Previous Next 19 Replies Latest reply on Feb 24, 2008 7:33 PM by 796254

    SQL Exception cannot find suitable driver

    843859

      hello

      I recently downloaded the mysql connecter, but I got a problem when running the application

      SQLException: No suitable driver found for jdbc:mysql://localhost:3306/java
      SQLState: 08001
      VendorError: 0

      import java.io.*; import java.net.*; import java.sql.*; public class dbcmd { public static void main(String[] args) { try { // The newInstance() call is a work around for some // broken Java implementations Class.forName("com.mysql.jdbc.Driver").newInstance(); } catch (Exception ex) { // handle the error } try { System.out.println("write the user used to access the mysql database with Devil Chat"); String user = Keyboard.readString(); //read mysql username from keyboard System.out.println("write the password used to access the mysql database with Devil Chat"); String pass = Keyboard.readString(); //read password for user from keyboard Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/java", user, pass); } catch (SQLException ex) { // handle any errors System.out.println("SQLException: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("VendorError: " + ex.getErrorCode()); } } }

      I've tried to search around for solutions but haven't been able to find any :/ hope to get a bit of help here

        • 1. Re: SQL Exception cannot find suitable driver
          843859
          Angelwinged_Devil wrote:
          SQLException: No suitable driver found for jdbc:mysql://localhost:3306/java
          Either the URL is wrong, or the desired driver is not loaded.

          The URL looks fine, so the desired driver might not be loaded. In your code you're swallowing any exceptions of Class#forName() instead of handling it. At least add e.printStackTrace() and/or throw it.
          • 2. Re: SQL Exception cannot find suitable driver
            843859
            java.sql.SQLException: No suitable driver found for jdbc:mysql://127.0.0.1:3307/
            java
            at java.sql.DriverManager.getConnection(Unknown Source)
            at java.sql.DriverManager.getConnection(Unknown Source)
            at dbcmd.main(dbcmd.java:23)

            is what I get
            • 3. Re: SQL Exception cannot find suitable driver
              843859
              The above didn't come from
              } catch (Exception ex) {
              // handle the error
              }
              • 4. Re: SQL Exception cannot find suitable driver
                843859
                I used
                catch (Exception ex) {
                ex.printStackTrace();
                }
                Edited by: Angelwinged_Devil on Feb 24, 2008 5:43 AM
                • 5. Re: SQL Exception cannot find suitable driver
                  843859
                  OK. And what did it say? Is the Class#forName() executed succesfully anyway?

                  By the way, you changed port 3306 in your first code to post 3307 in your beforelast reply. Why?
                  • 6. Re: SQL Exception cannot find suitable driver
                    843859
                    because I searched for sql error 8001 and found something about the connection being rejected so tried another port to see if I got any different errors

                    I got this

                    java.lang.ClassNotFoundException: org.gjt.mm.mysql.Driver
                    at java.net.URLClassLoader$1.run(Unknown Source)
                    at java.security.AccessController.doPrivileged(Native Method)
                    at java.net.URLClassLoader.findClass(Unknown Source)
                    at java.lang.ClassLoader.loadClass(Unknown Source)
                    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
                    at java.lang.ClassLoader.loadClass(Unknown Source)
                    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
                    at java.lang.Class.forName0(Native Method)
                    at java.lang.Class.forName(Unknown Source)
                    at dbcmd.main(dbcmd.java:13)
                    • 7. Re: SQL Exception cannot find suitable driver
                      843859
                      There is the real cause of your problem! It means that the given class cannot be found in the classpath while it is required. Put the MySQL JDBC driver JAR in the classpath and you're fine.

                      That's what I was meaning with that you should not swallow exceptions. The code comment in your first post even clearly stated "handle the error". Exceptions are not to be ignored, it would make debugging and finding the cause very hard otherwise.

                      By the way, you should be using com.mysql.jdbc.Driver and not org.gjt.mm.mysql.Driver.
                      • 8. Re: SQL Exception cannot find suitable driver
                        843859
                        I tried both com... and org... drivers, the jar is already in the classpath

                        edit
                        this is my classpath btw
                        C:\Programmer\Java\jdk1.6.0_02\jre\lib\ext

                        Edited by: Angelwinged_Devil on Feb 24, 2008 7:00 AM
                        • 9. Re: SQL Exception cannot find suitable driver
                          843859
                          The exception says that the JAR is not in the classpath.

                          The classpath used depends on how you're running the Java Application. Get through some basic Java tutorials. If you're using java.exe, I recommend using the -cp parameter.
                          • 10. Re: SQL Exception cannot find suitable driver
                            843859
                            I have the jar both in the local directory and in the ext folder, I tried running it like this java -cp . dbcmd

                            still doesn't work...
                            very weird
                            • 11. Re: SQL Exception cannot find suitable driver
                              796254
                              Angelwinged_Devil wrote:
                              I have the jar both in the local directory and in the ext folder, I tried running it like this java -cp . dbcmd

                              still doesn't work...
                              very weird
                              Weird? No, just wrong.

                              You should not be putting any JARs in the /ext folder. Utterly wrong.

                              Using java -classpath is correct, but you need more than the "dot" to indicate the current directory. You have to list JAR files individually with enough path for the class loader to find them.

                              So if you have the MySQL driver JAR in the same directory where the .class file is, you'd do it like this:

                              java -classpath .;mysql-driver.jar <your fully resolved class with the main you'd like to run>

                              %
                              • 12. Re: SQL Exception cannot find suitable driver
                                843859
                                java -cp .;mysql-connector-java-5.1.5-bin.jar dbcmd

                                using this gives me the same error
                                unless I change it to org.gjt.mm.mysql.Driver
                                thank you for all the help :D I love you \o/ in a totally non gay way
                                • 13. Re: SQL Exception cannot find suitable driver
                                  796254
                                  Angelwinged_Devil wrote:
                                  java -cp .;mysql-connector-java-5.1.5-bin.jar dbcmd

                                  using this gives me the same error
                                  Sorry, can't be bothered going back and reading your previous post to see what the same error was.
                                  unless I change it to org.gjt.mm.mysql.Driver
                                  No, that's wrong.
                                  thank you for all the help :D I love you \o/ in a totally non gay way
                                  Your code is still bad, and this is still wrong.

                                  %
                                  • 14. Re: SQL Exception cannot find suitable driver
                                    843859
                                    duffymo wrote:
                                    No, that's wrong.
                                    it's correct and not wrong, reading the documentation I should use that driver when using aspect

                                    Edited by: Angelwinged_Devil on Feb 24, 2008 8:57 AM

                                    ok another edit (the other one was to fix quotes)

                                    The org.gjt.mm.mysql.Driver class name is also usable to remain backward-compatible with MM.MySQL. You should use this class name when registering the driver, or when otherwise configuring software to use MySQL Connector/J
                                    from the documentationreadme
                                    1 2 Previous Next