2

尝试创建一个基于 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)
4

3 回答 3

1

连接超时通常发生在数据库服务器未运行时。我猜你的代码中缺少 dbname。只需检查此链接

Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
conn = DriverManager.getConnection("jdbc:mysql://hostname:port/dbname","username", "password");
conn.close();
于 2013-04-01T06:01:26.937 回答
1

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

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

这两件事都可能意味着没有联系,并且会解释你的错误。

事情应该关闭;否则可能会用完连接等。最好使用try-with-resources结构来完成。

然后几乎应该使用PreparedStatements 来转义反斜杠和引号,并防止 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);
                }
            }
        }
    }
}
于 2019-02-27T14:20:45.043 回答
0

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

于 2013-04-01T06:41:31.483 回答