com.mysql.jdbc.CommunicationsException: Communications link failure

悠悠隱於市發表於2011-04-18

com.mysql.jdbc.CommunicationsException: Communications link failure

Last packet sent to the server was 0 ms ago.
 at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1070)
 at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2104)
 at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:729)
 at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:298)
 at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:283)
 at java.sql.DriverManager.getConnection(DriverManager.java:525)
 at java.sql.DriverManager.getConnection(DriverManager.java:171)
 at pack.java.connection.mysql.Connection.createConn(Connection.java:11)
 at pack.java.connection.mysql.Connection.main(Connection.java:21)
Caused by: java.net.ConnectException: Connection refused: connect
 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:367)
 at java.net.Socket.connect(Socket.java:524)
 at java.net.Socket.connect(Socket.java:474)
 at java.net.Socket.<init>(Socket.java:371)
 at java.net.Socket.<init>(Socket.java:213)
 at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
 at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:276)
 at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2027)
 ... 7 more
null

 

解決方案:

	public static java.sql.Connection createConn() {
		java.sql.Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3036/portal","root","password");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

 

檢查你的埠號.  是否寫錯..是3306不是3036. 寫錯了,.就會發生如上錯誤!.

mysql預設埠3306,如果沒有改變..不需要指定也行。

 

相關文章