60
spring.freemarker.cache:false

spring.datasource.url=jdbc:mysql://localhost/mydb
spring.datasource.username=user
spring.datasource.password=pass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

I'm getting this error even with the driver-class-name defined:

java.lang.IllegalStateException: Cannot load driver class: com.mysql.jdbc.Driver at org.springframework.util.Assert.state(Assert.java:392) ~[spring-core-4.2.1.RELEASE.jar:4.2.1.RELEASE] at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.getDriverClassName(DataSourceProperties.java:153) ~[spring-boot-autoconfigure-1.3.0.M5.jar:1.3.0.M5] at org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbeddedConfiguration.dataSource(DataSourceAutoConfiguration.java:119) ~[spring-boot-autoconfigure-1.3.0.M5.jar:1.3.0.M5] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_60] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_60] at ......

and it goes on

1
  • It simply means mysql dependency is missing in your pom file. Please add it then reload. Commented Sep 23, 2022 at 6:01

16 Answers 16

93

If you're using Maven, add this to your pom.xml:

(Recommended) For MySQL 5.6, 5.7, 8.x and Java >= 8 use:

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

(Legacy) For MySQL <= 5.5 or Java <= 7 or JDBC < 4.2 use;

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

More details on Connector/J versions

Sign up to request clarification or add additional context in comments.

2 Comments

<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.0.8</version> </dependency> already have this on mine too
for java>=8, you can use as below in pom.xml <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <version>8.0.31</version> </dependency>
15

It might be that you are using the deprecated driver class name. I solved it by updating the property in application.properties file.

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

3 Comments

It's not showing deprecated in the latest Spring version.
Yes, but its deprecated. For more details you can check below link dev.mysql.com/doc/connector-j/8.0/en/…
While deprecated, the com.mysql.jdbc.Driver still exists, and can still be loaded. As such, this isn't a solution for the question.
10

Just confirm the following things.

  1. If you are under proxy make sure use VPN to connect to internal servers, iif you are accessing DEV/STG servers

  2. Make sure you add the following dependency inside correct location, like this

    <dependencies> <--- inside this section
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.15</version>
        </dependency>
    </dependencies>
    

    and not in

    <build> <--- Not this section
        <dependencies>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.15</version>
            </dependency>
        </dependencies>
    </build>
    
  3. And you don't need to use the following line

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

    The spring.datasource.url=jdbc:mysql://xxxxx automatically know which driver to fetch.

1 Comment

I tried everything but your third point did it. I just deleted the datasource-driver line and everything went great. Thank you!
9

If you are using IDEA and maven, you might forget reloading all maven projects, which causes the dependencies don't have the MySQL connector jar. And that's why when u run the application, there will be the error remind "com.mysql.cj.jdbc.Driver".

enter image description here

Comments

4

I will share more causes of this error:

  • I already seen this error many times using Intellij IDEA and running some integration test. The test fail with the message: Caused by: java.lang.IllegalStateException: Cannot load driver class: org.h2.Driver. After rebuild the project (Build > Rebuild Project) the error disappears.
  • Another cause of this error is a corrupted JAR. A college was receiving this error (also trying to run integration tests) and, after delete some related jars on .m2 directory, the error was gone.

Comments

3

The crux of the problem is that you're missing MySQL driver dependency.

One of the ways, as outlined by other answers, is to specify it in your build tool's configuration. However, if you do not want to do it and are using IntelliJ IDEA (though I'm pretty sure Eclipse has something similar), you can also add the dependency via it. Steps:

  1. Download desired jar from maven repository
  2. Open your project in Intellij IDEA
  3. File -> Project Structure -> Libraries
  4. Click New Project Library (green plus sign on the left), or press Alt + Insert keys
  5. Select Java
  6. Select your jar
  7. Press OK

Should look something like this:

enter image description here

Now you should be able to use the dependency you have just added.

Tested with IntelliJ IDEA 2017.3.4.

Comments

2

build.gradle

runtimeOnly 'mysql:mysql-connector-java'

application.yml

spring: 
    application:
        name: apevent
        version: 1.0
    datasource:
        driverClassName: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/applicaiton

2 Comments

It's showing some command is deprecated so Gradle 8.0 can't run properly. so this answer is only for before Gradle 8.0
Should use implementation rather than runtimeOnly
1

Probably someone still needs an answer (as me before) I solved it by adding the following dependency

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
</dependency>

Comments

1

I encountered the same problem now and solved it after providing the required dependency,

 <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.15</version>
    </dependency>

Comments

0

adding the following dependency solved my issue

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

Comments

0

I know this might be a little late but maybe anyone from the future might find this answer helpful. This problem arises from potentially three reasons

  • Incorrect maven or Gradle configuration

  • Incorrect spring configuration in the application.properties file

  • Wrong class name on the application.properties file.

  • To troubleshoot if you are using maven. do not include the version name. This mostly is a recipe for problems, especially in huge applications. use maven autocompletes features to save you a little time.

  • configure the data source correctly. There are a lot of answers on it to this question. make sure it's correct. sometimes there is a cj that is missing or included where it's not supposed to be. Trial and error might help you discover the problem

if neither of the above options works. Remove spring.datasource.driver-class-name=com.mysql.jdbc.driver completely. I don't know if it magic but this last one worked for me.

GO NERDS!!!

Comments

0

My problem is solved just by refresh the project, perhap the dependency did not load by the time you run the application

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review
0

I had the same issue. It happened because I forgot to declare the MySQL connector dependency in my pom file. I added the dependency, then boom! The error went away.

Comments

0

If you are using maven-shade-plugin to package your application in a fat jar, make sure you don't use its <minimizeJar>true</minimizeJar> option because this option will remove some classes from target jar. In my case it was the driver class and when I tried to deploy my app to k8s I got the error org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Cannot load JDBC driver class 'oracle.jdbc.OracleDriver' because class was not present in my_fat_jar/oracle/jdbc folder.

Comments

0

if you are using IJ simple select the maven option on the right bar then use the refresh arrows, this will be solved . it worked perfectly on my side

Comments

-1

For People who uses Gradle 8.0 and beyond. I had to use

implementation 'mysql:mysql-connector-java:8.0.26'

instead of

implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.26' ,

my application.yml looks like:

spring:
  datasource:
    driverClassName: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/<Your DB name>?useSSL=false

remember to reload your gradle file

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.