2012-12-15 195 views
2

嘗試創建基於JDBC的應用程序,該應用程序連接到允許用戶編輯網站的數據庫。JDBC連接超時

每次我測試應用程序從日食我得到一個連接超時錯誤,不知道爲什麼。

對不起,如果這是一個n00by問題,我是JDBC新手。任何幫助或建議將不勝感激。

下面是我的代碼:

try 
{ 
    Class.forName("com.mysql.jdbc.Driver").newInstance(); 
    Connection con = DriverManager.getConnection(
     "jdbc:mysql://nkuwebdata.db.8750359.hostedresource.com", 
     username, 
     password); 

    Statement stmt = con.createStatement(); 
    ResultSet rs = stmt.executeQuery("Select * FROM tbl_adminLogin"); 
    while(rs.next()) 
    { 
     String s = rs.getString("name"); 
     System.out.println(s); 
     break; 
    } 
} catch (SQLException e) 
{ 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (InstantiationException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (IllegalAccessException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (ClassNotFoundException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

下面是我的控制檯打印出錯誤消息:

com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 

** BEGIN NESTED EXCEPTION ** 

java.net.ConnectException 
MESSAGE: Connection timed out: connect 

STACKTRACE: 

java.net.ConnectException: Connection timed out: connect 
    at java.net.DualStackPlainSocketImpl.connect0(Native Method) 
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) 
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) 
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) 
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source) 
    at java.net.PlainSocketImpl.connect(Unknown Source) 
    at java.net.SocksSocketImpl.connect(Unknown Source) 
    at java.net.Socket.connect(Unknown Source) 
    at java.net.Socket.connect(Unknown Source) 
    at java.net.Socket.<init>(Unknown Source) 
    at java.net.Socket.<init>(Unknown Source) 
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256) 
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:271) 
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771) 
    at com.mysql.jdbc.Connection.<init>(Connection.java:1555) 
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285) 
    at java.sql.DriverManager.getConnection(Unknown Source) 
    at java.sql.DriverManager.getConnection(Unknown Source) 
    at uk.co.majorwebdesigns.ContentManager.connectToAndQueryDatabase(ContentManager.java:100) 
    at uk.co.majorwebdesigns.ContentManager.createMenuBar(ContentManager.java:92) 
    at uk.co.majorwebdesigns.ContentManager.launch(ContentManager.java:61) 
    at uk.co.majorwebdesigns.ContentManager.main(ContentManager.java:133) 


** END NESTED EXCEPTION ** 



Last packet sent to the server was 2 ms ago. 
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:2847) 
    at com.mysql.jdbc.Connection.<init>(Connection.java:1555) 
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285) 
    at java.sql.DriverManager.getConnection(Unknown Source) 
    at java.sql.DriverManager.getConnection(Unknown Source) 
    at uk.co.majorwebdesigns.ContentManager.connectToAndQueryDatabase(ContentManager.java:100) 
    at uk.co.majorwebdesigns.ContentManager.createMenuBar(ContentManager.java:92) 
    at uk.co.majorwebdesigns.ContentManager.launch(ContentManager.java:61) 
    at uk.co.majorwebdesigns.ContentManager.main(ContentManager.java:133) 
+2

您的MySQL配置必須接受遠程訪問,並將您的應用程序的遠程IP添加到遠程訪問白名單。 – Vulcan

回答

0

連接超時一般數據庫服務器不運行時發生。 我猜你的代碼中缺少dbname。只是檢查this link

Class.forName("com.mysql.jdbc.Driver"); 
Connection conn = null; 
conn = DriverManager.getConnection("jdbc:mysql://hostname:port/dbname","username", "password"); 
conn.close(); 
0

檢查連接的憑據,然後還要檢查你正在連接的服務器啓動和運行。