I made a Java program which accesses a database. During the development phase I used a local database (XAMPP with MySQL), but when I tried to access 2 different online databases (a commercial and a free one), I received in both cases the following exception:
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.
I hadn't any problems acessing them with PHP.
This is my code:
private String user = "user1";
private String pass = "pass1";
private String dbClass = "com.mysql.jdbc.Driver";
private String dbDriver = "jdbc:mysql://db4free.net:3306/DBNAME";
private Connection conn = null;
public boolean connect() {
boolean done = false;
//load driver
try {
Class.forName(dbClass).newInstance();
System.out.println("driver loaded"); // THIS IS BEING RETURNED
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
System.err.println(ex);
}
// Connection
try {
conn = DriverManager.getConnection(dbDriver, user, pass);
System.out.println("connected"); // THIS IS NOT BEING RETURNED
done = true;
} catch (SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());
}
return done;
}