连接到远程MySQL服务器引发错误

问题描述:

我试图连接到远程服务器(我的学校服务器访问数据库),但我没有任何运气我不断收到这个连接到远程MySQL服务器引发错误

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

问候

+0

服务器可能被配置为不接受远程连接 – 2012-02-07 06:52:51

+0

希望这是您的学校不允许互联网上的所有内容进入数据库。 – 2012-02-07 07:02:44

+0

它不是我改变了那部分:) - 哦,实际上我们不需要连接到该数据库..但没有运气..也许这是他们需要检查的东西 – user710502 2012-02-07 07:03:40

有,为什么你在连接到您的DATABSE获得“链接失败”很多可能的原因。

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

如果您确信这种情况下,2不适用你必须检查你身边的事情,在开发机器上关闭你的防病毒/防火墙软件,如果不行的话联系数据库管理员。

+0

伟大的如何允许远程连接在该数据库上,我可以登录使用SSH,但不是从Java程序 – user710502 2012-02-07 07:14:04

+0

http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-到MySQL数据库,server.html – Kris 2012-02-07 07:27:21