参见英文答案 > com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 32个
我正在尝试使用jdbc连接MysqL数据库.
这是我的数据库连接类:
public class Database {
String userName = "myUserName";
String password = "myPass";
String url = "jdbc:MysqL://xxx.xxx.xxx.xxx:3306/database_Name?autoReconnect=true&useSSL=false";
public void connect()
{
try {
Class.forName("com.MysqL.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection(url,userName,password);
Statement statement = conn.createStatement();
String queryString = "select * from users";
ResultSet rs = statement.executeQuery(queryString);
while (rs.next()) {
System.out.println(rs.getString(2));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
new Database().connect();
我得到这个例外:
com.MysqL.jdbc.exceptions.jdbc4.MysqLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
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:423)
at com.MysqL.jdbc.Util.handleNewInstance(Util.java:425)
at com.MysqL.jdbc.Util.getInstance(Util.java:408)
at com.MysqL.jdbc.sqlError.createsqlException(sqlError.java:918)
at com.MysqL.jdbc.sqlError.createsqlException(sqlError.java:897)
at com.MysqL.jdbc.sqlError.createsqlException(sqlError.java:886)
at com.MysqL.jdbc.sqlError.createsqlException(sqlError.java:860)
at com.MysqL.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2163)
at com.MysqL.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2088)
at com.MysqL.jdbc.ConnectionImpl.MysqL.jdbc.JDBC4Connection.MysqL.jdbc.Util.handleNewInstance(Util.java:425)
at com.MysqL.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:410)
at com.MysqL.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:328)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at Database.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:423)
at com.MysqL.jdbc.Util.handleNewInstance(Util.java:425)
at com.MysqL.jdbc.sqlError.createCommunicationsException(sqlError.java:989)
at com.MysqL.jdbc.MysqLIO.MysqLIO.java:341)
at com.MysqL.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2251)
at com.MysqL.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2104)
... 19 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.MysqL.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211)
at com.MysqL.jdbc.MysqLIO.MysqLIO.java:300)
... 21 more
* Java部分为localhost工作但是当我尝试这个远程数据库时,它失败了.
*创建了数据库和表.(没有问题.)
那问题出在哪里?
最佳答案