java.lang.ClassNotFoundException:即使导入库后com.mysql.jdbc.Driver错误 [英] java.lang.ClassNotFoundException: com.mysql.jdbc.Driver error even after importing library
问题描述
我已导入我的库"mysql-connector-java-5.1.39" 正如大多数人回答的一样, 但我仍然收到此错误
I have imported my library "mysql-connector-java-5.1.39" as answered by most people to exact same question , but I am still getting this error
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
这是测试类的代码
package database;
import java.sql.Connection;
import java.sql.DriverManager;
public class Main {
public static void main(String[] args) throws Exception {
getConnection();
}
public static Connection getConnection() throws Exception {
try{
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/test";
String username="root";
String password="root";
Class.forName(driver);
Connection conn= DriverManager.getConnection(url,username,password);
System.out.println("connected");
return conn;
}
catch (Exception e){
System.out.println(e);
}
return null;
}
}
使用mac OS.
推荐答案
每个人都在跟着我重复. (:-))
Everybody repeat after me. ( :-) )
"java.lang.ClassNotFoundException:com.mysql.jdbc.Driver"不是编译错误.
"java.lang.ClassNotFoundException: com.mysql.jdbc.Driver" is NOT a compilation error.
因此,更改>> build<<路径或添加import
不能解决问题.
Therefore, changing the >>build<< path or adding an import
cannot fix the problem.
解决方案是在运行应用程序时确保JAR文件位于类路径中.例如,如果您的测试类在bin\database\Main.class
中,而驱动程序JAR在lib
...
The solution is to make sure that the JAR file is on the classpath when you run the application. For example, if your test class is in bin\database\Main.class
and the driver JAR is in lib
...
$ java -classpath bin:lib/mysql-connector-java-5.1.39.jar database.Main
这篇关于java.lang.ClassNotFoundException:即使导入库后com.mysql.jdbc.Driver错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!