Nagen
Nagen

Reputation: 31

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.

Upvotes: 0

Views: 1341

Answers (2)

haoyu wang
haoyu wang

Reputation: 1377

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.

Upvotes: 0

hitesh bedre
hitesh bedre

Reputation: 589

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

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

Upvotes: 0

Related Questions