The database exception Cannot load driver class: com.mysql.jdbc.Driver occurs if there is a disparity between mysql driver class, mysql driver jar version and mysql database version. The java application attempts to load a driver class from the mysql database driver jar. If the driver class is unable to load, the exception Cannot load driver class: com.mysql.jdbc.Driver will be thrown.
The database driver class loads the mysql database driver from the driver’s jar. The driver establishes a connection to the mysql database and runs the database sql queries. In order to execute sql queries in a Java application, the driver class must be loaded.
Exception
The following stack trace will be shown if the database driver exception Cannot load driver class: com.mysql.jdbc.Driver is thrown. There is an another version of the exception. Cannot load driver class: com.mysql.cj.jdbc.Driver
Caused by: java.lang.IllegalStateException: Cannot load driver class: com.mysql.jdbc.Driver
at org.springframework.util.Assert.state(Assert.java:97) ~[spring-core-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineDriverClassName(DataSourceProperties.java:223) ~[spring-boot-autoconfigure-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.initializeDataSourceBuilder(DataSourceProperties.java:175) ~[spring-boot-autoconfigure-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.createDataSource(DataSourceConfiguration.java:43) ~[spring-boot-autoconfigure-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari.dataSource(DataSourceConfiguration.java:85) ~[spring-boot-autoconfigure-2.3.4.RELEASE.jar:2.3.4.RELEASE]
Root Cause
The driver load exception Cannot load driver class: com.mysql.jdbc.Driver and Cannot load driver class: com.mysql.cj.jdbc.Driver occurs if there are mismatch in mysql database driver class name, mysql database driver jar version and mysql database version. The mismatch will fails to load the configured driver class in the java application. This exception will be resolved if all three is configured correctly
Solution 1
If you are using the java version 1.8 and above and the mysql version 5.6 and above, the upgraded database driver jar should be used to connect mysql database from java. The driver class name is com.mysql.cj.jdbc.Driver. The database credentials are as like below.
spring.datasource.url=jdbc:mysql:driver://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
Solution 2
If you are using the java version 1.8 and above and the mysql version 5.6 and above, the upgraded database driver jar should be used to connect mysql database from java. The maven dependency must be 8.0.*. If the maven dependency is configured with lower version, it needs to be upgraded to latest mysql database driver version.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
Solution 3
If you are using a legacy application that uses the java version 1.7 and below, or mysql database version 5.5 and below, the older database driver class should be used to connect mysql database from java. The driver class name is com.mysql.jdbc.Driver. The database credentials are as like below.
spring.datasource.url=jdbc:mysql:driver://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
Solution 4
If you are using a legacy application that uses the java version 1.7 and below, or mysql database version 5.5 and below, the older database driver class should be used to connect mysql database from java. The older mysql database driver jar should be used. The last version of the older mysql database driver version is 5.1.48.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
Solution 5
The driver name could be configured wrongly in the database url. verify the datasource.driver-class name in the configuration. If the driver name is incorrectly configured, then the exception will be thrown. Please check the line below.
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.driver-class-name=com.mysql.jdbc.Driver