错误:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败

时间:2019-01-07 02:35:14

标签: java database

服务器中的错误消息

    [2019-01-07 09:44:02] ERROR 
    :com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications 
    link failure
    [2019-01-07 09:44:02]
    [2019-01-07 09:44:02] The last packet sent successfully to the server was 0 
    milliseconds ago. The driver has not received any packets from the server.

但是在本地测试..没问题..没有错误消息..

  1. 提交库..

    mysql-connector-java-5.1.47-bin.jar 
    mysql-connector-java-5.1.47.jar
    
  2. JDBC URL更改

    jdbc:mysql://IP:3306/ID -> jdbc:mysql://IP:3306/ID?autoReconnect=true
    

    更改的错误消息。

    [2019-01-07 11:04:32] Error : 
    com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could 
    not create connection to database server. Attempted reconnect 3 times. 
    Giving up.
    

ControlDAO.java

private final static String DRIVER = "com.mysql.jdbc.Driver";
private final static String URL = 
"jdbc:mysql://IP:3306/ID?autoReconnect=true;

public static void CsNumberCheck(String s) { 
    try {           
        Class.forName(DRIVER);

        conn = DriverManager.getConnection(URL, "ID", "PW");
        System.out.println(conn);

        stat = conn.createStatement();

        int result = 0;

        sql = Query;

        rs = stat.executeQuery(sql);

        while ( rs.next() ) {
            result = rs.getInt(1);
        }

        rs.close();
        stat.close();

        System.out.println(result);

        CsNumberResult(result, OrangeEmail);
    }
    catch (ClassNotFoundException e) {
        System.out.println("Driver Loading Failed..");
    }
    catch (SQLException e) {
        System.out.println("Error : " + e);
    }
}

Socket.java

     ControlDAO.CsNumberCheck(s);
  • 但是“ Sysout”没有结果,只有错误消息。...

1 个答案:

答案 0 :(得分:0)

检查Windows服务以查看MySQL服务器是否正在运行。那可能是原因。如果不是,请启动它并尝试重新连接。