JAVA连接MySQL数据库的警告处理以及可能的错误

JAVA连接MySQL数据库的警告处理以及可能的错误

IDEA控制台报出的红色警告

Mon Dec 21 20:43:40 CST 2020 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.

翻译:12月21日星期一20:43:40 CST 2020警告:不建议在没有服务器身份验证的情况下建立SSL连接。根据MySQL5.5.45+、5.6.26+和5.7.6+的要求,如果不设置显式选项,则默认必须建立SSL连接。为了符合不使用SSL的现有应用程序,verifyServerCertificate属性设置为“false”。您需要通过设置useSSL=false显式禁用SSL,或者设置useSSL=true并提供用于服务器证书验证的信任库。

处理过程

​ 在接口Connection的数据库名称url后添加设置useSSL=true

  • 处理前

    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb1","root","097052sf");
    
  • 处理后

    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb1?useSSL=true","root","097052sf");
    

可能犯的错误

  • 数据库用户表的创建

    USE mydb1;
    CREATE TABLE users(
           id INT PRIMARY KEY AUTO_INCREMENT,
           username VARCHAR(20) UNIQUE NOT NULL,
           PASSWORD VARCHAR(20) NOT NULL,
           phone VARCHAR(11) NOT NULL
    )CHARSET=utf8;
    INSERT INTO users (username,PASSWORD,phone) VALUES ('张三','1234','12345678901');
    INSERT INTO users (username,PASSWORD,phone) VALUES('李四','123','01234567899');
    INSERT INTO users(username,PASSWORD,phone) VALUES ('zs','1','12345678901');
    
  • IDEA连接数据库查询时

    请输入用户名:
    zs
    请输入密码:
    1
    登陆成功!
    
    Process finished with exit code 0
    
    请输入用户名:
    张三
    请输入密码:
    1234
    用户名或密码错误!
    
    Process finished with exit code 0
    

    由此可见,当输入用户名使用英文字符的时候符合预期结果,使用中文字符的时候,不符合预期结果。

    究其原因,应该是代码中缺少字符编码设置,在JAVA的Connection接口的数据库名称后面再加上和数据库设置一样的字符编码characterEncoding=utf8

    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb1?useSSL=true&characterEncoding=utf8","root","097052sf");
    

    运行结果

    请输入用户名:
    张三
    请输入密码:
    1234
    登陆成功!
    
    Process finished with exit code 0
    
posted @ 2020-12-21 21:12  某人很酷  阅读(308)  评论(0)    收藏  举报