繁体   English   中英

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

[英]connecting to remote mysql server throwing errors

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

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种情况不适用,则必须检查一下情况,然后关闭开发计算机上的防病毒/防火墙软件,如果不能正常工作,请与数据库管理员联系。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM