为了账号安全,请及时绑定邮箱和手机立即绑定

JDBC无法连接数据库?

JDBC无法连接数据库?

一只勤奋的鱼3773709 2016-10-16 20:11:39
驱动是5.1.40的,MySQL是5.6.33的,数据库开着,能进能修改package com.kevin.db; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class DBUtil {     private static final String URL="jdbc:mysql://127.0.0.1:3306/test";     private static final String USER="root";     private static final String PASSWORD="root"; public static void main(String[] args) throws Exception{ Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection(URL,USER,PASSWORD); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT id,pname FROM provinces"); while(rs.next()){ System.out.println(rs.getString("pname")+","+rs.getInt("id")); } } }下面是异常: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(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:425) at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:989) at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:632) at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1016) at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2253) 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:425) 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.kevin.db.DBUtil.main(DBUtil.java:14) Caused by: java.net.SocketException: Connection reset at java.net.SocketInputStream.read(Unknown Source) at java.net.SocketInputStream.read(Unknown Source) at com.mysql.jdbc.util.ReadAheadInputStream.fill(ReadAheadInputStream.java:101) at com.mysql.jdbc.util.ReadAheadInputStream.readFromUnderlyingStreamIfNecessary(ReadAheadInputStream.java:144) at com.mysql.jdbc.util.ReadAheadInputStream.read(ReadAheadInputStream.java:174) at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:3005) at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:567) ... 16 more
查看完整描述

4 回答

?
路人丶丨

TA贡献115条经验 获得超45个赞

应该是因为数据库一直保持打开,很久没有进行操作。  mysql自动断开,连接池认为还在连接中

查看完整回答
1 反对 回复 2016-10-17
?
忘性最大的人

TA贡献118条经验 获得超37个赞

搬运stack overflow

  1. IP address or hostname in JDBC URL is wrong.

  2. Hostname in JDBC URL is not recognized by local DNS server.

  3. Port number is missing or wrong in JDBC URL.

  4. DB server is down.

  5. DB server doesn't accept TCP/IP connections.

  6. DB server has run out of connections.

  7. Something in between Java and DB is blocking connections, e.g. a firewall or proxy. 


查看完整回答
1 反对 回复 2016-10-16
?
悠小爷

TA贡献21条经验 获得超8个赞


长时间不妨问,connection失效导致的。

Caused by: java.net.SocketException: Connection reset 学会看报错信息,长时间不妨问connection重置了。

重启mysql: service mysql restart  

查看完整回答
反对 回复 2016-10-23
  • 4 回答
  • 1 关注
  • 3003 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信