如何解决 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21813069/
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
How to solve java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
提问by Akari
I've written this java application to respond the data from the command line and store it into a database :
我编写了这个 java 应用程序来响应来自命令行的数据并将其存储到数据库中:
import java.util.Scanner;
import java.sql.*;
public class Program {
public static void main(String[] args)throws ClassNotFoundException
{
Connection conn=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/DevOps_DB","root","root");
PreparedStatement st = conn.prepareStatement("INSERT INTO pers " + "VALUES ('"+args[0]+"'); ");
st.executeUpdate();
}
catch (SQLException ex) {
System.out.println("SQL Exception : "+ ex.getMessage());
System.out.println("Vendor Error : "+ ex.getErrorCode());
}
catch(ClassNotFoundException ex) {
ex.printStackTrace();
}
//
// for(String arg : args)
// {
// System.out.println(arg);
// }
}
}
But I've got the following exception :
但我有以下例外:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at Program.main(Program.java:18)
why ? ... any help to fix the problem ?
为什么 ?...任何帮助解决问题?
EDIT :
编辑 :
I added the .jar file , see the following image :
我添加了 .jar 文件,见下图:
采纳答案by Aniket Thakur
You need to download mysql connector jarfile(driver implementation) and add it to your class path.
您需要下载mysql 连接器 jar文件(驱动程序实现)并将其添加到您的类路径中。
On the side note if your are using JDBC 4.0 (Java 7 and even 6 I think) then you need not use Class.forName("com.mysql.jdbc.Driver");
. Simply add the jar to the class path. Driver implementation will automatically be searched and loaded from class path.
附带说明一下,如果您使用的是 JDBC 4.0(我认为是 Java 7 甚至 6),那么您不需要使用Class.forName("com.mysql.jdbc.Driver");
. 只需将 jar 添加到类路径。驱动程序实现将自动从类路径中搜索和加载。
回答by Suneet Bansal
@Akari : this is related to the .jar file for MySql. Try to check whether the .jar for MySql is set to the classpath or not.
@Akari:这与 MySql 的 .jar 文件有关。尝试检查 MySql 的 .jar 是否设置为类路径。
回答by kkashyap1707
You have to download mysql connector jar file(driver implementation) and add it to your class path.
您必须下载 mysql 连接器 jar 文件(驱动程序实现)并将其添加到您的类路径中。
回答by Kirtikumar Thakur
ok you added the jar.do one thing open server, form window->show view->servers then double click on server
好的,您添加了 jar。做一件事打开服务器,窗体窗口-> 显示视图-> 服务器然后双击服务器
then it will open a new window then goto open Launch configuration then
然后它将打开一个新窗口然后转到打开启动配置然后
go to class path and agin add mysql-connector.jar click on ok.
转到类路径并 agin 添加 mysql-connector.jar 单击确定。
回答by Anshuman
Extract the mysql driver which you can download from a link! and copy the .jar file of the mysql zip and paste it here in "lib"Screenshot of the location u have to paste in into (in the interface)
提取可以从链接下载的 mysql 驱动程序!并复制 mysql zip 的 .jar 文件并将其粘贴到“lib”中您必须粘贴到的位置的屏幕截图(在界面中)