连接MySQL提示Establishing SSL connection问题

连接mysql数据库时,通常的做法是在类的static块中写连接数据库的部分。如下图:

复制代码
static {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
        statement = (Statement) conn.createStatement();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
复制代码

我使用的mysql是8.0,mysql-connector-java.jar版本是5.1.46,如果直接运行,程序不会报错,一样执行成功,但会有如下的提示:

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更高版本中,需要使用useSSL建立连接,如果不需要用到可以将useSSL改为false。

方法就是在原有连接字符串加上useSSL=false。

jdbc:mysql://localhost:3306/test

替换为

jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&useSSL=false

—————————————————————————————————————————————

添加mysql驱动包过程

1. 在idea中project里建立lib文件夹,并将mysql-connector-java-5.1.46.jar包拷贝到lib里

2. 点击菜单栏File-Project Structure-Modules-Dependencies-Add(+) 选择lib里的mysql驱动jar包添加即可

posted @   guwei4037  阅读(791)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示
主题色彩