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沒問題


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM