简体   繁体   中英

Java - trouble connecting to online MySQL database using JDBC

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.

I've had this problem too and it's always (in my case) been down to an incorrect jdbc url, eg 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

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM