如何使用证书在java中设置MySQL SSL连接?

如何使用证书在java中设置MySQL SSL连接?,java,mysql,database,ssl,jdbc,Java,Mysql,Database,Ssl,Jdbc,我正在尝试使用以下代码设置SSL连接,但由于出现错误,无法连接到数据库服务器: “通信链路故障” 导入java.io.FileReader; 导入java.io.IOException; 导入java.sql.Connection; 导入java.sql.DriverManager; 导入java.sql.ResultSet; 导入java.sql.SQLException; 导入java.sql.SQLNonTransientConnectionException; 导入java.sql.St

我正在尝试使用以下代码设置SSL连接,但由于出现错误,无法连接到数据库服务器:

“通信链路故障”

导入java.io.FileReader;
导入java.io.IOException;
导入java.sql.Connection;
导入java.sql.DriverManager;
导入java.sql.ResultSet;
导入java.sql.SQLException;
导入java.sql.SQLNonTransientConnectionException;
导入java.sql.Statement;
导入java.util.Properties;
公共类GoogleCloudSQL{
公共静态void main(字符串[]args)抛出SQLNonTransientConnectionException、IOException、SQLException{
连接=空;
String url=“jdbc:mysql://:/”+
“?verifyServerCertificate=true”+“&useSSL=true”+“&requireSSL=true”;
setProperty(“javax.net.ssl.keyStore”、“abc.pem”);
setProperty(“javax.net.ssl.keystrepassword”、“def.pem”);
setProperty(“javax.net.ssl.trustStore”、“ghi.pem”);
connection=DriverManager.getConnection(url,“用户名”、“密码”);
try(Statement=connection.createStatement()){
ResultSet ResultSet=statement.executeQuery(“显示表格”);
while(resultSet.next()){
System.out.println(resultSet.getString(1));
}
}捕获(例外e){
e、 printStackTrace();
}
}
}
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLNonTransientConnectionException;
import java.sql.Statement;
import java.util.Properties;

public class GoogleCloudSQL {

    public static void main(String[] args) throws SQLNonTransientConnectionException ,IOException, SQLException {
        Connection connection = null;
        String url = "jdbc:mysql://<host>:<port>/<database>"+
        "?verifyServerCertificate=true"+ "&useSSL=true"+ "&requireSSL=true";
         
        System.setProperty("javax.net.ssl.keyStore","abc.pem");
        System.setProperty("javax.net.ssl.keyStorePassword","def.pem");
        System.setProperty("javax.net.ssl.trustStore","ghi.pem");
         
        connection = DriverManager.getConnection(url,"username", "password");
        
        try (Statement statement = connection.createStatement()) {
          ResultSet resultSet = statement.executeQuery("SHOW TABLES");
          while (resultSet.next()) {
            System.out.println(resultSet.getString(1));
          }
        }catch(Exception e){
          e.printStackTrace();
        }
    }
    
}