苦苦挣扎 - Class not found 异常:com.mysql.jdbc.Driver in JAVA

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6699796/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:29:15  来源:igfitidea点击:

Struggling with - Class not found exception: com.mysql.jdbc.Driver in JAVA

mysqljdbcdatabase-connectionclassnotfoundexceptionnetbeans7.0

提问by K.P.

In Netbeans ide 7.0, I am facing this particular problem. I wrote the following line of code in my program

在 Netbeans ide 7.0 中,我面临着这个特殊问题。我在我的程序中编写了以下代码行

Class.forName("com.mysql.jdbc.Driver").newInstance();

Class.forName("com.mysql.jdbc.Driver").newInstance();

This particular line is throwing an exception. And when I connected the same mysql db server from the "Services -> Databases -> Drivers -> MySQL(Connector/Jdriver)" in the netbeans 7.0, it got connected. When I went into the properties of that connection I found that it is connecting the mysql server using the same class which I used in the line of code.

此特定行引发异常。当我从 netbeans 7.0 中的“服务 -> 数据库 -> 驱动程序 -> MySQL(连接器/Jdriver)”连接同一个 mysql 数据库服务器时,它连接上了。当我进入该连接的属性时,我发现它正在使用我在代码行中使用的相同类连接 mysql 服务器。

I also read some of the forums such as

我还阅读了一些论坛,例如

http://www.javakb.com/Uwe/Forum.aspx/java-setup/11895/ClassNotFoundException-com-mysql-jdbc-Driver-from-netbeans

http://www.javakb.com/Uwe/Forum.aspx/java-setup/11895/ClassNotFoundException-com-mysql-jdbc-Driver-from-netbeans

but this 1 is for netbeans on Ubuntu. What about Windows?

但是这个 1 是针对 Ubuntu 上的 netbeans 的。窗户呢?

also, went through all of the responses in here

另外,浏览了这里的所有回复

http://forums.mysql.com/read.php?60,42473,42473#msg-42473

http://forums.mysql.com/read.php?60,42473,42473#msg-42473

I don't know what is wrong? Please help.

我不知道有什么问题?请帮忙。

采纳答案by antonis_wrx

You need to download the connector from here: http://www.mysql.com/products/connector/and include it in your classpath

您需要从这里下载连接器:http: //www.mysql.com/products/connector/并将其包含在您的类路径中

回答by K.P.

Solution : Well the problem here was different. My netbeans was able to connect MySQL server through its own predefined services. That was because the path of that class was defined in the driver it was using. But in the project I was building, the class-path for MySQL driver was not set. So I just added the same driver "MySQL JDBC Driver" to my Project.

解决方案:嗯,这里的问题是不同的。我的 netbeans 能够通过它自己的预定义服务连接 MySQL 服务器。那是因为该类的路径是在它使用的驱动程序中定义的。但是在我正在构建的项目中,没有设置 MySQL 驱动程序的类路径。所以我只是在我的项目中添加了相同的驱动程序“MySQL JDBC 驱动程序”。

It can be done, just go to the Project navigator which is on the left side. There you'll find the Libraries attached to your Project at the bottom of Project navigator. Then right click on the library, select "Add Library..." and then select the particular library from the Global Libraries present. In my case the connector class was present in the netbeans but the library was not added to my project.

可以做到,只需转到左侧的项目导航器即可。在那里,您会在项目导航器的底部找到附加到您的项目的库。然后右键单击库,选择“添加库...”,然后从现有的全局库中选择特定的库。在我的情况下,连接器类存在于 netbeans 中,但该库未添加到我的项目中。

Thank you btw for your precious time.

顺便说一句,谢谢您的宝贵时间。

回答by Viraj Nalawade

yes

是的

  1. You don't have mysql-connector.jar in your Classpath. as stated earlier this jar file contains "com.mysql.jdbc.Driver" class it must be present in classpath in order to successful connection to mysql database. you can downlad mysql-connector.jar from mysql.com.
  2. mysql-connector.jar is in your classpath but somehow your classpath is getting overridden. Classpath is tricky in Java and classpath specified in jar may override CLASSPATH path variable. See how classpath works in Java to understand this issue in detail.
  3. mysql-connector.jar is in classpath but current user doesn't have read permission. This problem often happens in Unix or Linux operating system which has sophisticated file and directory permission based on user, group and owner level. just get the right permission and run your program again.
  1. 您的类路径中没有 mysql-connector.jar。如前所述,这个 jar 文件包含“com.mysql.jdbc.Driver”类,它必须存在于类路径中才能成功连接到 mysql 数据库。你可以从 mysql.com 下载 mysql-connector.jar。
  2. mysql-connector.jar 在您的类路径中,但不知何故您的类路径被覆盖了。Java 中的类路径很棘手,jar 中指定的类路径可能会覆盖 CLASSPATH 路径变量。查看类路径如何在 Java 中工作以详细了解此问题。
  3. mysql-connector.jar 在类路径中,但当前用户没有读取权限。这个问题经常发生在具有基于用户、组和所有者级别的复杂文件和目录权限的 Unix 或 Linux 操作系统中。只需获得正确的权限并再次运行您的程序。

Read more: http://javarevisited.blogspot.com/2012/03/jdbc-javalangclassnotfoundexception.html#ixzz2Ply4zLFF

阅读更多:http: //javarevisited.blogspot.com/2012/03/jdbc-javalangclassnotfoundexception.html#ixzz2Ply4zLFF