我正在努力让我的数据库与我的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 …Run Code Online (Sandbox Code Playgroud) 我有一个Quartz Job,它每5分钟在我的MySQL数据库中执行一次存储过程,由于某种原因,3次执行中有1次失败并给出了这个奇怪的异常.我搜索并搜索了这个异常的含义,但我找不到解决方案.这是完整的堆栈跟踪:
java.sql.SQLException: Could not retrieve transation read-only status server
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:951)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:941)
at com.mysql.jdbc.ConnectionImpl.isReadOnly(ConnectionImpl.java:3939)
at com.mysql.jdbc.ConnectionImpl.isReadOnly(ConnectionImpl.java:3910)
at com.mysql.jdbc.PreparedStatement.checkReadOnlySafeStatement(PreparedStatement.java:1258)
at com.mysql.jdbc.CallableStatement.checkReadOnlySafeStatement(CallableStatement.java:2656)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1278)
at com.mysql.jdbc.CallableStatement.execute(CallableStatement.java:920)
at com.mchange.v2.c3p0.impl.NewProxyCallableStatement.execute(NewProxyCallableStatement.java:3044)
at org.deadmandungeons.website.tasks.RankUpdateTask.execute(RankUpdateTask.java:30)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 1,198,219 milliseconds ago. The last packet sent successfully to the server was 950,420 milliseconds ago.
at sun.reflect.GeneratedConstructorAccessor43.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526) …Run Code Online (Sandbox Code Playgroud) 我有一个tomcat实例设置,但我配置的数据库连接context.xml在一段时间不活动后仍然会死.
当我检查日志时,我收到以下错误:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:从服务器成功收到的最后一个数据包是68051秒前.成功发送到服务器的最后一个数据包是68051秒前,这比服务器配置的'wait_timeout'值长.您应该考虑在应用程序中使用之前过期和/或测试连接有效性,增加服务器配置的客户端超时值,或使用Connector/J连接属性"autoReconnect = true"来避免此问题.
这是context.xml中的配置:
<Resource name="dataSourceName"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="username"
password="********"
removeAbandoned = "true"
logAbandoned = "true"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://127.0.0.1:3306/databasename?autoReconnect=true&useEncoding=true&characterEncoding=UTF-8" />
Run Code Online (Sandbox Code Playgroud)
我正在使用autoReconnect=true错误说要做,但连接一直在死.我以前从未见过这种情况.
我还验证了所有数据库连接都已正确关闭.
我收到此错误(不是那么频繁):
2013-05-08 16:44:35,786 WARN (JDBCExceptionReporter.java:100) [org.hibernate.util.JDBCExceptionReporter, logExceptions] - SQL Error: 0, SQLState: 08S01
2013-05-08 16:44:35,786 ERROR (JDBCExceptionReporter.java:101) [org.hibernate.util.JDBCExceptionReporter, logExceptions] - 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.
2013-05-08 16:44:35,833 ERROR (AdminDaoImpl.java:297) [com.myapp.admin.db.AdminDaoImpl, createFilePackage] - data-upload: Exception while adding new file package
org.hibernate.exception.JDBCConnectionException: Cannot open connection
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:97) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:142) …Run Code Online (Sandbox Code Playgroud) 我正在使用 PhpStorm + DataGrip + MySQL + WAMP 开发一个 PHP 项目,从那时起,我就可以使用这些配置正常使用远程数据库(参见图片):
但突然我收到这个错误:
[08S01] 通信链路故障 最后一次成功发送到服务器的数据包是在 0 毫秒前。驱动程序尚未收到来自服务器的任何数据包。java.net.ConnectException:连接被拒绝:连接
有人知道我可以做什么来解决这个错误吗?
我已经尝试做我在这里找到的事情(用 JDBC 和 MySQL 解决“通信链接故障”),但还没有成功。
我希望能够再次正常连接到我的数据库。
如果我的Spring Boot应用程序在几个小时内处于非活动状态(例如在夜间),我会收到此错误:
2015-05-19 09:16:32.666 WARN 20582 --- [http-nio-8080-exec-6] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 08S01
2015-05-19 09:16:32.668 ERROR 20582 --- [http-nio-8080-exec-6] o.h.engine.jdbc.spi.SqlExceptionHelper : Communications link failure
The last packet successfully received from the server was 29.792.613 milliseconds ago. The last packet sent successfully to the server was 6 milliseconds ago.
Run Code Online (Sandbox Code Playgroud)
试图解决这个问题我读到MySQL有一个名为wait_timeoutsetted 的参数,默认为8小时(28800秒),在此之后我的所有非活动连接都关闭了,所以我的Spring Boot应用程序停止工作......
我的问题是:
编辑
这里还有其他类似/有用的问题:
我无法连接到我的数据库.我认为有一个我不知道的地方,需要改变.我在这里有一些非常基本的代码:
public static void main(String[] args)
{
try
{
Connection con = DriverManager.getConnection("jdbc:mysql://IP:3306/TABLENAME?autoReconnect=true","USERNAME", "PASSWORD");
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
显然有登录信息.它会导致此错误:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
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.
Caused by: java.net.ConnectException: Connection refused
Run Code Online (Sandbox Code Playgroud)
我已经浏览了大约10个关于同一问题的不同帖子,并尝试了他们的解决方案,仍然无法让它工作.
计划
我希望我的tomcat服务器能够在单独的容器中连接到我的MySQL服务器.
问题
Tomcat无法连接到MySQL
我使用了wordpress教程中有关设置mysql容器链接的一些细节,并创建了MySQL的链接.
虽然tomcat和mysql旋转得很好我似乎无法让tomcat能够连接到MySQL,我的本地机器上的设置工作完全没问题.
我也尝试过使用--net: "host"虽然它不能用于Tomcat,因为它会引发严重的错误.
以前的答案
我在这篇文章中注意到了错误的可能修复程序,尽管我不相信其中任何一个会转换为我的问题,因为我认为这是一个docker问题而不是主机问题.
泊坞窗,compose.yml
web:
image: tomcat:7.0
container_name: tomcat-container
ports:
- "80:8080"
hostname: docker-tomcat
volumes:
- /home/webapps:/usr/local/tomcat/webapps
links:
- db
db:
image: mysql
container_name: mysql-container
environment:
MYSQL_ROOT_PASSWORD: Mysqlpassword1
MYSQL_DATABASE: tracker
volumes:
- /home/mysqlDB:/var/lib/mysql
Run Code Online (Sandbox Code Playgroud)
这是我的 Context.xml from tomcat.
<Context>
<Resource
name="jdbc/tracker" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
url="jdbc:mysql://localhost:3306/tracker?useSSL=false"
driverClassName="com.mysql.jdbc.Driver"
username="root" password="mysqladmin1"
/>
<Resource
name="jdbc/jenkins" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
url="jdbc:mysql://localhost:3306/jenkins?useSSL=false"
driverClassName="com.mysql.jdbc.Driver"
username="root" password="mysqladmin1"
/>
</Context>
Run Code Online (Sandbox Code Playgroud)
错误代码.
The last packet sent successfully …Run Code Online (Sandbox Code Playgroud) 我"Communications link failure"在这行代码中得到错误:
mySqlCon = DriverManager.getConnection("jdbc:mysql://**server ip address**:3306/db-name", "mu-user-name", "my-password");
Run Code Online (Sandbox Code Playgroud)
我查了这篇文章中的所有内容:
当我将查询字符串更改为:
mySqlCon = DriverManager.getConnection("jdbc:mysql://**server ip address**:22/127.0.0.1:3306/db-name", "mu-user-name", "my-password");
Run Code Online (Sandbox Code Playgroud)
我收到了错误 Packet for query is too large (4739923 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable.
虽然我已经改变了my.cnf上的数据包大小,然后重新启动了mysql服务.
有什么建议?
注意:
我可以通过ssh与这段代码连接,但这种方式似乎不合理!我可以在main中连接一次然后我应该将连接传递给所有类.
public my-class-constructor() {
try {
go();
} catch (Exception e) {
e.printStackTrace();
}
mySqlCon = null; …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用MySQL数据库(安装在我的电脑上的虚拟机(Debian)上并具有IP地址192.168.1.5)来使用NetBeans.
我已按如下方式配置连接:
Driver Name MySQL(Connector/J Driver)
Host 192.168.1.5
Database test
Username root
Password *
JDBC URL jdbc:mysql://192.168.1.5:3306/test
Run Code Online (Sandbox Code Playgroud)
然后我收到以下错误:
cannot establish a connection to jdbc:mysql://192.168.1.5:3306/test using
com.mysql.jdbc.Driver (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.)
Run Code Online (Sandbox Code Playgroud)
我的mysql.user表看起来像这样(我知道% root这不是很安全,但这只是为了简化目前的事情):
+------------+------------------+
| host | user |
+------------+------------------+
| % | root |
| 127.0.0.1 | root |
| ::1 | root …Run Code Online (Sandbox Code Playgroud) 我最近学习了客户端和服务器之间的套接字编程。所以我想到做一个连接客户端和服务器的练习。但是,当我尝试运行代码时遇到此错误消息: 线程“main”java.net.ConnectException 中的异常:连接被拒绝:连接
这是我的客户端类代码:
public class clientpart {
public static void main(String[]args) throws UnknownHostException, IOException {
Scanner input = new Scanner(System.in);
int port = 8080;
String host=null;
String answer; String sendMessage; String receivedMessage;
InetAddress address = InetAddress.getByName(host);
Socket socket= new Socket(address,port);
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
System.out.println("Please answered the following question: ");
System.out.println("What is the subject code for Socket Programming?");
answer = input.nextLine();
sendMessage = answer;
bw.write(sendMessage);
bw.newLine();
bw.flush();
System.out.println("Message sent …Run Code Online (Sandbox Code Playgroud) 对不起也许这是我第二次问这个问题是因为没有得到任何答案.
这是我的代码
try{
File f = new File("Database.sql");
if(f.exists()){
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/"+f.getName(),"","");
}else{
f.createNewFile();
System.out.println("file created");
//also do the connection
}
}catch(Exception ex){
System.out.println(ex.getMessage());
}
Run Code Online (Sandbox Code Playgroud)
这是错误:
通信链路故障成功发送到服务器的最后一个数据包是0毫秒前.驱动程序未收到来自服务器的任何数据包.
这个问题的目的是:我正在创建一个适用于许多用户的应用程序,问题是他们对计算机一无所知,我必须尽可能简单.
那么有没有办法通过目录路径连接MYSQL,如MS ACCESS?
或者还有其他建议吗?
谢谢 .
我尝试在 Docker 上运行带有 mysql 连接的 Springboot 应用程序。没有docker,它工作得很好。但是当我尝试在容器上部署时,我有com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure。\n我使用这个 docker-compose:
version: \'3\'\nservices:\n nginx:\n container_name: nginx\n image: nginx:latest\n restart: always\n ports:\n - 80:80\n - 443:443\n volumes:\n - ./nginx/conf.d:/etc/nginx/conf.d\n depends_on:\n - spring-boot-app\n\n\n mysql:\n container_name: mysql\n image: mysql:latest\n environment:\n MYSQL_ROOT_PASSWORD: "rootpass"\n MYSQL_DATABASE: "db"\n MYSQL_USER: "user"\n MYSQL_PASSWORD: "pass"\n ports:\n - "1306:3306"\n volumes:\n - ./data/:/var/lib/mysql\n networks: \n - mysql-db\n restart: always\n\n spring-boot-app:\n container_name: spring-boot-app\n image: spring-boot-app\n build:\n context: ./spring-boot-app\n dockerfile: Dockerfile\n ports:\n - "8080:8080"\n depends_on:\n - mysql\n networks: \n - mysql-db\n …Run Code Online (Sandbox Code Playgroud)