找不到适合 jdbc mysql 的驱动程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11485532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No suitable driver found for jdbc mysql?
提问by RGB314
I am trying to write a program to connect to a MySQL database in eclipse, but I get the error "java.sql.SQLException: No suitable driver found".
我正在尝试编写一个程序来连接到 eclipse 中的 MySQL 数据库,但是我收到错误"java.sql.SQLException: 找不到合适的驱动程序"。
The java code is:
Java代码是:
import java.sql.*;
public class FirstExample {
//static final String S_JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String S_DB_URL = "jdbc:mysql://localhost:3306/emp";
static final String S_USER = "root";
static final String S_PASS = "root";
public static void main(String[] args) {
try {
System.out.println("Connecting to database...");
//Class.forName(S_JDBC_DRIVER);
Connection connection = DriverManager.getConnection(S_DB_URL,
S_USER, S_PASS);
System.out.println("Creating statement...");
Statement statement = connection.createStatement();
String sql = "SELECT * FROM Employee";
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
int iId = resultSet.getInt("id");
int iAge = resultSet.getInt("age");
String sFirst = resultSet.getString("fname");
String sLast = resultSet.getString("lname");
System.out.print("ID: " + iId);
System.out.print("\tAge: " + iAge);
System.out.print("\tFirst: " + sFirst);
System.out.println("\tLast: " + sLast);
}
resultSet.close();
statement.close();
connection.close();
} catch (SQLException se) {
for (Throwable t : se) {
t.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}
}
The output in the console tab is:
控制台选项卡中的输出是:
Connecting to database...
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/emp
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at FirstExample.main(FirstExample.java:21)
Goodbye!
I have used the MySQL Connector/J. It is unzipped in the MySQL installation directory and the jar file is added to the CLASSPATH.
我使用过 MySQL Connector/J。解压到MySQL安装目录下,将jar文件添加到CLASSPATH中。
Also refer to this image. There is an ! mark at the project root.image01
另请参阅此图像。有一个 !标记在项目根。图像01
I get the error as in the next image: image02when I include the 2 commented statements:
当我包含 2 个带注释的语句时,出现如下图所示的错误:image02:
static final String S_JDBC_DRIVER = "com.mysql.jdbc.Driver";
Class.forName(S_JDBC_DRIVER);
采纳答案by Mark Rotteveel
For all but the most trivial applications the CLASSPATH
environment variable is NOTused. Normally the libraries are include in the Class-Path
entry in the manifest of the jar, or in the -cp
option of the java commandline.
除了最琐碎的应用程序之外,其他所有应用程序都不使用CLASSPATH
环境变量。通常,这些库包含在jar 清单的条目中,或包含在 java 命令行的选项中。Class-Path
-cp
In this case you need to add the MySQL JDBC driver to the buildpath of your Eclipse project.
在这种情况下,您需要将 MySQL JDBC 驱动程序添加到 Eclipse 项目的构建路径中。
回答by André
I had the same problem. I solved it by adding:
我有同样的问题。我通过添加解决了它:
Class.forName("com.mysql.jdbc.Driver");
回答by rɑ?d?ɑ
you can place the path like java -cp
pwd/mysql-connector-java-5.1.22-bin.jar:. <classname>
.
你可以像java -cp
pwd一样放置路径/mysql-connector-java-5.1.22-bin.jar:. <classname>
。
make sure that your in same directory where the mysql driver is .
确保您在 mysql 驱动程序所在的同一目录中。
Hope that helps .
希望有所帮助。
回答by Sumit Jadiya
Load Driver class just before getting the connection.
在获取连接之前加载驱动程序类。
Use this code:
使用此代码:
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_db", "user", "passw");
回答by Douglas olupot
Alternatively you can also add the installed jar file to your eclipse project by selecting the project in eclipse, right click it and go down to properties, select the Java Build Path>>select the Libraries Tab>>Add external jar file and browse for the installed mysql-connector-java.jar file or any mysql java connector file int the /usr/share/java/ directory for most ubuntu users. Click okay and rebuild your project. Good luck
或者,您也可以通过在 eclipse 中选择项目,将安装的 jar 文件添加到 eclipse 项目中,右键单击它并转到属性,选择 Java 构建路径>>选择库选项卡>>添加外部 jar 文件并浏览对于大多数 ubuntu 用户,已在 /usr/share/java/ 目录中安装了 mysql-connector-java.jar 文件或任何 mysql java 连接器文件。单击确定并重建您的项目。祝你好运
回答by Will.Tom.ZT
I encountered the same problem as you, but I handled it as follows: I copied the jar, which is called mysql-connector-java-5.1.23-bin.jar, into the \Apache Software Foundation\Tomcat 6.0\lib, and restarted tomcat. Hope that helps
我遇到了和你一样的问题,但是我是这样处理的:我把名为mysql-connector-java-5.1.23-bin.jar的jar复制到\Apache Software Foundation\Tomcat 6.0\lib中,然后重启tomcat。希望有帮助