Loading class `com.mysql.jdbc.Driver'. This is

2020-05-19 02:08发布

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 iscom.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

标签: mysql jdbc
19条回答
你好瞎i
2楼-- · 2020-05-19 02:33

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

查看更多
叼着烟拽天下
3楼-- · 2020-05-19 02:33

The driver is automatically registered via SPI and manual loading of the driver class is usually unnecessary. Just change "com.mysql.cj.jdbc.Driver"

查看更多
ゆ 、 Hurt°
4楼-- · 2020-05-19 02:35

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 -> Difference between SPI and API?.

查看更多
Deceive 欺骗
5楼-- · 2020-05-19 02:37

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"/>

查看更多
手持菜刀,她持情操
6楼-- · 2020-05-19 02:38

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

查看更多
smile是对你的礼貌
7楼-- · 2020-05-19 02:39

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.

查看更多
登录 后发表回答