我正在使用eclipse,这是我的代码,以便连接到远程mysql服务器:
package test;
import java.sql.*;
public class test{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://host:port/database?useSSL=true","username","password");
}catch(Exception e){ System.out.println(e);}
}
}
我经常会遇到这样的错误:
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
我使用mysql workbench,并且能够连接到服务器。我的mysql驱动程序是:
mysql-connector-java-8.0.22
java version is : 1.8.0_271
远程数据库不是一个实际的mysql数据库,但是我使用mongodbatlas中的connectorforbi。有没有办法知道使用哪个mysql connectorjava?
1条答案
按热度按时间hc2pp10m1#
您是否为java应用程序尝试了mysql连接器依赖项jar?
如果您的项目是基于maven的,您可以从这里直接将此依赖项添加到pom.xml文件中https://mvnrepository.com/artifact/mysql/mysql-connector-java.