Pergunta

I am trying to connect to my MySQL database in a new project that I am working on. The error message that I receive is: CommunicationsExceptions: Communications link failure. I have tried to connect to my friends database, and it is working correct, but as soon as I try with my own localhost server I get errors.

I have also tried to write the exact same code in a JAVAFX application in netbeans and it is working perfectly, it is like there is something wrong with android and localhosts..

Here is my code:

private Connection conn = null;
private String dbName = "world";
private String user = "root";
private String pwd = "root";
private Statement statement = null;

public void connectingDatabase() throws Exception {

    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        System.out.println("Found instance");
    } catch (Exception ex) {
        System.out.println("No instance ");
    }
    try {
        setConn(DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/" + getDbName() + "?user=" + getUser() + "&password=" + getPwd()));
        System.out.println("Connecta till " + getDbName());
    } catch (Exception ex) {
        System.out.println("no connection");
        System.out.println(ex);
    }
}

If I use my friends IP instead of 127.0.0.1:3306 or localhost it is working. Does someone have any expertise in this kinds of problems?

Foi útil?

Solução

Since the code is running on Android device and you want to connect to the MYSQL server running on your desktop, you cannot use 127.0.0.1 for connecting to the database. Instead use the ip address of the machine where MySQL database is hosted.

Outras dicas

If localhost or 127.0.0.1 is still not working. Assign a static IP on your computer/server i.e: 192.168.1.10 and then use that in your connection URL

Good luck.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top