jdbc连接MySQL8.0遇到的坑

之前一直用MySQL5.5,使用jdbc一点问题都没有,后来换用高版本MySQL8.0运行起来就有问题了

慢慢以后开发肯定都是用高级一点的,以前的虽然好,但是慢慢也会被淘汰所以来看看今天的问题

 看控制的提升信息,发现是MySQL的问题,仔细一看有一句警告

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.

大致意思就是 不能在没有验证服务器身份的情况下建立SSL连接

SSL连接?

那么什么是SSL连接?

SSL连接也就是说,利用SSL证书进行https加密协议,使用户在使用Internet确保数据传输的安全,保证在信息传输过程中不会被截取及窃听。 SSL协议位于TCP/IP协议与各种应用层协议之间,为数据通讯提供安全支持。

MySQL在5.6版本后做了安全升级,所以需要验证SSL连接

知道这个之后就好办了,我们可以配置SSL连接方式或者暂时禁用SSL连接

但是还是有问题

因为新版本不只是更新了SSL连接,驱动也要换

以前版本驱动我用的是

com.mysql.jdbc.Driver

现在新版本驱动为

com.mysql.cj.jdbc.Driver

可以在官网下载,maven添加下列语句

    <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
         </dependency>

官网驱动下载地址:https://dev.mysql.com/downloads/

现在对应版本就可以了

但是新版本连接除了要带SSL连接设置还需要一个时区的参数,不然无法连接

所以最后的连接就是

package com.test.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

public class DButil {

    private static Connection conn;
    private static Statement stmt;
    private static PreparedStatement pstmt;
    
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public static Connection getConnection() {
        try {
            conn = DriverManager.getConnection("jdbc:mysql://39.106.189.250:3306/javaweb?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true", "root", "*********");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return conn;
    }
    
    public static Statement getStatement() {
        Connection conn = getConnection();
        try {
            if(conn != null) {
                stmt = conn.createStatement();
            }
            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return stmt;
    }
    
    public static PreparedStatement getPreparedStatement(String sql) {
        Connection conn = getConnection();
        try {
            if(conn != null) {
                pstmt = conn.prepareStatement(sql);
            }
        }catch(SQLException e) {
            e.printStackTrace();
        }
        return pstmt;
    }
    
    public static void closeDBResources() {
        try {
            if(pstmt != null && !pstmt.isClosed()) {
                pstmt.close();
            }
            if(stmt != null && !stmt.isClosed()) {
                stmt.close();
            }
            if(conn != null && !conn.isClosed()) {
                conn.close();
            }
        }catch(SQLException e) {
            e.printStackTrace();
        }
        
    }
    
}

测试一下

 

 ok没问题

猜你喜欢

转载自www.cnblogs.com/Sunboy910/p/12579835.html