我创建了一个简单的类来测试与本地主机数据库的通信,该数据库是我使用Mysql Workbench创建的。 Mysql服务器正在运行。 JDBC驱动程序已添加到我的项目的类路径中。
public class Database
{
public static void main(String[] args)
throws SQLException, ClassNotFoundException
{
Connection connection = null;
String serverName = "localhost:3306";
String databaseName = "detector_tests";
String url = "jdbc:mysql://" + serverName + "/" + databaseName
+ "?useSSL=TRUE";
String username = "simon";
String password = "password123";
connection = DriverManager.getConnection(url, username, password);
}
}
运行程序时,我遇到了以下异常: “main”线程中的异常com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure,表示通信链路失败。最近一次成功发送到服务器的数据包是0毫秒前发送的。驱动程序没有接收到来自服务器的任何数据包。原因是:com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure,最近一次成功发送到服务器的数据包是0毫秒前发送的,驱动程序没有接收到来自服务器的任何数据包。Caused by: javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Path does not chain with any of the trust anchors,表示SSL握手失败,证书验证不通过。路径与受信任锚点的任何链接不匹配。
看起来我需要添加某种证书或类似的东西。因为我对数据库还比较新,所以不太确定。有没有和我有相同经验或者解决方案的人?如果需要更多信息,我很乐意编辑帖子。
提前感谢!