1

I know that this issue has been discussed a lot of times but I cannot establish a connection between my Java app and MySQL server.

I'm using code form tutorials:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JDBCTest{
  private Connection conn = null;
  public void connect(){
    try {
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      System.out.println("test");
      conn = DriverManager.getConnection("jdbc:mysql://mysql.***.***.**:3306/******","*******","top_secret_password");
      System.out.println("test2");


    } catch (SQLException ex) {
      // handle any errors
      System.out.println("SQLException: "   ex.getMessage());
      System.out.println("SQLState: "   ex.getSQLState());
      System.out.println("VendorError: "   ex.getErrorCode());
    }catch(Exception e){e.printStackTrace();}   
  }

}

Error that occured is:

test
SQLException: 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.
SQLState: 08S01
VendorError: 0

Have you got any ideas how fix it?

ruhungry
  • 4,506
  • 20
  • 54
  • 98
  • 1
    Looks like an exact duplicate of http://stackoverflow.com/questions/2121829/java-db-communications-link-failure – Barend Mar 31 '12 at 07:25

1 Answers1

2

Try to provide the IP-address of the database instead of its name. Say

conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/*******","*******","top_secret_password");

and make sure that your firewall doesn' block 3306 port

ruhungry
  • 4,506
  • 20
  • 54
  • 98
Alex Stybaev
  • 4,623
  • 3
  • 30
  • 44