com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败 - 引起:java.net.UnknownHostException:null:未知错误

时间:2015-11-11 14:25:22

标签: java mysql

我在几天内收到此错误,但未对应用程序进行任何更改。

这是例外,我找不到与

相关的任何内容
Caused by: java.net.UnknownHostException: null: unknown error

这是堆栈跟踪

    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(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:983)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:339)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2252)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2285)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2084)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:795)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:400)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:327)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at it.besmart.parkserver.DBConnection.selectQuery(DBConnection.java:37)
    at it.besmart.parkserver.SocketClientHandler.updatePark(SocketClientHandler.java:135)
    at it.besmart.parkserver.StartServer.main(StartServer.java:31)
Caused by: java.net.UnknownHostException: null: unknown error
    at java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:907)
    at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1302)
    at java.net.InetAddress.getAllByName0(InetAddress.java:1255)
    at java.net.InetAddress.getAllByName(InetAddress.java:1171)
    at java.net.InetAddress.getAllByName(InetAddress.java:1105)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:191)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:298)
    ... 17 more
java.lang.NullPointerException
    at it.besmart.parkserver.StartServer.main(StartServer.java:31)

正在进行连接的班级是:

public class DBConnection {
    private String host;
    private String name;
    private String user;
    private String password;
    private String query;
    private String column;

    public DBConnection(String host, String name, String user, String password, String query, String column){
        this.host = host;
        this.name = name;
        this.user = user;
        this.password = password;
        this.query = query;
        this.column = column;
    }
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    String hostcompleto = "jdbc:mysql://"+host+":3306/"+name;

    public String[] selectQuery(){
        String[] result = null;

        try{
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        conn = DriverManager.getConnection(hostcompleto, user, password);
        stmt = conn.createStatement();
        rs = stmt.executeQuery(query);
        ArrayList<String> list= new ArrayList<String>();

        while (rs.next()) {
               list.add(rs.getString(column)); 
        }   

        result = new String[list.size()];
        result = list.toArray(result);
        for(int i =0; i<result.length; i++){
              System.out.println(result[i]);
        } 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (stmt != null) stmt.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
        }
        return result;



    }

正在运行的数据库,我可以通过PHPMyadmin访问它,它发生在我尝试的每个查询中,但我不认为这是一个查询问题...

2 个答案:

答案 0 :(得分:1)

请在调用构造函数后调用:

String hostcompleto = "jdbc:mysql://"+host+":3306/"+name;

在您创建hostcompleto时,字符串hostname必须为null, 两者都是class members,并将在构造函数中填充。

hostcompleto放在selectQuery方法中,或将其定义为类成员并在构造函数中初始化它:

 public String[] selectQuery(){
    String[] result = null;

    try{
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      String hostcompleto = "jdbc:mysql://"+host+":3306/"+name;
      conn = DriverManager.getConnection(hostcompleto, user, password);

public DBConnection(String host, String name, String user, String password, String query, String column){
        this.host = host;
        this.name = name;
        this.user = user;
        this.password = password;
        this.query = query;
        this.column = column;
        this.hostcompleto = "jdbc:mysql://"+host+":3306/"+name;
    }

答案 1 :(得分:0)

(请点数)

使用java冗余更好一点:

  • 不变的字段可以是最终的
  • 从JDBC 4开始,运行时驱动程序加载不再需要Class.forName
  • 尝试资源自动关闭内容
  • SQL异常的快速失败意味着抛出它
  • 记录错误查询
  • 仍然很有用
  • 返回“常规”列表而不是数组

特别是try-with-resources节省了输入。

private final String user;
private final String password;
private final String query;
private final String column;
private final String hostcompleto;

public DBConnection(String host, String name, String user, String password, String query, String column) {
    this.user = user;
    this.password = password;
    this.query = query;
    this.column = column;
    hostcompleto = "jdbc:mysql://" + host + ":3306/" + name;
}

public List<String> selectQuery() throws SQLException {
    try (Connection conn = DriverManager.getConnection(hostcompleto, user, password);
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(query)) {

        List<String> list = new ArrayList<>();

        while (rs.next()) {
            list.add(rs.getString(column));
        }
        for (String item : list) {
            System.out.println(item);
        }
        return list;
    } catch (SQLException e) {
        Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, "Error in: " + query, e);
        throw e;
    }
}