jdbc encountered in connection pit MySQL8.0

Before been used MySQL5.5, use jdbc no problem at all, it had to change there is a problem with high version MySQL8.0 up and running

Slowly later developed are certainly a high point, although the former is good, but will slowly be eliminated so take a look at today's problems

 

 

 Control of information to enhance the look, MySQL problem is found, look closely, there is a warning

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.

Roughly meaning an SSL connection can not be established without authenticate the server case

 

SSL connection?

So what is an SSL connection?

That SSL connection using SSL certificate https encryption protocol, allowing users to use the Internet to ensure secure data transmission, guaranteed not to be intercepted and eavesdropped information during transmission. SSL protocol is located between the TCP / IP protocol with a variety of application-layer protocol that provides secure support for data communications.

 

MySQL version 5.6 after doing a security upgrade, it is necessary to validate the SSL connection

Know after this will be easier, we can configure SSL connection or temporarily disable the SSL connection

 

But there is still a problem

Because the new version not only updated the SSL connection, the drive must change

The previous version of the driver I use

com.mysql.jdbc.Driver

Now the new version of the driver for the

com.mysql.cj.jdbc.Driver

You can download the official website, maven, add the following statement

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

Official website driver download address: https://dev.mysql.com/downloads/

Now the corresponding version on it

But the new version in addition to the connection with SSL connection setup parameters also need a time zone, or can not connect

So the last connection is

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();
        }
        
    }
    
}

have a test

 

 

 

 ok no problem

Guess you like

Origin www.cnblogs.com/Sunboy910/p/12579835.html