如何解决java mysql中Establishing SSL connection 的错误?

新手上路,请多包涵

`package sqltest;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class testuser {

private static String url = "jdbc:mysql://localhost:3306/terminaldb?useSSL=false";
private static String user = "testuser";
private static String password = "testpassword";
private static String driver = "com.mysql.jdbc.Driver";
private static int id = 0;
private static String hostname = "HP";

public static void main(String[] args){
    Getconn get = new Getconn();
    Connection conn = get.getconn();
    PreparedStatement ps = null;
    
    id++;
    String insertSql = "insert into filesyslog (id,hostname,operation,date) values(?,?,?,?)";
    try{
        ps = conn.prepareStatement(insertSql);            
        ps.setInt(1, 12345);
        ps.setString(2, hostname);
        ps.setString(3, "abcd");
        ps.setString(4, "date now date");        
        ps.executeQuery();
    }
    catch(Exception e){
        
    }
}

}
`
我已经添加了?useSSL=false,并且已经检查MySQL中ssl为disabled
但是仍然有如下提示

Sun Dec 01 10:59:03 GMT+08:00 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.
阅读 3.3k
2 个回答
✓ 已被采纳新手上路,请多包涵

java.beans.Statement ps = null;

    Connection conn;
    PreparedStatement stmt;
    id++;
    String insertSql = "insert into filesyslog (id,hostname,operation,date) values(?,?,?,?)";
    try {
        Class.forName(driver);
        conn = DriverManager.getConnection(url, user, password);
        stmt = (PreparedStatement) conn.prepareStatement(insertSql);
        stmt.setInt(1, id);
        stmt.setString(2, "HP");
        stmt.setString(3, args[0]);
        stmt.setString(4, DateNowStr);
        stmt.executeUpdate();
        
    } catch (ClassNotFoundException e) {
        // TODO 自动生成的 catch 块
        e.printStackTrace();
    } catch (SQLException e) {
        // TODO 自动生成的 catch 块
        e.printStackTrace();
    }

你的url没有在创建链接时候用到吧? 看看 Getconn();是怎么实现的。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题