Cannot load driver class: com.mysql.jdbc.Driver Spring

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

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>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.25</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


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


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>

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