Hi All,

This is the first time I'm using Java to connect to my online MySQL database that is hosted by a separate web host. Following is the code in use:
(I've removed the server url, username and pass for obvious reasons. They all work when I use them in my ASP web project.)

import java.sql.*;

 * @author Siddharth Dahiya
public class TWMC_Online {

     * @param args the command line arguments
    public static void main(String[] args) {
        try {
            String connectionUrl = "jdbc:mysql://tribalwars.db.4489877.hostedresource.com";
            Connection con = DriverManager.getConnection(connectionUrl, "xxxx", "xxxx");
            System.out.println("Con Done...");
        } catch (SQLException e) {
            System.out.println("SQL Exception: " + e.toString());
        } catch (ClassNotFoundException e) {
            System.out.println("Class Not Found Exception: " + e.toString());

I get the following error:

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


MESSAGE: Connection timed out: connect


java.net.ConnectException: Connection timed out: connect
	at java.net.PlainSocketImpl.socketConnect(Native Method)
	at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
	at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
	at java.net.Socket.connect(Socket.java:529)
	at java.net.Socket.connect(Socket.java:478)
	at java.net.Socket.<init>(Socket.java:375)
	at java.net.Socket.<init>(Socket.java:218)
	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(DriverManager.java:582)
	at java.sql.DriverManager.getConnection(DriverManager.java:207)
	at twmc_online.TWMC_Online.main(TWMC_Online.java:23)


Last packet sent to the server was 0 ms ago.
BUILD SUCCESSFUL (total time: 21 seconds)

Edited by ~s.o.s~: snipped out login details

6 Years
Discussion Span
Last Post by sid78669

Are those your real login details? If yes, please edit them out.

Regarding timeout, are you able to ping the given host from your machine (ping tribalwars.db.4489877.hostedresource.com)? Also, what happens when you comment out the DriverManager.setLoginTimeout(100) line? I don't see a port number mentioned in there; is the db in consideration running on default MySQL port?


Changed my passwords since the edit timeout has occured and I can't change it :(

Also, nothing happens when the timeout code is commented. The DB is running on the default port and I tried putting that in as well.

This is the result from the ping:

Pinging tribalwars.db.4489877.hostedresource.com [] with 32 bytes
of data:
Request timed out.
Reply from bytes=32 time=137ms TTL=53
Reply from bytes=32 time=136ms TTL=53
Reply from bytes=32 time=134ms TTL=53

Ping statistics for
Packets: Sent = 4, Received = 3, Lost = 1 (25% loss),
Approximate round trip times in milli-seconds:
Minimum = 134ms, Maximum = 137ms, Average = 135ms

Edited by sid78669: n/a


Is there a way to make it retry the connection? I set the timeout to 500Secs and it still timesout in like 5-7 secs...


> Is there a way to make it retry the connection

Automatic retries? Not that I'm aware of. Of course, it's dead simple to just loop over for a given number of times before giving up completely.

> I set the timeout to 500Secs and it still timesout in like 5-7 secs...

The timeout exception need not necessarily be of the client socket (in our case the socket used by the Driver). Server sockets can also have timeouts which might be the reason why you are getting time outs (which again is not surprising given timeouts in your ping). Are you able to connect to the database in consideration using some sort of SQL explorer like workbench?


I have used the MySQL GUI client to setup the database, so I'd say yes, I'm able to connect.

I Also tried to loop the try-catch block but got the same error twice :p


I was just going over my hosts description of details and this is what they say i need to connect to the server:
Host Name or Server
The name of the server your database is stored on. Don't know what this is? See Locating Your Database's Host Name for more information.

If asked for the port, enter the following:
MySQL: 3306
MSSQL: 1433

User Name
The database's user name.

The database's password. The password is created when the database is first set up but can be changed at any time.

The database you want to connect to.

How do I send the Database name in my connection string?


Din't work... I changed the connection string to this:

String connectionUrl = "jdbc:mysql://tribalwars.db.4489877.hostedresource.com:3306/tribalwars";

Anyways, I have also posted a ticket with my host incase it is something on their side...


SO it was a problem with my host. Still thanks SOS for helping me out!!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.