简体   繁体   English

例外:找不到适用于jdbc:mysql的驱动程序

[英]Exception: No suitable driver found for jdbc:mysql

I am trying to connect to a mysql database by using this simple code. 我试图通过使用此简单的代码连接到mysql数据库。

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. 我的数据库没有问题,此代码/连接在netbeans上有效。 The StackTrace i am getting is - 我得到的StackTrace是-

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. 我正在使用64位Windows 7,并使用5.1版本的Connector / ODBC驱动程序64位。 On the ODBC all seems to connect and the test was successful. 在ODBC上似乎所有人都可以连接,并且测试成功。 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. 转到netbeans或您正在使用的任何IDE中的``运行''菜单=>``设置项目配置''然后``自定义'',然后从左侧的下拉菜单中选择``库'',然后添加相应的驱动程序文件jar或文件夹。 Click OK. 单击确定。

jdbc:mysql://localhost/books is a URL that you use to connect to MySQL directly, using the MySQL JDBC driver. jdbc:mysql://localhost/books是一个URL,可用于使用MySQL JDBC驱动程序直接连接到MySQL。 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 ). JDBC / ODBC驱动程序使用的URL是不同的(请参阅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. 不鼓励使用此JDBC / ODBC桥,并且仅应将其用于访问不提供任何JDBC驱动程序的数据库。 This is not the case of MySQL. MySQL并非如此。 Use Connector/J , their JDBC driver. 使用他们的JDBC驱动程序Connector / J。 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). 在类路径中拥有此驱动程序后,就可以使用当前使用的URL,并从类路径中删除JDBC / ODBC驱动程序(以及从代码中加载它)。

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

This error creeped on me because I forgot to add the Class.forName line. 因为我忘了添加Class.forName行,所以这个错误蔓延到我身上。 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. mysql驱动程序jar位于类路径上,但是没有人隐式加载该驱动程序类,因此会话工厂找不到任何已加载的驱动程序类。 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. 如果您打算将它与jdbc:mysql://连接一起使用,则应为Class.forName(“ com.mysql.jdbc.Driver”)

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. 转到“ JCreator配置”菜单,然后单击“选项”,然后单击“ JDK配置文件”,然后双击到使用自动提及的ur版本,然后单击“添加存档”,然后转到该路径-> C:\\ Program Files \\ MySQL \\ MySQL Tools for 5.0 \\ java \\ lib \\ mysql-connector-java-5.0.4-bin.jar按确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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