gpt4 book ai didi

java - 如何消除Java中的CommunicationException?

转载 作者:行者123 更新时间:2023-11-30 00:54:42 28 4
gpt4 key购买 nike

我只是想通过使用以下代码来显示 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com