我只是想通过使用以下代码来显示 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.
我该如何解决这个问题?
最佳答案
您正在创建与 mysql 的连接,但未提及数据库名称
jdbc:mysql://Localhost:3306
它应该被定义为
jdbc:mysql://Localhost:3306/test
它可能会解决你的问题
关于java - 如何消除Java中的CommunicationException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20675818/