com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败 [英] com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
问题描述
我正在努力让我的数据库与我的Java程序交谈。
I'm working on getting my database to talk to my Java programs.
有人可以使用JDBC给我一个快速而脏的示例程序吗?
Can someone give me a quick and dirty sample program using the JDBC?
我收到了一个相当惊人的错误:
I'm getting a rather stupendous error:
Exception in thread "main" 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:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1122)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2260)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:787)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:49)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:357)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:207)
at SqlTest.main(SqlTest.java:22)
Caused by: 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:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1122)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:344)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2181)
... 12 more
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:432)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.<init>(Socket.java:375)
at java.net.Socket.<init>(Socket.java:218)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:293)
... 13 more
测试文件的内容:
import com.mysql.jdbc.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SqlTest {
public static void main(String [] args) throws Exception {
// Class.forName( "com.mysql.jdbc.Driver" ); // do this in init
// // edit the jdbc url
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/projects?user=user1&password=123");
// Statement st = conn.createStatement();
// ResultSet rs = st.executeQuery( "select * from table" );
System.out.println("Connected?");
}
}
推荐答案
所以,你有一个
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败
java。 net.ConnectException:拒绝连接
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
java.net.ConnectException: Connection refused
我引用这个答案还包含一步一步的MySQL + JDBC教程:
I'm quoting from this answer which also contains a step-by-step MySQL+JDBC tutorial:
如果你得到
SQLException:连接被拒绝
或连接超时
或特定于MySQL的CommunicationsException:
,则表示根本无法访问数据库。这可能有以下一个或多个原因:
通讯链接失败
If you get a
SQLException: Connection refused
orConnection timed out
or a MySQL specificCommunicationsException: Communications link failure
, then it means that the DB isn't reachable at all. This can have one or more of the following causes:
- JDBC URL中的IP地址或主机名错误。
- 本地DNS服务器无法识别JDBC URL中的主机名。
- JDBC URL中缺少或错误端口号。
- 数据库服务器
- 数据库服务器不接受TCP / IP连接。
- 数据库服务器连接已用完。
- Java和DB之间的某些东西阻塞了连接,例如防火墙或代理。
- IP address or hostname in JDBC URL is wrong.
- Hostname in JDBC URL is not recognized by local DNS server.
- Port number is missing or wrong in JDBC URL.
- DB server is down.
- DB server doesn't accept TCP/IP connections.
- DB server has run out of connections.
- Something in between Java and DB is blocking connections, e.g. a firewall or proxy.
要解决其中一个问题,请遵循以下建议:
To solve the one or the other, follow the following advices:
- 使用
ping
验证并测试它们。 - 刷新DNS或使用JDBC URL中的IP地址。
- 根据MySQL DB的
my.cnf
验证它。 - 启动数据库。
- 验证mysqld是否在没有
- skip-networking选项
的情况下启动。 - 重新启动数据库并修改代码,以便它关闭
中的连接
。 - 禁用防火墙和/或配置防火墙/代理以允许/转发端口。
- Verify and test them with
ping
. - Refresh DNS or use IP address in JDBC URL instead.
- Verify it based on
my.cnf
of MySQL DB. - Start the DB.
- Verify if mysqld is started without the
--skip-networking option
. - Restart the DB and fix your code accordingly that it closes connections in
finally
. - Disable firewall and/or configure firewall/proxy to allow/forward the port.
参见:
- 如何在基于servlet的应用程序中连接到JDBC数据库/数据源?
- 使用静态java.sql.Connection是否安全多线程系统中的实例?
- How should I connect to JDBC database / datasource in a servlet based application?
- Is it safe to use a static java.sql.Connection instance in a multithreaded system?
See also:
这篇关于com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!