英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论