0

I'm trying to connect my application to a remote server database with JDBC connector, but I get a connection error each time.

ConnexionBDD.java

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

public class ConnexionBDD {

private static final String dbURL = "jdbc:mysql://**my_server_ip_address**/**my_db_name**:3306?useUnicode=yes&characterEncoding=UTF-8&connectTimeout=3000";
private static final String user = "**my_username**";
private static final String password = "**my_password**";
private static Connection connexion = null;

/**
 * Connexion a la base de donnees; chargement du driver
 * 
 * @throws ClassNotFoundException
 * @throws SQLException
 */
private ConnexionBDD() throws ClassNotFoundException, SQLException {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        try {
            connexion = DriverManager.getConnection(dbURL, user, password);
            System.out.println("Connexion BDD Ok");
        } catch (SQLException e) {
            System.out.println("Erreur Connexion BDD : " + e.getMessage());
        }
    } catch (ClassNotFoundException e) {
        System.out.println("Erreur Chargement Driver" + e.getMessage());
    }
}

/**
 * Recuperation de la connexion
 * 
 * @return la connexion
 * @throws ClassNotFoundException
 * @throws SQLException
 */
public static Connection getConnexion() throws ClassNotFoundException, SQLException {
    if (connexion == null) {
        new ConnexionBDD();
    }
    return connexion;
}
}

When I try to call the getConnexion() method :

Connection connexion = ConnexionBDD.getConnexion() ;

I'm getting this error as output :

Erreur Connexion BDD : 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.

Note that when I try to connect on a local server it works perfectly, but not on a remote server..

Kara
  • 6,115
  • 16
  • 50
  • 57
  • Maybe is the connection url! Did you put the corret port and database? And the host url, can you ping it? Could it be a firewall behind you and the server!!? – Fausto Carvalho Marques Silva Sep 28 '15 at 14:50
  • Check if you can reach the server from command prompt ? by using ping and nmap tools ? – Abhiram mishra Sep 28 '15 at 14:51
  • Try to change `Class.forName("com.mysql.jdbc.Driver");` to `Class.forName("com.mysql.jdbc.Driver").newInstance();` – Alucard Sep 28 '15 at 14:53
  • @TheNeoNoirDeveloper Actually the ping command is blocked by my server, but when I put the URL in a web browser it works. – DeveloppeurDuTurfu Sep 28 '15 at 15:07
  • @Alucard I tried but it doesn't work aswell :/ – DeveloppeurDuTurfu Sep 28 '15 at 15:09
  • @FaustoCarvalhoMarquesSilva Yeah I put the correct port and database, as I said it works when I try on a local server, but not on a remote one – DeveloppeurDuTurfu Sep 28 '15 at 15:11
  • System.out.println("Erreur Connexion BDD : " + e.getMessage()); is never a good idea, do e.printStackTrace(); It would give a little something we can understand : – Abhiram mishra Sep 28 '15 at 15:14
  • @TheNeoNoirDeveloper I replaced this line as you said and got this message : `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.` – DeveloppeurDuTurfu Sep 28 '15 at 15:17
  • Hmm, it should give us a full stack trace. But here is something you can try .http://stackoverflow.com/questions/6865538/solving-a-communications-link-failure-with-jdbc-and-mysql – Abhiram mishra Sep 28 '15 at 15:19
  • @TheNeoNoirDeveloper I'll try this out ! – DeveloppeurDuTurfu Sep 28 '15 at 15:25
  • The correct syntax is `jdbc:mysql://hostname:3306/dbname?...`. But 3306 is the default, so it doesn't matter you've left it off the host, and it only causes a problem on the database after you connect to the server. Try `telnet host 3306` as a test; decades ago IP connectivity with `ping` and all TCP and UDP was the same but not today. – dave_thompson_085 Sep 29 '15 at 06:35

0 Answers0