运行时错误:java.lang.ClassNotFoundException:com.mysql.jdbc.Driver
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18158864/
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
runtime error: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
提问by user2669813
I am new to mysql and jdbc and I am getting the error in this title. I have been searching all day and cannot find a solution that works for me.
我是 mysql 和 jdbc 的新手,我收到了这个标题中的错误。我已经搜索了一整天,但找不到适合我的解决方案。
What I have tried: uninstall/reinstall mysql, copy paste mysql-connector-java-5.1.25-bin.jar and ojdbc7.jar to same location as the .class file I am trying to run, rebuilt the program in a different directory, and probably a couple other things.
我尝试过的:卸载/重新安装 mysql,将 mysql-connector-java-5.1.25-bin.jar 和 ojdbc7.jar 复制粘贴到与我尝试运行的 .class 文件相同的位置,在不同的目录中重建程序,可能还有其他一些事情。
I am using notepad++ for coding and the windows command prompt to compile and run. it compiles fine but I try to run with
我正在使用记事本 ++ 进行编码,并使用 windows 命令提示符进行编译和运行。它编译得很好,但我尝试运行
C:\Projects\bin>java -cp . ClientBase
C:\Projects\bin>java -cp 。客户群
The output is:
输出是:
java.lang.ClassnNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassloader$1.run(URLClassLoader.java:336)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:432)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:188)
at ClientBase.main(ClientBase.java:21)
Goodbye.
java.lang.ClassnNotFoundException: com.mysql.jdbc.Driver
在 java.net.URLClassloader$1.run(URLClassLoader.java:336)
在 java.net.URLClassLoader$1.run(URLClassLoader.java:355)
在 java.security.AccessController.doPrivileged(Native Method)
在 java.net.URLClassLoader .findClass(URLClassLoader.java:354)
在 java.lang.ClassLoader.loadClass(ClassLoader.java:432)
在 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
在 java.lang.ClassLoader.loadClass( ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:188)
at ClientBase.main(ClientBase.java:21)
再见。
// import packages
import java.sql.*;
// create class ClientBase
public class ClientBase{
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/CLIENTBASE";
// Database credentials
static final String USER = "root";
static final String PASS = "";
// Begin method main
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
// register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
// Open connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "SELECT id, name, address, address 2, city, phone, state, zip, fax FROM CLIENTBASE";
ResultSet rs = stmt.executeQuery(sql);
// Extract data from result set
while(rs.next()){
// Retrieve by column name
int id = rs.getInt("id");
String name = rs.getString("name");
String address = rs.getString("address");
String address2 = rs.getString("address2");
String city = rs.getString("city");
String phone = rs.getString("phone");
String state = rs.getString("state");
String zip = rs.getString("zip");
String fax = rs.getString("fax");
// Display values
System.out.print("ID: " + id);
System.out.print(" Name: " + name);
System.out.println("Address:" + address);
System.out.println(address2);
System.out.print("City:" + city);
System.out.print(" State: " + state);
System.out.println(" Zip: " + zip);
System.out.print("Phone: " + phone);
System.out.println(" Fax: " + fax);
} // end while
// clean up
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
// Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
// Handle errors for Class.forName
e.printStackTrace();
}finally{
// finally block used to close resources
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se){
se.printStackTrace();
} // end finally
} // end try
System.out.println("Goodbye.");
} // End method main
} // end class ClientBase
I should also say that I am going off an online tutorial for this code. It is not exactly how they have it as I decided to make something a little different than theirs, but it is generally the same. I don't think it is a code problem though from what the error is.
我还应该说,我将关闭此代码的在线教程。这并不完全是他们的方式,因为我决定做一些与他们的有点不同的东西,但大体上是一样的。我认为这不是代码问题,但错误是什么。
Any help would be appreciated! I'm going crazy!
任何帮助,将不胜感激!我要疯了!
采纳答案by Jk1
You need to add a connector library to the Runtime classpath:
您需要将连接器库添加到运行时类路径:
java -cp .;mysql-connector-java-5.1.25-bin.jar ClientBase
My example uses Windows classpath separator ";"
, on other systems it may be different (":"
on Linux/Mac). It also assumes, that mysql-connector-java-5.1.25-bin.jar
is located on the same folder. If it's not the case, then put a path to the library instead of the plain name.
我的示例使用 Windows 类路径分隔符";"
,在其他系统上它可能不同(":"
在 Linux/Mac 上)。它还假定mysql-connector-java-5.1.25-bin.jar
位于同一文件夹中。如果不是这种情况,则输入库的路径而不是普通名称。
ClientBase
stands for Java class file name here
ClientBase
这里代表Java类文件名
c:\>javac Test.java
c:\>java -cp .;F:\CK\JavaTest\JDBCTutorial\mysql-connector-java-5.1.18-bin Test
回答by alfasin
What did you import ? From the documentation: http://dev.mysql.com/doc/connector-j/en/connector-j-usagenotes-connect-drivermanager.html
你导入了什么?从文档:http: //dev.mysql.com/doc/connector-j/en/connector-j-usagenotes-connect-drivermanager.html
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
// Notice, do not import com.mysql.jdbc.*
// or you will have problems!
Comment:
评论:
Why are you using notepad++ ? install an IDE (Eclipse/Netbeans/IntelliJ) - it'll be much easier to locate such problems (un-included jars for example)
你为什么使用记事本++?安装 IDE (Eclipse/Netbeans/IntelliJ) - 定位此类问题会容易得多(例如,未包含的 jars)
回答by prabhakaran
if u use netbeans,do following 1.Open Netbeans IDE 2.Right-click your Project. 3.Select Properties. 4.On the left-hand side click Libraries. 5.Under "Compile" tab - click Add Jar/Folder button. 6.Select Downloaded "mysql-connector-java-5.1.25-bin.jar" file (Download Connector/J from dev.mysql.com) 7.Click OK Run Again... Its work.
如果您使用 netbeans,请执行以下操作 1.打开 Netbeans IDE 2.右键单击您的项目。3.选择属性。4.在左侧单击库。5.在“编译”选项卡下 - 单击添加 Jar/文件夹按钮。6.选择下载的“mysql-connector-java-5.1.25-bin.jar”文件(从dev.mysql.com下载连接器/J) 7.单击确定再次运行...它的工作。
回答by agcala
Add mysql.connector-java-x.x.x-bin.jar
to your libraries folder. No need to import anything. Have a good one.
添加mysql.connector-java-x.x.x-bin.jar
到您的库文件夹。无需导入任何东西。祝你有个好的一天。
回答by HaRsH
I was also get same problem :-
How I solved for Linux system.
I was also get same problem :-
我是如何解决Linux 系统的。
1.)Download file mysql-connector-java-5.1.18-bin.jarfrom given link or other.
1.)从给定的链接或其他下载文件mysql-connector-java-5.1.18-bin.jar。
2.)Then paste it to same folder or directory of your class or file (on where you want connection to present)
2.)然后将其粘贴到您的类或文件的同一文件夹或目录中(在您想要连接的位置)
3.)Now use the following command by your linux command prompt.
3.)现在在你的 linux 命令提示符下使用以下命令。
java -cp .:mysql-connector-java-5.1.18-bin.jar YourFileName
..
java -cp .:mysql-connector-java-5.1.18-bin.jar YourFileName
..
Thats all you need to do... :)
这就是你需要做的... :)
回答by Bhola
If You Are Using Swing
you have to add External Jar of my-sql-connect
如果您正在使用Swing
,则必须添加my-sql-connect 的外部 Jar
else if You are Using jsp servlet
You have to add External jar and Copy that Jar into WEB_INF/libproject folder
否则,如果您正在使用,jsp servlet
您必须添加外部 jar 并将该 Jar 复制到WEB_INF/lib项目文件夹中