JDBC连接超时

时间:2012-12-15 00:54:47

标签: java mysql jdbc

尝试创建一个基于JDBC的应用程序,该应用程序连接到允许用户编辑网站的数据库。

每次我从eclipse测试应用程序时都会出现连接超时错误,并且不知道原因。

很抱歉,如果这是一个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)

3 个答案:

答案 0 :(得分:1)

当数据库服务器未运行时,通常会发生连接超时。 我想你的代码中缺少dbname。只需查看this link

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

答案 1 :(得分:0)

检查您的连接凭据,然后检查您正在连接的服务器是否已启动并正在运行。

答案 2 :(得分:0)

几件事:不是通过localhost:3306在本地而是通过网络通过的数据库必须通过防火墙。

端口可能是有序的,对于MySQL,默认值为3306,如localhost:3306中所示。

这两种情况都可能意味着没有任何联系,并会解释您的错误。

事物应该关闭;否则可能会耗尽连接等。 最好使用 try-with-resources 构造。

然后应该几乎使用PreparedStatement来转义反斜杠和引号,并防止SQL注入。

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

        String sql = "SELECT * FROM tbl_adminLogin";
        try (PreparedStatement stmt = con.prepareStatement(sql)) {
            try (ResultSet rs = stmt.executeQuery()) {
                if (rs.next()) {
                    String s = rs.getString("name");
                    System.out.println(s);
                }
            }
        }
    }
}