2012-02-07 90 views
0

我試圖連接到遠程服務器(我的學校服務器訪問數據庫),但我沒有任何運氣我不斷收到這個連接到遠程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

回答

1

有,爲什麼你在連接到您的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