5

My Spring Boot Project trying connect to MYSQL database with driver mysql-connector-java. I have import newest mysql driver and spring-boot-starter-data-jpa

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

I have configured database connection in application.properties file

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
spring.datasource.username=somethingfunny
spring.datasource.password=somethingfunny
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
#spring.jpa.show-sql: true

MYSQL version is 8.0.26

enter image description here

Spring boot version 2.6.2

enter image description here

When run project with Intellij I get error

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: com.mysql.cj.jdbc.Driver at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.3.14.jar:5.3.14] at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.14.jar:5.3.14] ... 35 common frames omitted Caused by: java.lang.IllegalStateException: Cannot load driver class: com.mysql.cj.jdbc.Driver at org.springframework.util.Assert.state(Assert.java:97) ~[spring-core-5.3.14.jar:5.3.14] at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineDriverClassName(DataSourceProperties.java:241) ~[spring-boot-autoconfigure-2.6.2.jar:2.6.2] at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.initializeDataSourceBuilder(DataSourceProperties.java:193) ~[spring-boot-autoconfigure-2.6.2.jar:2.6.2] at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.createDataSource(DataSourceConfiguration.java:48) ~[spring-boot-autoconfigure-2.6.2.jar:2.6.2] at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari.dataSource(DataSourceConfiguration.java:90) ~[spring-boot-autoconfigure-2.6.2.jar:2.6.2] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na] at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na] at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.3.14.jar:5.3.14] ... 36 common frames omitted

I have refered the post about Cannot load driver class: com.mysql.jdbc.Driver (NOT com.mysql.cj.jdbc.Driver), I cannot apply for my project because my project get error when using com.mysql.cj.jdbc.Driver but not com.mysql.jdbc.Driver.

I also refered this post Cannot load driver class: com.mysql.cj.jdbc.Driver. But i can not find correct answer(the answer is marked corrected) for this error.

How to fix this error ?

2
  • Check the JDK version that you are using Commented Dec 28, 2021 at 7:46
  • Remove the driverClassName line. It hasn't been needed since 2006. Commented Apr 15, 2024 at 4:18

6 Answers 6

5
  1. Use this dependency in pom.xml

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.19</version>
    </dependency>
    
  2. Use this property in your application.properties file

    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    
Sign up to request clarification or add additional context in comments.

Comments

2

I have fix this issue. In Intellij right click to project, choose maven, choose reload project. Now Intellij will add mysql driver to the project

enter image description here

Comments

1

Have you checked which version of MySQL connector do you have?

Since you haven't specified the version in your pom.xml, there is a chance that it pulled version 5 and now it is complaining about that .cj which is required for version 8.

That might be the reason why is it working without .cj ( com.mysql.jdbc.Driver ), because it pulled the version 5. Manually add the version in your pom.xml and keep .cj as it is.

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

EDIT:

This dependency from above, if you visit it in Maven central

https://mvnrepository.com/artifact/mysql/mysql-connector-java

Is now moved to

<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <version>8.0.32</version>
</dependency>

EDIT:

After that make sure that you pull your dependencies. But make sure that you check in your project if you do have ONLY version 8 after "mvn clean install".

As of the picture that you shared "mysql version", that means nothing, the exception is regarding mysql-connector jar, it has nothing to do with the workbench, you can still have workbench version 5, and use mysql-connector jar version 8, it will make no difference.

In any version of mysql workbench ( 5 or 8 ):

mysql-connector 8 jar = requires .cj

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

mysql-connector 5 jar = does not require .cj

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

The only thing that you need to do is define the version of mysql-connector-java in your pom.xml

Comments

0

Going off of one of the above answers https://stackoverflow.com/a/70514438/14111809, but with a Gradle instead of Maven (in IntelliJ IDE), this fixed the issue for me: reload gradle project

Comments

0

Add this in pom.xml

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

Comments

-1

Fix the line that sets jdbc driver class as below: I don't think it matters but driver-class-name is more common. And check if your build file is up to date. Might as well try re-building using pom.xml

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

2 Comments

she already said "my project get error when using com.mysql.cj.jdbc.Driver but not com.mysql.jdbc.Driver." I don't that that renaming the key to driver-class-name would be helpful.
com.mysql.jdbc.Driver -> com.mysql.cj.jdbc.Driver is relevant to mysql version and I didn't say anything about that. And I did say I don't think it matters but try if it fixes it. And I did say build it again because often that fixes it. And yeah there you go. As you can see from OP's answer it was simple build-again-and-fixed kind of deal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.