java - 连接到远程 mysql 服务器抛出错误

标签 java mysql bluej

我正在尝试连接到远程服务器(我的学校服务器来访问数据库),但我运气不佳,一直收到此消息

SQL Exception:
State  : 08S01
Message: Communications link failure

Last packet sent to the server was 0 ms ago.
Error  : 0

这是我在 Java 中找到的代码,只是复制来看看是否可以连接..

public void test()
 {
    try
    {
       // Load the database driver
       Class.forName( "com.mysql.jdbc.Driver" ) ;

       // Get a connection to the database
       Connection conn = DriverManager.getConnection( "jdbc:mysql://my.db.url.edu;databaseName=marco;user=USERNAME;password=PASSWORD" ) ;

       // Print all warnings
       for( SQLWarning warn = conn.getWarnings(); warn != null; warn = warn.getNextWarning() )
       {
          System.out.println( "SQL Warning:" ) ;
          System.out.println( "State  : " + warn.getSQLState()  ) ;
          System.out.println( "Message: " + warn.getMessage()   ) ;
          System.out.println( "Error  : " + warn.getErrorCode() ) ;
       }

       // Get a statement from the connection
       Statement stmt = conn.createStatement() ;

       // Execute the query
       ResultSet rs = stmt.executeQuery( "SELECT * FROM Test" ) ;

       // Loop through the result set
       while( rs.next() )
          System.out.println( rs.getString(1) ) ;

       // Close the result set, statement and the connection
       rs.close() ;
       stmt.close() ;
       conn.close() ;
   }
   catch( SQLException se )
   {
       System.out.println( "SQL Exception:" ) ;

       // Loop through the SQL Exceptions
       while( se != null )
       {
          System.out.println( "State  : " + se.getSQLState()  ) ;
          System.out.println( "Message: " + se.getMessage()   ) ;
          System.out.println( "Error  : " + se.getErrorCode() ) ;

          se = se.getNextException() ;
       }
   }
   catch( Exception e )
   {
      System.out.println( e ) ;
   }
 }
}

为了补充信息,我使用的是 Vista。可能是什么问题?

问候

最佳答案

连接数据库时出现“链接失败”的可能原因有很多。

  1. 防火墙和/或路由器阻止您与数据库的连接
  2. 数据库本身不允许远程连接

如果您确定情况 2 不适用,您必须检查自己的情况,关闭开发计算机上的防病毒/防火墙软件,如果这不起作用,请联系数据库管理员。

关于java - 连接到远程 mysql 服务器抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9172094/

相关文章:

java - 变量无法解析,Eclipse 中出现错误,但 BlueJ 中没有

java - 如何解决 Stackoverflow 错误?

java - 如何将这些流式映射键从 Longs 转换为对象?

java - Android Room 分页 - 如何读取两个表?

mysql - 如何将这两个查询合并为一个? (针对同一个表的多个联接)

php - 使用同一查询更新两行

java - 多个扫描仪 DoWhile HiLo(10 面骰子游戏)

java - 错误请求中的重定向

java - Fresco 未更新图像

java - 为什么我的表不是使用注释生成的?