我正在开发一个Java应用程序(其中包括)查询远程数据库。我在连接数据库时遇到问题。 我的代码是:
private static String MYSQL_URL = "jdbc:mysql://db644874220.db.1and1.com";
private static String DB_NAME = "db644874220";
private static Connection CONNECTION;
static {
String dbUrl = MYSQL_URL + "/" + DB_NAME;
try {
Class.forName("com.mysql.jdbc.Driver");
CONNECTION = DriverManager.getConnection(dbUrl, MYSQL_USERNAME, MYSQL_PASSWORD);
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
}
我得到的错误是:
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(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:988)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:341)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2251)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2284)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2083)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:806)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:410)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:328)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.joshuaevanslowell.IO.<clinit>(IO.java:41)
at com.joshuaevanslowell.Speculator.<init>(Speculator.java:46)
at com.joshuaevanslowell.Speculator.main(Speculator.java:32)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:300)
... 17 more
我已经看到了其他一些与此相关的问题,但他们通常认为数据库位于localhost上,我的不是。我使用1&amp; 1网络托管,我可以通过浏览器通过phpMyAdmin使用数据库。它提供的有关服务器的信息是
我觉得我做了一件非常糟糕的事情,而我却无法做出这样的头脑或尾巴。
答案 0 :(得分:0)
如果您安装了telnet客户端客户端,假设3306是mysql端口,请尝试
telnet ipaddress 3306
。
我担心你可能没有直接连接到mysql服务器。要启用此功能,您可能需要在类似问题中编辑配置。
另外,请关闭数据库连接,或者使用连接池。打开过多的连接(超出配置的限制)可能会导致类似的问题。
答案 1 :(得分:-1)
我的猜测是,从运行Java代码的位置不知道db644874220.db.1and1.com主机
尝试从运行Java代码的计算机ping db644874220.db.1and1.com
执行telnet db644874220.db.1and1.com 3306
。如果两者都没有返回结果,则意味着您无法通过其名称访问该计算机。
如果您知道它或在主机文件中添加一行
,请使用IP[IP address] db644874220.db.1and1.com