連接MySQL提示Establishing SSL connection問題

連接mysql數據庫時,通常的做法是在類的static塊中寫連接數據庫的部分。如下圖:

static {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
        statement = (Statement) conn.createStatement();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

我使用的mysql是8.0,mysql-connector-java.jar版本是5.1.46,如果直接運行,程序不會報錯,一樣執行成功,但會有如下的提示:

WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

大概意思就是在mysql更高版本中,需要使用useSSL建立連接,如果不需要用到可以將useSSL改爲false。

方法就是在原有連接字符串加上useSSL=false。

jdbc:mysql://localhost:3306/test

替換爲

jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&useSSL=false

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章