class.forName(“com.mysql.jdbc.driver”) 在Eclipse中返回一个ClassNotFoundException。

huangapple go评论88阅读模式
英文:

class.forName("com.mysql.jdbc.driver") returns a classnotfound exception in Eclips

问题

我正在使用Eclipse 2020-06和MySQL 8.0.21在我的Mac上。
我下载了JDBC驱动程序并将其添加为我的Eclipse项目属性中的外部JAR。

当我运行这行代码时:

Class.forName("com.mysql.jdbc.driver");

它抛出了ClassNotFoundException异常。

然而,在相同的代码中,我能够成功连接到MySQL并运行查询。

DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "root")

成功并运行我的查询。

为什么Class.forname抛出该异常,尽管能够使用完全相同的驱动程序创建JDBC连接呢?
请帮忙。

英文:

I am using Eclipse 2020-06 and MySQL 8.0.21 on my Mac.
I downloaded the JDBC driver and added that as an external JAR in my Eclipse project properties.

When I run this line of code:

Class.forName("com.mysql.jdbc.driver");

it throws the
>ClassNotFoundException exception.

However, in the same code, I am able to connect to MySQL and run queries successfully.
>DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "root")
succeeds and runs my query.

Why does Class.forname throw that exception despite being able to create a JDBC connection using the very same driver?
Please help.

答案1

得分: 0

你需要根据所提到的MySQL版本使用以下语句:

Class.forName("com.mysql.cj.jdbc.Driver");
英文:

Here you need to use the below statement as per mentioned MySQL version

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

答案2

得分: 0

com.mysql.jdbc.driver 已经被废弃很长时间了,mysql建议使用 com.mysql.cj.jdbc.Driver。也许您的mysql连接器已经将其移除。您可以打开JAR文件查看旧驱动程序是否存在。

英文:

com.mysql.jdbc.driver has been deprecated for a long time,mysql suggest to use com.mysql.cj.jdbc.Driver. Maybe your mysql connector has removed it. you can open the JAR file to see whether the old driver exists.

huangapple
  • 本文由 发表于 2020年7月28日 03:44:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63122449.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定