猿问

JDBC无法连接数据库?

驱动是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


一只勤奋的鱼3773709
浏览 3009回答 4
4回答

路人丶丨

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

忘性最大的人

搬运stack overflowIP 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. 

悠小爷

长时间不妨问,connection失效导致的。Caused by: java.net.SocketException: Connection reset 学会看报错信息,长时间不妨问connection重置了。重启mysql: service mysql restart  
随时随地看视频慕课网APP

相关分类

Java
MySQL
我要回答