Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

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

im getting this error even with the driver-class-name difined

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

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

<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.0.8</version> </dependency> already have this on mine too – voidcurser Oct 14, 2015 at 11:28 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> – aJaY May 12 at 6:41

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

Yes, but its deprecated. For more details you can check below link dev.mysql.com/doc/connector-j/8.0/en/… – Brijesh Feb 7, 2019 at 8:25
  • If you are under proxy make sure use VPN to connect to internal servers, iif you are accessing DEV/STG servers

  • 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>
    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.

    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:

  • Download desired jar from maven repository
  • Open your project in Intellij IDEA
  • File -> Project Structure -> Libraries
  • Click New Project Library (green plus sign on the left), or press Alt + Insert keys
  • Select Java
  • Select your jar
  • Press OK
  • Should look something like this:

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

    Tested with IntelliJ IDEA 2017.3.4.

  • 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.
  • 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 – stoneshishang Sep 29, 2021 at 21:34

    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>
    

    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>
    

    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!!!

    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. – Community Jul 6, 2022 at 17:14 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 – Japhei Jul 9, 2022 at 19:43

    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

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.

  •