我试图连接到我的远程MySQL数据库,但我失败并得到了这个错误.
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Run Code Online (Sandbox Code Playgroud)
令人困惑的是,当我使用MySQL-Front工具连接远程数据库时,它是有效的,我可以成功ping到IP地址.但是当我使用我的代码时,它会在大约十秒后显示错误.
此外,当我在我的代码中使用了错误的用户名或密码时,它会立即显示错误的验证.这是否证明设置连接是没有问题的?
这是我的代码(它可以在我的localhost数据库上工作):
public static void main(String[] args) {
String url = "jdbc:mysql://(IP address):3306/";
String dbName = "talk";
String driver = "com.mysql.jdbc.Driver";
String userName = "talkroot";
String password = "123456";
try {
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url + dbName,
userName, password);
System.out.println("connect");
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("end");
}
Run Code Online (Sandbox Code Playgroud)
这是错误:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Last packet sent to the server was 0 ms ago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown …Run Code Online (Sandbox Code Playgroud)