如何在Java中连接到SQL数据库时禁用WARN

时间:2016-04-20 23:03:56

标签: java mysql ssl

在Java应用程序编程中,当我要求程序从SQL数据库中检索数据时,它会在控制台中显示:

Wed Apr 20 16:57:58 MDT 2016 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.

我知道要阅读很多,并且我在SQL工作台中禁用了SSL,所以我无法弄清楚如何摆脱警告或者我是否应该关注它?提前致谢。

3 个答案:

答案 0 :(得分:20)

尝试在连接字符串

中使用useSSL = false

示例:

  

JDBC:MySQL的:// XXXXXXX:3306 / XXXX autoReconnect的=真安培; useSSL =假

答案 1 :(得分:1)

替换 - jdbc:mysql:// localhost:3306 / demo

使用 - jdbc:mysql:// localhost:3306 / demo?useSSL = false

答案 2 :(得分:0)

如果有人使用 MysqlDataSource ,这比DriverManager更受欢迎,那么你可以使用setUseSSL(boolean sslstate)函数:

MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setUser("USER_NAME");
dataSource.setPassword("PASSWORK");
dataSource.setServerName("localhost"); //most likely
dataSource.setDatabaseName("DATABASE_NAME");
dataSource.setUseSSL(false); //Disable SSL
try(
    Connection conn = dataSource.getConnection();
)
{
    Statement stmt = conn.createStatement();
    stmt.executeUpdate("INSERT INTO TABLE_NAME ...");
} catch (SQLException e)
{
    e.printStackTrace();
}