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

发布于 2025-01-03 03:05:44 字数 1935 浏览 0 评论 0原文

我正在尝试连接到远程服务器(我的学校服务器来访问数据库),但我没有任何运气,我一直得到这个

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。可能是什么问题?

问候

I am trying to connect to a remote server (my schools server to access the database) but I am not having any luck i keep getting this

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

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

Here is a code i found in Java, just copied to see if i can connect..

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 ) ;
   }
 }
}

Just to complement the information, I am using Vista. What can be the issue?

Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

埖埖迣鎅 2025-01-10 03:05:44

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

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

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

There are many possible reasons why you get 'link failure' while connecting to your databse.

  1. Firewalls and/or routers blocking your connection to the db
  2. The database itself doesn't allow remote connection

If you are sure that case 2 doesn't apply you would have to check things on your side, switch off your antivirus/firewall software on development machine, if that doesn't work contact the db administrator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文