简体   繁体   中英

Exception: No suitable driver found for jdbc:mysql

I am trying to connect to a mysql database by using this simple code.

import java.sql.*;
public class OdbcAccessConnection_1 {
  public static void main(String [] args) {
    Connection con = null;
    try {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
    // Connect with a url string
      con = DriverManager.getConnection("jdbc:mysql://localhost/books","root","1234");
      System.out.println("Connection ok.");
      con.close();
    } catch (Exception e) {
      System.err.println("Exception: "+e.getMessage());
      e.printStackTrace();
    }
  }
}

All it does is tell me if the connection is working. There is no problem with my database and this code/connection work on netbeans. The StackTrace i am getting is -

the java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/books
        at java.sql.DriverManager.getConnection(DriverManager.java:602)
        at java.sql.DriverManager.getConnection(DriverManager.java:185)
        at OdbcAccessConnection_1.main(OdbcAccessConnection_1.java:13)

I am working on 64 bit windows 7 and using 5.1 versions of the Connector/ODBC driver 64 bit. On the ODBC all seems to connect and the test was successful. But when i run the code i get the stack trace above. I am missing something very simple so any input and help would be very much appreciated. Thank you:)

Go to Run menu in netbeans or whatever IDE you are using =>Set Project Configuration then Customize.Then choose the Libraries on left dropdown menu Add your appropriate driver file either jar or folder. Click OK.

jdbc:mysql://localhost/books is a URL that you use to connect to MySQL directly, using the MySQL JDBC driver. The URL used by the JDBC/ODBC driver is different (see http://docs.oracle.com/javase/1.3/docs/guide/jdbc/getstart/bridge.doc.html ).

The usage of this JDBC/ODBC bridge is discouraged, and should only be used to access a database that doesn't provide any JDBC driver. This is not the case of MySQL. Use Connector/J , their JDBC driver. Once you have this driver in your classpath, you can use the URL you're currently using, and remove the JDBC/ODBC driver from your classpath (and its loading from your code).

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/books","root","1234");

This error creeped on me because I forgot to add the Class.forName line. The mysql driver jar was on the classpath but no one implicitly loads the driver class, so the session factory can't find any driver classes loaded. Thus the purpose of this line.

In your case, you're loading the wrong thing. It should be Class.forName("com.mysql.jdbc.Driver") if you intend to use it with a jdbc:mysql:// connection.

best solution bhai logo -:

go to JCreator configure menu then click to options then JDK profiles then double click to whatever the version ur using automatically mention there then click to add archive then go to that path -> C:\\Program Files\\MySQL\\MySQL Tools for 5.0\\java\\lib\\mysql-connector-java-5.0.4-bin.jar press ok.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM