我在网上找到的解决方案都不起作用。我一直在打
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链路故障
我正在尝试从另一台服务器连接到MySQL数据库。我可以向你保证,当我尝试使用PgAdmin
软件连接数据库时,url、用户和密码信息是正确的。然而,当我尝试使用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
导入到库中。我已经尝试了所有三种不同的方法getConnection()
getConnection(String url)
(其中我的字符串url="jdbc:mysql://MySQLUrl:3306/MySQL_dbName?user=user&password=密码")getConnection(String url, Properties info)
getConnection(String url, String user, String password)
我还尝试使用以下命令注册驱动程序:
Driver myDriver = new oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver( myDriver );
我找到的解决方案之一是在my.cnf文件中评论"跳过网络"规则。但是,我在服务器中找不到这样的文件。(服务器基于Linux操作系统运行)
非常感谢为解决这一问题提供的协助。提前谢谢。
更新:查看我的答案。我再次为我愚蠢的错误道歉。
我终于知道错误是什么了。这真是个愚蠢的错误。它是一个postgres数据库,而不是mysql。所以,我只是更改url和驱动程序。
String url = "jdbc:postgresql:://MySQLUrl:3306/";
String driver = "org.postgresql.Driver";
很抱歉我的错误,并感谢那些试图帮助我的人。