com.mysql.cj.jdbc.Driver 警告

今天跑 shenyu 項(xiàng)目的時候出現(xiàn)一個十分顯眼的報錯

2022-05-03 21:55:08 [main] INFO  org.apache.shenyu.admin.spring.LocalDataSourceLoader - execute shenyu schema sql: sql-script/mysql/schema.sql
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.
2022-05-03 21:55:09 [main] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...

著重拎出來看看

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.

大概意思就是警告你要換個類名,但是項(xiàng)目代碼運(yùn)行并沒有錯誤
我這里 mysql-server 版本是 8.0.29, mysql-connector-java 版本是 8.0.16


解決這個報錯的問題很簡單就是替換一下配置的 driver class 名字

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/shenyu25?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
    username: root
    password: febsA123
    #替換前   driver-class-name: com.mysql.jdbc.Driver
    driver-class-name: com.mysql.cj.jdbc.Driver

好奇查下原因
官網(wǎng)文檔在 8.0 的連接介紹里確實(shí)有介紹要這么用
https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-usagenotes-connect-drivermanager.html

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

// Notice, do not import com.mysql.cj.jdbc.*
// or you will have problems!

public class LoadDriver {
    public static void main(String[] args) {
        try {
            // The newInstance() call is a work around for some
            // broken Java implementations

            Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
        } catch (Exception ex) {
            // handle the error
        }
    }
}

切換到 5.1 的頁面名字就不一樣了
https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-usagenotes-connect-drivermanager.html

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

// Notice, do not import com.mysql.jdbc.*
// or you will have problems!

public class LoadDriver {
    public static void main(String[] args) {
        try {
            // The newInstance() call is a work around for some
            // broken Java implementations

            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch (Exception ex) {
            // handle the error
        }
    }
}

翻了下依賴包下面的源碼就發(fā)現(xiàn)警告信息出處了


Driver.java

這里也可以看到為了兼容原來的使用方式把
com.mysql.jdbc.Driver 實(shí)現(xiàn)成了 com.mysql.cj.jdbc.Driver 的子類

好家伙

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容