0

How to connect online mysql database to a java application?I have used below code to get the connection to my database.

private static Connection con;
        public static Connection connect() throws Exception {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        String url = "jdbc:mysql://host/database";
        con = DriverManager.getConnection(url, "username", "password");
        return con;
    }

but this gives me following error everytime.

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.

What should i do?

Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
Hussey123
  • 479
  • 1
  • 5
  • 21
  • May be you need to edit my.cnf to change bind-address to 0.0.0.0. Check this out http://stackoverflow.com/questions/6865538/solving-a-communications-link-failure-with-jdbc-and-mysql – sakthisundar Jul 03 '15 at 03:17
  • In your actual code, are you using the correct host, database, username, and password or is the code your posted above the exact version you're using? Does the MySQL server accept network connections from an outside client (most hosting providers block this, for instance); is there a firewall or other network block on the MySQL port; does your user have proper permission for accessing from whatever hostname you're connecting from? – Isaac Bennetch Jul 07 '15 at 00:01
  • Also, I'm not sure what this has to do with phpMyAdmin? – Isaac Bennetch Jul 07 '15 at 00:01
  • I have tested your code it does work, in addition to @isaacbennetch, there are 3 more things you should check as far as I know; 1. try to added port number to your localhost:3306 2. does your host/database or host:3306/database return some thing in browser at all 3. have you download mysql J driver in your library. let me know for further feedback – Maytham Fahmi Jul 15 '15 at 04:23

0 Answers0