如何停用在 Java 中連線到 MySQL 資料庫時出現的“不建議在未驗證伺服器身份的情況下建立 SSL 連線”警告?
若要在連線到 Java 中的資料庫時停用警告,請使用以下概念 −
autoReconnect=true&useSSL=false
完整語法如下 −
yourJdbcURL="jdbc:mysql://:yourPortNumber/yourDatabaseName?autoReconnect=true&useSSL=false";
如果您不包含 “useSSL=false”,則會出現警告訊息 −
Wed Feb 06 18:53:39 IST 2019 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 警告,請使用開頭提到的語法。
Java 程式碼如下 −
import java.sql.Connection; import java.sql.DriverManager; public class AvoidSQLWarnDemo { public static void main(String[] args) { String JdbcURL = "jdbc:mysql://:3306/mybusiness?" + "autoReconnect=true&useSSL=false"; String Username = "root"; String password = "123456"; Connection con = null; try { con = DriverManager.getConnection(JdbcURL, Username, password); System.out.println("Your JDBC URL is as follows:" + JdbcURL); } catch (Exception exec) { exec.printStackTrace(); } } }
在執行上述 Java 程式後,您將不會收到警告。但是,您將獲得以下輸出 −
廣告