2013-12-19 17 views
0

我只是想通過使用下面的代碼顯示mysql服務器的當前狀態。如何在Java中刪除CommunicationException?

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. 

我怎麼能解決這個問題?

+0

是本地主機是否有效?嘗試localhost。 MySQL正在運行? –

回答

0

你創建你的MySQL連接,並沒有提到數據庫名稱

jdbc:mysql://Localhost:3306 

它應該被定義爲

jdbc:mysql://Localhost:3306/test 

它可能會解決你的問題

+0

沒有朋友,它也顯示相同的錯誤信息。 – user3099091

+0

你有沒有更改MySQL端口? –

+0

使用以下netstat/a/o |檢查mysql端口更多在cmd.exe中 –