如何在Java中删除CommunicationException?

时间:2013-12-19 07:31:49

标签: java mysql

我只是想通过使用以下代码来显示mysql服务器的当前状态。

public static void main(String[] args) throws Exception {

    String dbURL = "jdbc:mysql://Localhost:3306";
    String username ="root";
        String password = "trace";
        Connection dbCon = null;
        Statement stmt = null;
        ResultSet rs = null;
        String query ="SHOW GLOBAL STATUS";
       Class.forName("com.mysql.jdbc.Driver").newInstance();
    dbCon = DriverManager.getConnection(dbURL, username, password);
        stmt = dbCon.prepareStatement(query);
         rs = stmt.executeQuery(query);
         System.out.println(rs);
         while(rs.next())
         {
           String Counter = rs.getString(1);
           int val=rs.getInt(2);
           System.out.print("Counter Name : " + Counter);
           System.out.print("---   Value : " + val);
         }
      }

但是我收到了这个错误:

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.

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您正在创建与mysql的连接,并且未提及数据库名称

jdbc:mysql://Localhost:3306 

应该定义为

jdbc:mysql://Localhost:3306/test

它可能会解决您的问题