ClassNotFoundException with com.mysql.cj.jdbc.Driver, MySQL Connector and IntelliJ IDEA [duplicate]
Asked Answered
C

6

14

I'm building up a Maven Java 1.8 project in which I've included the MySQL Connector as a dependency:

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.39</version>
    </dependency>

In my application I've a singleton that holds the MySQL connection so when the application starts, the MySQL connector is triggered but I got a ClassNotFoundException for the driver that I'm using: com.mysql.cj.jdbc.Driver.

The JDBC URL that I'm using is:

jdbc:mysql://localhost:3306/mydb?autoReconnect=true&useSSL=false

I'm using IntelliJ IDEA (2017.2) IDE.

I've checked in File -> Project Structure -> Libraries -> I see "Maven: mysql:mysql-connector-java:5.1.392" as a Library.

I've also checked under File -> Project Structure -> Artifacts -> and under myapp.war exploded -> WEB-INF -> lib -> Maven: mysql:mysql-connector-java:5.1.39 is present.

From View -> Tool Windows -> Database, I've succesfully made a MySQL connection dowloading the suggested driver (MySQL Connector 5.1.35).

How can I tell the application that it has to load the MySQL driver at runtime?

Thanks in advance

Candicandia answered 24/7, 2017 at 17:16 Comment(2)
Are you using a Class.forName(...) statement immediately before opening the connection? If so, then try commenting it out and see if that helps.Grubstake
Yes, I'm using Class.forName(...) method. Commenting it, I've got a java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/dbname?autoReconnect=true&useSSL=false :(Candicandia
B
14

Try upgrading your driver. Coz Mysql community has updated class name from com.mysql.jdbc.Driver to com.mysql.cj.jdbc.Driver Check out more on MySql Community

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>

Or Download jar file direct from here :

mysql-connector-java-8.0.11.jar

List of MySql jar file

Batory answered 10/6, 2018 at 9:25 Comment(1)
For SpringBoot 3.2.2 need to add dependency in build.gradle.kts: implementation("com.mysql:mysql-connector-j"). And in application.properties you can use: spring.datasource.driver-class-name=com.mysql.cj.jdbc.DriverGoethe
C
5

It looks like you are trying to use the JDBC DriverManager for version 8 check out this. Try to use com.mysql.jdbc.Driver driver instead of com.mysql.cj.jdbc.Driver.

Conduction answered 24/7, 2017 at 23:5 Comment(0)
O
3

If you're facing this problem with Eclipse, I've been following many different solutions but the one that worked for me is this:

  1. Right click your project folder and open up Properties.

  2. From the right panel, select Java Build Path then go to Libraries tab.

  3. Select Add External JARs to import the mysql driver.

  4. From the right panel, select Deployment Assembly.

  5. Select Add..., then select Java Build Path Entries and click Next.

  6. You should see the sql driver on the list. Select it and click first.

And that's it! Try to run it again!

Oceangoing answered 17/10, 2019 at 11:16 Comment(1)
This fixed my problem, thanks! In Eclipse 2019-12, the Deployment Assembly option is located under Project -> Properties on the left-hand side, just before Java Build Path.Acker
U
1

Just Do ItDownload Mysql Driver

Download Mysql Driver bY php storm IDE No Need to download manually

Upland answered 22/9, 2020 at 22:36 Comment(0)
A
0

Try version 6.x of the driver. It has the missing class: https://mvnbuild.com/artifact/mysql/mysql-connector-java/6.0.6

<!-- https://mvnbuild.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>6.0.6</version>
</dependency>
Aerodontia answered 10/8, 2017 at 3:54 Comment(0)
E
0

Alternatively you can load the mysqlconnector/j.jar file to the \lib\ext folder in the path where you installed the jre.

Edith answered 26/5, 2020 at 13:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.