我在网上找到的解决方案都没有。 我一直在打
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通讯链接失败
我正在尝试从另一台服务器连接MySQL数据库。
我可以向您保证,当我尝试使用PgAdmin
软件连接数据库时,网址,用户和密码信息是正确的。但是,当我尝试使用Java代码执行时(通过NetBean 8.1),它失败了。
好的,以下是我的代码:
import oracle.jdbc.pool.OracleDataSource;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Client9{
public static void main(String[] args) {
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://MySQLUrl:3306/";
String dbName = "MySQL_dbName";
String driver = "com.mysql.jdbc.Driver";
String userName = "user";
String password = "password";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}
}
}
错误信息如下所示:
run:
MySQL Connect Example.
com.mysql.jdbc.exceptions.jdbc4.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.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1117)
at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:673)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1084)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2465)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2498)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at netiqcustomprov.Client9.main(Client9.java:35)
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:3052)
at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:597)
... 16 more
BUILD SUCCESSFUL (total time: 1 minute 0 seconds)
到目前为止我尝试过的解决方案:
我已将ojdb14.jar
和mysql-connector-java-5.1.23-bin.jar
导入到库中。
我尝试了所有3种不同的方法getConnection()
getConnection(String url)
getConnection(String url, Properties info)
getConnection(String url, String user, String password)
我还尝试使用以下命令注册驱动程序:
Driver myDriver = new oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver( myDriver );
我找到的解决方案之一是评论&#34; skip-networking&#34; my.cnf文件中的规则。但是,我在服务器中找不到这样的文件。 (服务器基于Linux OS运行)
非常感谢协助解决这个问题。 提前致谢
更新:查看我的回答。我再一次为我的愚蠢错误道歉。
答案 0 :(得分:-1)
最后我知道错误是什么。这真是一个愚蠢的错误。 这是一个postgres db而不是mysql。 所以,我只是更改了网址和驱动程序。
String url = "jdbc:postgresql:://MySQLUrl:3306/";
String driver = "org.postgresql.Driver";
对不起我的错误,感谢那些试图帮助我的人。