6

I'm trying to establish a connection in my Java app with JDBC to access my online database so I can insert and query tables. Here is what I am currently trying: (actual IP/user/pass edited, but they're right since I've done similar from a PHP script)

String url = "jdbc:mysql://984.168.199.70/my_db_name"; 
    String user = "my_username"; 
    String pass = "my_password";

Class.forName ("com.mysql.jdbc.Driver").newInstance ();
    Connection conn = (Connection) DriverManager.getConnection(url, user, pass);
    stmt = (Statement) conn.createStatement();

But this doesn't work, I get the error:

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

And the error log is pretty long after that.

So just to point out: I have connected to this server before using a PHP script, and I have used JDBC to connect to and interact with my localhost MySQL databases.

Thanks for any help.

JDS
  • 16,388
  • 47
  • 161
  • 224
  • Any special port opened up for the MySQL, or it's the default? – sgibly Aug 26 '11 at 22:36
  • Also, look at http://stackoverflow.com/questions/2102912/cant-make-jdbc-connection-to-mysql-using-java-intellij-and-linux – sgibly Aug 26 '11 at 22:38
  • 1
    As to the concrete problem, check http://stackoverflow.com/questions/2983248/jdbc-with-mysql/2985169#2985169 As to the code you've posted as far, those unnecessary casts makes me think you actually imported MySQL's concrete implementation classes for `Connection` and `Statement`. While not necessarily the cause of your problem, this is a *bad practice*. Always declare against `java.sql.*` interfaces! If you ever upgrade the driver or switch the DB, your JDBC code would otherwise break and need a complete rewrite/rebuild. – BalusC Aug 26 '11 at 23:00
  • oh, I have variables like private Statement stmt as part of my class, as well as the java.sql.* imports. Am I missing something? – JDS Aug 26 '11 at 23:11
  • That's fine. Remove those misleading unnecessary casts then. Again, it's not related to your concrete problem. See the duplicate link. – BalusC Aug 26 '11 at 23:13
  • oh, well Eclipse made me put them in for some reason (and gave me an error otherwise...) – JDS Aug 26 '11 at 23:17

2 Answers2

2

I've had this problem too and it's always (in my case) been down to an incorrect jdbc url, e.g. missing/wrong port number (i know your sample code has been editted but it doesn't specify any port) or incorrect ip address, failing that check that mysql is running and accepting connections on the port and ip you're expecting. Check the bind-address in your my.cnf against the ip address in your jdbc url

  • I'm not sure what (if any) port I should specify... The server I have is hosted on Godaddy.com, which is never easy to deal with – JDS Aug 26 '11 at 23:18
  • the default port is 3306, so your jdbc url should be like jdbc:mysql://84.168.199.70:3306/my_db_name then as BalusC said remove the (Connection) casts. Quick google showed this, might help http://help.godaddy.com/article/262 –  Aug 26 '11 at 23:27
  • thanks, but still no luck. The annoying thing is that a basic PHP script with the same IP/username/password works perfectly. Is Godaddy not allowing Java to have access or something? – JDS Aug 27 '11 at 00:28
  • if you are trying to connect to a mysql server at godaddy from your home pc it most likely won't work due to firewalls, for example i can't imagine port 3306 being accessible from the outside, so question is where is the client, inside godaddy network? also you mention a PHP script that works, where is the PHP script running? –  Aug 27 '11 at 00:35
  • I think I resolved the issue - see my answer below. I'm not sure if that's the right answer or if I just got lucky, but it seems to have worked. – JDS Aug 27 '11 at 00:45
1

Answering my own question here, because the code I had was fine. The problem was with my Godaddy settings.

Basically when you're making a MySQL database with these guys, make sure you have "Allow Direct Database Access" set to YES. Apparently my other database wasn't.

I'll still leave the question unanswered for a while in case other people have any input. These configuration things always get me...

JDS
  • 16,388
  • 47
  • 161
  • 224
  • i am facing exactly same. I am using the free service of 000webhost.com for that. Same error? Any ideas how can trouble shoot or find the "allow direct access" to the database? – Nitesh Verma Jul 07 '13 at 17:43