- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在努力让我的数据库与我的 Java 程序对话。
有人能给我一个使用 JDBC 的快速但肮脏的示例程序吗?
我遇到了一个相当惊人的错误:
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: Communications link failure
java.net.ConnectException: Connection refused
我引用的是this answer其中还包含 MySQL+JDBC 分步教程:
If you get a
SQLException: Connection refused
orConnection timed out
or a MySQL specificCommunicationsException:
, then it means that the DB isn't reachable at all. This can have one or more of the following causes:
Communications link failure
- 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:
- 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.
关于java - com.mysql.jdbc.exceptions.jdbc4.CommunicationsException : Communications link failure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35061366/
在 CruiseControl 版本 1.6.7981.1 中,我从事件查看器中收到以下错误,导致 ccservice 停止。到目前为止,我的解决方法是登录构建框并手动启动 ccservice。看起来
日志消息全文: 日志消息:获取新用户时发生错误 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:从服务器成功接收的最后一个数据包是在 4
我们正在编写一个用于经济计算的软件。我们使用 Java 6、Swing 应用程序框架、MySQL 5.1、JDBC 和 Hibernate(JDBC 仅用于初始连接测试)。我们将该应用程序部署在 Wi
我通过 App Engine Java Rest API 项目连接到 Google Cloud SQL,以在云上检索和存储数据。我可以通过本地主机成功与服务器通信,但在发布后,我收到此错误: Tryi
我正在使用 Eclipse 和 Android 模拟器。我可以在模拟器浏览器中访问10.0.2.2。但在我的应用程序中,此代码未运行: Connection con = DriverManager.g
我的 MySQL 连接池有点问题。是这样的: 通过 Quartz 安排不同的作业。所有作业都连接到不同的数据库,这些数据库全天工作正常,而夜间计划的作业因 CommunicationsExceptio
我正在运行一个使用 Hibernate 和 C3P0 作为连接池、MySQL 5.6 数据库的 Java Web 应用程序。我继续在已部署的应用程序中遇到连接超时和生成错误的问题: Caused by
这个问题在这里已经有了答案: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
有人在执行某些查询时遇到过这样的错误吗? (jdbc4.CommunicationsException:通信链接失败) 一般来说,这个异常是应用程序和mysql之间的通信出现了一些问题。你可以找到很多
我的 jhipster 后端应用程序使用 mysql 和 elasticsearch,我使用“./mvnw package -Pprod dockerfile:build -DskipTests”成功
我有一个谷歌应用引擎连接的项目,我试图连接到谷歌云 sql 数据库。我将我的项目上传到谷歌,并通过它尝试连接到数据库。 我的连接网址如下-> Class.forName("com.mysql.jdbc
我有两个网络服务。一个 Web 服务调用另一个 Web 服务。这些 Web 服务托管在两个不同的 AWS 实例上。如果被调用的 Web 服务空闲一段时间,我会收到此异常 - com.mysql.jdb
我正在尝试从 GAE(Google App Engine)内部连接到我的 MySQL 数据库实例,也在 GAE 上。从外面看效果很好。 经过一周的搜索,我似乎找不到问题。 没有 MySQL 连接代码,
/** * */ package ORM; import java.sql.Connection; import java.sql.DriverManager; import java.sql.S
我们正在使用 Spring + Hibernate + MySql 运行一个 J2EE 应用程序,它在内部使用 C3P0 和 Connector/J 用于连接池。但我们收到通信链接失败错误以下是配置
我正在努力使数据库与Java程序进行通讯。 有人可以使用JDBC给我一个快速而肮脏的示例程序吗? 我收到一个相当惊人的错误: Exception in thread "main" com.mysql.
我正在努力让数据库与Java程序进行通讯。 有人可以使用JDBC给我一个快速而肮脏的示例程序吗? 我收到一个相当惊人的错误: Exception in thread "main" com.mysql.
我在保持事件检查期间遇到 异常,这意味着连接 (com.mysql.jdbc.JDBC4Connection@5d8f3404) 必须已断开。 CommunicationsException:通信链接
这个问题已经有答案了: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure (51
我的产品服务器出现以下异常,但几个小时后会自动解决。造成此问题的原因可能是什么 Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsExc
我是一名优秀的程序员,十分优秀!