com.mysql.jdbc.CommunicationsException: Communications link failure

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,如果没有改变..不需要指定也行。

 

你可能感兴趣的:(java,mysql,.net,socket,jdbc)