我只是想通過使用下面的代碼顯示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.
我怎麼能解決這個問題?
是本地主機是否有效?嘗試localhost。 MySQL正在運行? –