Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

ปิด Warning SSL Connection ใน Java JDBC MySQL Connection

เพิ่มพารามิเตอร์ useSSL=false และ autoReconnect=true ใน JDBC URL เพื่อปิด Warning การเชื่อมต่อ SSL

ไวยากรณ์

jdbc:mysql://localhost:port/databaseName?autoReconnect=true&useSSL=false

ข้อความ Warning ที่จะเกิดหากไม่ตั้งค่า

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...

โค้ด Java ตัวอย่าง

import java.sql.Connection;
import java.sql.DriverManager;

public class AvoidSQLWarnDemo {
    public static void main(String[] args) {
        String JdbcURL = "jdbc:mysql://localhost:3306/mybusiness?" + "autoReconnect=true&useSSL=false";
        String Username = "root";
        String password = "123456";
        Connection con = null;
        try {
            con = DriverManager.getConnection(JdbcURL, Username, password);
            System.out.println("Your JDBC URL is as follows: " + JdbcURL);
        } catch (Exception exec) {
            exec.printStackTrace();
        }
    }
}

หลังรันโปรแกรม จะไม่ได้ Warning อีกต่อไป

ตัวเลือกอื่นๆ

  • useSSL=true พร้อม truststore สำหรับการ verify certificate
  • verifyServerCertificate=false (deprecated)
  • MySQL 8.0+ ใช้ sslMode=DISABLED แทน