简体   繁体   中英

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

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.

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM