Java - problem connecting to MySQL online database using JDBC

I am trying to establish a connection in my Java application with JDBC to access my online database so that I can insert and query tables. Here is what I'm trying to do now: (the actual IP / user / pass has been edited, but they are right, since I did a similar script with PHP)

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 does not work, I get an error message:

 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 quite long after that.

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

Thanks for any help.

+6
source share
2 answers

I had this problem too, and it always (in my case) was up to the wrong jdbc url, for example. missing / incorrect port number (I know that your code sample has been changed, but it does not indicate any port) or the wrong IP address, otherwise check that mysql is running and accepts the connections on the port and ip that you expect . Check the binding address in my.cnf for the ip address in jdbc url

+2
source

Answering my own question here, because my code was fine. The problem was in my Godaddy settings.

Basically, when you create a MySQL database with these guys, make sure that โ€œAllow direct access to the databaseโ€ is set to โ€œYESโ€. Apparently my other database was not there.

I will not answer the question if others have any data. These configuration things always give me ...

+1
source

Source: https://habr.com/ru/post/896012/


All Articles