Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'
MysqlJdbcMysql Problem Overview
This is the Warning im getting in console, Im confused with this warning:
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.
Mysql Solutions
Solution 1 - Mysql
I resolved this problem by change application.properties
of
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
to
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
Hope it help
Solution 2 - Mysql
According Changes in the Connector/J API "The name of the class that implements java.sql.Driver in MySQL Connector/J has changed from com.mysql.jdbc.Driver to com.mysql.cj.jdbc.Driver. The old class name has been deprecated."
This means that you just need to change the name of the driver:
Class.forName("com.mysql.jdbc.Driver");
to
Class.forName("com.mysql.cj.jdbc.Driver");
Solution 3 - Mysql
If you're using Hibernate then change in your "hibernate.cfg.xml" the following:
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
To:
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
That should do :)
Solution 4 - Mysql
Change driver property in your ORM config file from
<property name="driver" value="com.mysql.jdbc.Driver"/>
to
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
This will resolve the warning :-)
Solution 5 - Mysql
The sentence "Loading class 'com.mysql.jdbc.Driver'. This is deprecated. The new driver class is 'com.mysql.cj.jdbc.Driver' " is clear. You should use the newer driver, like this:
Class.forName("com.mysql.cj.jdbc.Driver");
And in mysql-connector-java-8.0.17. You would find that Class com.mysql.jdbc.Driver doesn't provide service any more. (You also can found the warning came from here.)
public class Driver extends com.mysql.cj.jdbc.Driver {
public Driver() throws SQLException {
}
static {
System.err.println("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.");
}
}
The sentence 'The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.' It mean that write code like this is ok:
//Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/world?useSSL=false&serverTimezone=Asia/Shanghai","root","root");
Due to SPI, driver is automatically registered. How does it work? You can find this from java.sql.DriverManager:
private static void ensureDriversInitialized() {
...
ServiceLoader<Driver> loadedDrivers = ServiceLoader.load(Driver.class);
...
}
And in your mysql-connector-java-XXX.jar, you also can find the file 'java.sql.Driver' in the META-INF\services. The file as follows:
com.mysql.cj.jdbc.Driver
When you run DriverManager.getConnection(), the static block also start running. So driver can be automatically registered with file 'java.sql.Driver'.
And more about SPI -> https://stackoverflow.com/questions/2954372/difference-between-spi-and-api.
Solution 6 - Mysql
This is beacuse the version of mysql to be connected is lower than the version of the mysql driver. Many people say that com.mysql.jdbc.Driver is changed to com.mysql.cj.jdbc.Driver , although this does not solve the problem, but it should also attract attention.
Solution 7 - Mysql
Please remove existing spring.datasource.driver-class-name property in the application.properties file and add the below property.
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
Solution 8 - Mysql
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
Solution 9 - Mysql
Changed my application.conf file as below. It solved the problem.
Before Change:
slick {
dbs {
default {
profile = "slick.jdbc.MySQLProfile$"
db {
driver = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://localhost:3306/test"
user = "root"
password = "root"
}
}
}
}
After Change:
slick {
dbs {
default {
profile = "slick.jdbc.MySQLProfile$"
db {
**driver = "com.mysql.cj.jdbc.Driver"**
url = "jdbc:mysql://localhost:3306/test"
user = "root"
password = "root"
}
}
}
}
Solution 10 - Mysql
in my case, I had a line Class.forName("com.mysql.jdbc.Driver"); after removing this line code works fine if you have any line for loading "com.mysql.jdbc.Driver" remove it, it doesn't require any more
Solution 11 - Mysql
By changing the driver name from "com.mysql.jdbc.Driver" to "com.mysql.cj.jdbc.Driver" will solve this problem.
In case of simple JDBC connection :
Class.forName("com.mysql.cj.jdbc.Driver");
In case of hibernate :
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
Solution 12 - Mysql
I'm using Eclipse and defined MySql connection pool in META_INF/context.xml. Part of its content is:
<Context>
<Resource name="..."
driverClassName="com.mysql.jdbc.Driver"
... />
</Context>
When I changed the line starting with "driverClassName" as follows, problem is gone.
driverClassName="com.mysql.cj.jdbc.Driver"
Solution 13 - Mysql
I only changed this line of code Class.forName("com.mysql.jdbc.Driver");
to Class.forName("com.mysql.cj.jdbc.Driver");
. I see some other people going to application.properties and adding a few things. For me this is the best and fastest way but I also respect other people's answer.
Solution 14 - Mysql
In case of using a configuration based on a YML file, the following will be the property that needs to be adjusted inside the given file:
*driverClassName: com.mysql.cj.jdbc.Driver*
Solution 15 - Mysql
The driver is automatically registered via SPI and manual loading of the driver class is usually unnecessary. Just change "com.mysql.cj.jdbc.Driver"
Solution 16 - Mysql
This warning also appears if you are using the log4jdbc-spring-boot-starter library directly.
However there is a config to choose the correct driver yourself. Put this in your application.properties
:
log4jdbc.drivers=com.mysql.cj.jdbc.Driver
log4jdbc.auto.load.popular.drivers=false
See documentation on Github
Solution 17 - Mysql
If seeing this message in Hive with new MySQL connector 8.x
(MySQL metastore)
open hive-site.xml
and change:
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.jdbc.Driver</value>
<description>MySQL JDBC driver class</description>
</property>
to
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.cj.jdbc.Driver</value>
<description>MySQL JDBC driver class</description>
</property>
Solution 18 - Mysql
my solution: org.springframework.boot 2.0.5.RELEASE
Rather: org.springframework.boot 2.1.0.RELEASE
Solution 19 - Mysql
in my experience. I was using jsp for web. at that time I use mysql 5 and mysql connecter jar 8. So due to the version problem I face this kind of problem. I solve by replace the mysql connector jar file the exact version mysql.
Solution 20 - Mysql
working example:
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/your_db_name?autoReconnect=true&useSSL=false", "root", "root");
call like this it will work.
Solution 21 - Mysql
There important changes to the Connector/J API going from version 5.1 to 8.0. You might need to adjust your API calls accordingly if the version you are using falls above 5.1.
please visit MySQL on the following link for more information https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-api-changes.html
Solution 22 - Mysql
just delete this part Class.forName("com.mysql.jdbc.Driver")
from your code
because the machine is throwing a warning that
> The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary."
meaning that no need to include it beacuse the driver is automatically registered for you by default.
Solution 23 - Mysql
If you have this in your application.properties
:
spring.datasource.driverClassName=com.mysql.jdbc.Driver
,
you can get rid of the error by removing that line.
Solution 24 - Mysql
Now you database connection create according to the this format.
public void Connect() {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/JavaCRUD","root","");
}catch(ClassNotFoundException ex) {
} catch (SQLException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
}
Edit this code like this.
public void Connect() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/JavaCRUD","root","");
}catch(ClassNotFoundException ex) {
} catch (SQLException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
}
Now it will execute.
Solution 25 - Mysql
Class.forName("com.mysql.jdbc.Driver"); //it's old the mysql driver
changes old to new driver -------------------------
Class.forName("com.mysql.cj.jdbc.Driver"); //it's new the mysql driver and paste same place and resolved your problem