The Java exception Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class is
com.mysql.cj.jdbc.Driver’. occurs because the deprecated mysql driver class is configured or used in the mysql database connection. This exception will be shown in framework such as spring, spring boot, hibernate etc.
The MySQL JDBC driver class is updated after version 5.x.x of mysql. If the older version of the driver class is configured and the new MySQL JDBC driver jar is used in the Java class path, Java could not load the older version of the driver class. This is why the exception will be thrown.
If the MySQL JDBC driver class is upgraded to the latest version in the Java application, the exception will be resolved. If the older version of the mysql database is used, the MySQL JDBC driver jar must be downgraded.
Exception
The complete exception error message will be as shown below.
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
How to reproduce the exception
If the older version of the MySQL JDBC driver class is used in the Java application and the latest version of the MySQL JDBC driver jar is used in the Java class path, the Java driver class can not be loaded. In this scenario, the exception Loading class `com.mysql.jdbc.Driver’. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver will be thrown.
package com.yawintutor;
import java.sql.Connection;
import java.sql.DriverManager;
public class DBConnection {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/testdb";
String username = "root";
String password = "root";
Connection con = DriverManager.getConnection(url, username, password);
if (con != null) {
System.out.println("Database Connected successfully");
} else {
System.out.println("Database Connection failed");
}
}
}
Output
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
Exception in thread "main" java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:197)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at com.yawintutor.DBConnection.main(DBConnection.java:13)
Solution 1 – Java
If the java program throws the exception, modify the driver class name in the class.forName() method as shown below. The newest MySQL JDBC driver class name “com.mysql.cj.jdbc.Driver” should be added to the method instead of older driver class name “com.mysql.jdbc.Driver”.
package com.yawintutor;
import java.sql.Connection;
import java.sql.DriverManager;
public class DBConnection {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/testdb";
String username = "root";
String password = "root";
Connection con = DriverManager.getConnection(url, username, password);
if (con != null) {
System.out.println("Database Connected successfully");
} else {
System.out.println("Database Connection failed");
}
}
}
Output
Database Connected successfully
Solution 2 – Spring Boot
The spring boot application will show the exception as a warning message. The MySQL JDBC driver class can be changed in the application.properties file as shown below.
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
to
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
Solution 3 – Hibernate
If the java application uses the hibernate configuration to configure the mysql database JDBCexample driver class, change the configuration as like below
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
to
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
Solution 4 – ORM
if you are using ORM tools other than hibernate, then use the below configuration in the respective configuration files.
<property name="driver" value="com.mysql.jdbc.Driver"/>
to
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
Solution 5 – Older Mysql Database
If you are using the older version mysql database in your application, then the latest driver class will not be required. The older version of MySQL JDBC driver jar must be added in the java class path. The older version MySQL JDBC driver jar can be downloaded from https://dev.mysql.com/downloads/connector/j/. The name of mysql driver jar is same as mysql-connector-java-5.x.x.jar. Add the jar to the java class path.
download the jar mysql-connector-java-5.x.x.jar from https://dev.mysql.com/downloads/connector/j/
(
goto https://dev.mysql.com/downloads/connector/j/
select "Select Operating System:" as your operating system and install
or
select "Select Operating System:" as "Platform Independent", download the zip and extract
)
Add in your java project.
In eclipse
Right click in mysql-connector-java-5.x.x.jar -> Build Path -> Add to Build Path