Matthew Law
Matthew Law

Reputation: 31

JDBC Connection timed out

Trying to create a JDBC based app that connects to a database allowing a user to edit a website.

Each time I test the app from eclipse I get a connection timed out error and have no idea why.

Sorry if it's a n00by question I'm new to JDBC. Any help or suggestions will be greatly appreciated.

Below is my code:

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();
}

Below is the Error Message that is printing out in my console:

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)

Upvotes: 3

Views: 16507

Answers (3)

Joop Eggen
Joop Eggen

Reputation: 109532

Several things: database not locally via localhost:3306 but over the net must pass through the firewall.

A port might be in order, for MySQL the default is 3306, as in localhost:3306.

Both these things might mean no connection, and would explain your error.

Things should be closed; otherwise one might run out of connections and such. This can best be done with the try-with-resources construct.

And then one should almost use PreparedStatements to escape backslashes and quotes, and prevent SQL injection.

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);
                }
            }
        }
    }
}

Upvotes: 1

luffymonkey
luffymonkey

Reputation: 50

Connection timeout generally occurs when the database server is not running. dbname is missing in your code i guess. Just check this link

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

Upvotes: 1

Alpesh Gediya
Alpesh Gediya

Reputation: 3794

Check your connections credentials then also check server you are connecting is up and running.

Upvotes: 0

Related Questions