2

I've seen few threads about this topic but I still can't figure out what's wrong. Following is the code:

import java.sql.*;

public class SQL
{
    public static void main(String[] args) 
    {
        Connection conn = null;
        String url = "jdbc:mysql://mysql1.oyo.co.il:3306/";
        String dbName = "sdarot2_winner";
        String driver = "com.mysql.jdbc.Driver";
        String userName = ""; 
        String password = "";
        String table = "LEADER_CAM_PRODUCTS";

        try {
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(url+dbName,userName,password);
            System.out.println("Connected to the database");
            conn.close();
            System.out.println("Disconnected from database");

        }catch (Exception e) {
            System.out.println(e);
        }
  }
}

And here is the error I get:

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.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(Unknown Source) at java.net.PlainSocketImpl.connectToAddress(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.(Unknown Source) at java.net.Socket.(Unknown Source) at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256) at com.mysql.jdbc.MysqlIO.(MysqlIO.java:271) at com.mysql.jdbc.Connection.createNewIO(Connection.java:2744) at com.mysql.jdbc.Connection.(Connection.java:1553) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285) at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at SQL.main(SQL.java:17)

** END NESTED EXCEPTION **

Last packet sent to the server was 1 ms ago.

Here is few things I found around:

  1. The I.P/domain or port is incorrect
  2. The I.P/domain or port (i.e service) is down
  3. The I.P/domain is taking longer than your default timeout to respond
  4. You have a firewall that is blocking requests or responses on whatever port you are using
  5. You have a firewall that is blocking requests to that particular host
  6. Your internet access is down

a) it is correct. b) it's not down since I got a website that is working right now with that database. c) how can I check that?I don't think that it's the problem since as I said I got a running website on this database. d) now way because the same reason I mentioned above. e) samem as d

So what I'm doing wrong?how come a php code can work with sql but java gives me errors? Thank you.

Bilesh Ganguly
  • 3,792
  • 3
  • 36
  • 58
Imri Persiado
  • 1,857
  • 8
  • 29
  • 45
  • check out here http://stackoverflow.com/questions/86824/why-would-a-java-net-connectexception-connection-timed-out-exception-occur-wh – PermGenError Dec 05 '12 at 09:16
  • Have you tried a command line mysql client from the same machine the code is failing in? That would let you check for firewalls and timeouts – Miquel Dec 05 '12 at 09:19
  • @GanGnaMStYleOverFlowErroR in the thread you posted the guy mentioned reasons to the exception but not a solution. I need a solution :D – Imri Persiado Dec 05 '12 at 09:37

2 Answers2

3

As an experiment, try connecting to port 3306 on mysql1.oyo.co.il using your system's telnet command. Or some other utility (for example nc or netcat) that opens a raw TCP/IP connection. Depending on your OS / distro, you may have to locate and install a suitable command to help you with this trouble-shooting. Observe what happens.


Re your diagnosis:

a) it is correct.

If you say so ...

b) it's not down since I got a website that is working right now with that database.

At best you have demonstrated that the host is working. The web service on port 80 and the MySQL service on port 3306 are different services. (And in fact, it is possible that mysql1.oyo.co.il is doing clever packet routing tricks so that traffic on different ports is being tranparently routed to different hosts or virtual hosts.)

c) how can I check that?

Try changing / increasing the timeout.

I don't think that it's the problem since as I said I got a running website on this database.

My guess it is not a timeout issue ... but as I said above, the fact you got a website says NOTHING about whether the MySQL service is running.

d) now way because the same reason I mentioned above.

You've only demonstrated that you can get to port 80 ... see above

e) samem as d

You've only demonstrated that you can get to port 80 ... see above


Based on the above, it is plausible that:

  • you have a local firewall problem,
  • the MySQL service on that port is not currently running,
  • networking routing on the service side are broken for traffic on that port,
  • the MySQL service has been permanently turned off, or
  • the service's firewall has been configured to not accept network connections from random (or specific) places, including yours.

The fact that you are seeing a timeout rather than a "connection refused" suggests that this is more likely to be a networking or firewalling issue than a problem with the MySQL service endpoint itself.

My guess is that "they" have withdrawn the service ... and that you are not supposed to use it anymore. (Providing a public MySQL database service strikes me as an odd thing to do ... as well as being problematic from a security and administration standpoint.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thanks for opening my eyes. I'm trying to use the telnet command but it sais that it's not a recognized command, and with ping I can't define a port. which command to use to check that? – Imri Persiado Dec 05 '12 at 10:11
  • I tried this website:http://www.yougetsignal.com/tools/open-ports/ for port 80 it sais online but for 3306 offline. is it trustable? – Imri Persiado Dec 05 '12 at 10:13
  • @ImriPersiado - I've no idea if it is trustable. What operating system are you using? If you are using Linux you can install `telnet` ... or `nc` ... from your yum / apt-get repo. For windows: http://joncraton.org/blog/46/netcat-for-windows – Stephen C Dec 05 '12 at 12:43
  • Thanks guys, the reason is that the port is closed since it's a shared server. thank you both. – Imri Persiado Dec 07 '12 at 00:59
1

'Connection timed out' indicates either:

  • a server-side firewall that is deliberately ignoring your connection request
  • a network topology problem such that IP packets aren't getting through
  • the server host itself is down.

Most likely it is the first.

Note that, contrary to other answers here, it doesn't indicate that the server program is down, or a local firewall problem at the client. The former would cause 'connection refused' and the latter would give you something along the lines of 'permission denied', depending on your platform.

user207421
  • 305,947
  • 44
  • 307
  • 483