英文:
How to fix the error "The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server"
问题
你好,我刚刚创建了我的应用程序,目前还是空的。我只是放了一个用于连接到我的SQL数据库的类,但是当我运行我的应用程序时,我收到了以下错误信息:
> 警告:
> com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@17460bca
> -- 获取尝试失败!正在清除待定的获取。在尝试获取所需的新资源时,我们未能成功执行超过允许的获取尝试的最大数量(30次)。上次获取尝试异常:
> com.mysql.cj.jdbc.exceptions.CommunicationsException:通信链接失败
>
> 上次成功发送给服务器的数据包距离现在已经过去了0毫秒。驱动程序没有从服务器接收到任何数据包。位于
> com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
> ……(以下为其他错误信息,省略)
这是我编写的代码:
```java
package com.luv2code.testdb;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
@WebServlet("/TestDbServlet")
public class TestDbServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 建立与MySQL的连接:
String user = "springstudent";
String password = "springstudent";
String jdbcUrl = "jdbc:mysql://localhost:3309/web_customer_tracker?useSSL=false&serverTimezone=UTC";
String driver = "com.mysql.jdbc.Driver";
try {
PrintWriter out = response.getWriter();
out.println("连接到数据库:" + jdbcUrl);
Class.forName(driver);
Connection cnx;
cnx = DriverManager.getConnection(jdbcUrl, user, password);
out.println("成功!!!!!!");
cnx.close();
}
catch (Exception ex) {
ex.printStackTrace();
throw new ServletException(ex);
}
}
}
我将端口从3306更改为3309,因为在我的MySQL Workbench中,端口3306无法工作,会出现以下错误:
(图片链接1)
即使在我想要使用端口3306创建新数据库时,也会出现以下错误:
(图片链接2)
我不知道是不是这个端口问题导致了这个错误。
请帮忙解决,谢谢!
<details>
<summary>英文:</summary>
hello i just created my application it is still empty i just put a class which allows to connect to my sql database but when i run my application i get the following error:
> AVERTISSEMENT:
> com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@17460bca
> -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more
> than the maximum number of allowed acquisition attempts (30). Last
> acquisition attempt exception:
> com.mysql.cj.jdbc.exceptions.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
> com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
> at
> com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
> at
> com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:832)
> at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
> at
> com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
> at
> com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207)
> at
> com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:175)
> at
> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:220)
> at
> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:206)
> at
> com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:203)
> at
> com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1138)
> at
> com.mchange.v2.resourcepool.BasicResourcePool.doAcquireAndDecrementPendingAcquiresWithinLockOnSuccess(BasicResourcePool.java:1125)
> at
> com.mchange.v2.resourcepool.BasicResourcePool.access$700(BasicResourcePool.java:44) at
> com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask.run(BasicResourcePool.java:1870)
> at
> com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:696)
> Caused by: com.mysql.cj.exceptions.CJCommunicationsException:
> 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.GeneratedConstructorAccessor34.newInstance(Unknown Source)
> at
> sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
> at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
> at
> com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
> at
> com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
> at
> com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
> at
> com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
> at
> com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91)
> at com.mysql.cj.NativeSession.connect(NativeSession.java:152) at
> com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:952)
> at
> com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:822)
> ... 12 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.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:173)
> at
> com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65)
> ... 15 more
here is the code i made :
package com.luv2code.testdb;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
/**
* Servlet implementation class TestDbServlet
*/
@WebServlet("/TestDbServlet")
public class TestDbServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// établir la connexion à mysql :
String user="springstudent";
String password="springstudent";
String jdbcUrl="jdbc:mysql://localhost:3309/web_customer_tracker?useSSL=false&serverTimezone=UTC";
String driver="com.mysql.jdbc.Driver";
try {
PrintWriter out= response.getWriter();
out.println("Connection à la base de donnée :"+jdbcUrl);
Class.forName(driver);
Connection cnx;
cnx=DriverManager.getConnection(jdbcUrl, user, password);
out.println("Success !!!!!!!!!");
cnx.close();
}
catch(Exception ex) {
ex.printStackTrace();
throw new ServletException(ex);
}
}
}
I put port 3309 instead of 3306 because port 3306 does not work in my mysqlworkbench it gives me the following error :
[![error 1][1]][1]
and even when i want to create a new database with port 3306 it gives me the following error:
[![error 2][2]][2]
I do not know if it is this port problem that gives me this error.
help me please !!
[1]: https://i.stack.imgur.com/D8x2C.png
[2]: https://i.stack.imgur.com/DGuOf.png
</details>
专注分享java语言的经验与见解,让所有开发者获益!
评论