java - 如何消除Java中的CommunicationException?

标签 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.

我该如何解决这个问题?

最佳答案

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

jdbc:mysql://Localhost:3306 

它应该被定义为

jdbc:mysql://Localhost:3306/test

它可能会解决你的问题

关于java - 如何消除Java中的CommunicationException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20675818/

相关文章:

java程序在比较2个文件时找出错误的列

java - Spark `FileAlreadyExistsException` when `saveAsTextFile` 即使输出目录不存在

mysql - 具有一列和多个条件的 where 子句返回一行而不是 13

php - PDO 中的 mysql_result

mysql - INSERT INTO 使用来自其他表的数据导致错误 1064

sql - 需要帮助编写查询

java - 为多个 Web 服务指定模拟时,instanceof 运算符的替代方案

java - 在Java中解析一个固定宽度的格式化文件

java - Hibernate 自动创建数据库

java - 使用 Docker 桥接网络时无法在集成测试中获取 JDBC 连接