MySQL 如何在 Java 中连接 MySQL 数据库时禁用“建立 SSL 连接而不进行服务器身份验证不推荐”的警告
要在 Java 中连接数据库时禁用警告,请使用以下概念:
autoReconnect=true&useSSL=false
完整的语法如下:
yourJdbcURL="jdbc:mysql://localhost: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://localhost: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 程序后,将不会得到警告。但是,你将得到以下输出:
阅读更多:MySQL 教程