2

I'm trying to connect to mysql database from my android application.

I'm getting Communications link failure error.
Below is the code snippet:

public class MySqlConnector {
    private Connection con = null;
    private String s = "";
    private String username = "root";
    private String password = "password01";
    private String connectionString;

    public String ConnectToDb() {
         connectionString ="jdbc:mysql://192.168.1.104:3306/mydatabase";
        //connectionString="jdbc:mysql://10.0.0.0:3306/mydatabase";
        // connectionString="jdbc:mysql://127.0.0.1:3306/mydatabase";
        //connectionString = "jdbc:mysql://MainSrv04:3306/mydatabase";
        // connectionString="jdbc:mysql://localhost:3306/mydatabase";

        // connectionString =
        // "jdbc:mysql://localhost:3306/mydatabase?user=root&password=password01&useUnicode=true&characterEncoding=UTF-8";

        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        try {
            con = DriverManager.getConnection(connectionString, username,
                    password);

            Statement st = con.createStatement();
            String sql = "SELECT First_Name FROM mydatabase.custinfo where CardNumber=5325784707";
            ResultSet rs = st.executeQuery(sql);
            s = rs.getString("First_Name");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (con != null) {
                try {
                    con.close();
                    Log.i("MySqlConnector", "Database connection terminated");
                } catch (Exception e) { /* ignore close errors */
                }
            }
        }

        if (s == "") {
            s = "No Result";
        }
        Log.i("MySqlConnector : s=", s);

        return s;
    }
}

I tried all possible combinations as showed in comments and getting following error in caused by field in logcat:

  • while using localhost --> Caused by: java.net.ConnectException: localhost/127.0.0.1:3306 - Connection refused
  • while using 127.0.0.1 --> Caused by: java.net.ConnectException: /127.0.0.1:3306 - Connection refused
  • while using 10.0.0.0 --> Caused by: java.net.SocketException: The operation timed out
  • while using 192.168.1.104 --> Caused by: java.net.SocketException: The operation timed out
  • while using MainSrv04 --> Caused by: java.net.UnknownHostException: MainSrv04

I also pinged mysql port through telnet and it's working.
Also, I have taken care of privileges.

But still I'm getting Communications link failure error.

Any help appreciated.

GAMA
  • 5,958
  • 14
  • 79
  • 126
  • So what host/port *is* your MySQL on? Is the Android device on the same network? – Anders R. Bystrup Jan 07 '13 at 13:22
  • 3306. I've mentioned that in the `connectionString` variable. – GAMA Jan 07 '13 at 13:23
  • what happens if you use this one `jdbc:mysql://localhost:3306/mydatabase?user=root&password=password01`? – John Woo Jan 07 '13 at 13:24
  • have you seen this link? [MySQL and Java JDBC - Tutorial](http://www.vogella.com/articles/MySQLJava/article.html) – John Woo Jan 07 '13 at 13:25
  • Does your MySQL server allow remote connections? – Georgian Jan 07 '13 at 13:27
  • @JW. : if I write `jdbc:mysql://localhost:3306/mydatabase?user=root&password=password01`, I get `Connection refused`. – GAMA Jan 07 '13 at 13:28
  • @JW. : that vogella tutorial suggests similar to what I have done. But it uses only `localhost` (without port number). – GAMA Jan 07 '13 at 13:31
  • @GGeorge : how to check whether mysql server allows remote connections? – GAMA Jan 07 '13 at 13:32
  • Have you confirmed your ability to connect, using the `mysql` command line client (mysql --host=localhost --user=root --password=password01)? – Perception Jan 07 '13 at 13:32
  • @GAMA See Perception's response. :) Is your server locally or remotely? – Georgian Jan 07 '13 at 13:36
  • locally. And I have also allowed remote connections (if it means granting privileges)? – GAMA Jan 07 '13 at 13:39
  • by using `GRANT ALL PRIVILEGES ON *.* TO 'root'@192.168.1.104 IDENTIFIED BY 'password01';` – GAMA Jan 07 '13 at 13:39
  • @Perception & GGeorge : **YES**, when i use above credentials, it's giving desired output on command line. – GAMA Jan 07 '13 at 13:53
  • Also, I didn't find any `my.cnf` or `my.ini` in MySql directory. So I created `my.cnf` at *C:\Program Files\MySQL\MySQL Server 5.5\bin* manually. I set `bind-address="127.0.0.1"` and commented `skip-networking`. Is it ok? – GAMA Jan 07 '13 at 14:01

2 Answers2

2

Is android application is on same ntework? If not

jdbc:mysql://192.168.1.104:3306

and

jdbc:mysql://10.0.0.0:3306

will not work.

Give the proper network address of mysql.

code_fish
  • 3,381
  • 5
  • 47
  • 90
  • Please have a look at question. I've tried 5 different combinations for specifying network address and corresponding issue. – GAMA Jan 08 '13 at 05:46
  • if application is on the same address then 192.168.104 approach should work fine. You should look at this post in that case http://stackoverflow.com/questions/585599/whats-causing-my-java-net-socketexception-connection-reset – code_fish Jan 08 '13 at 06:02
  • m getting `Caused by: java.net.SocketException: The operation timed out` if I use `192.168.1.104:3306`.... – GAMA Jan 08 '13 at 07:21
  • 1
    `connectionString = "jdbc:mysql://192.168.1.104:3306/dbname?user=root&password=password01";` worked... – GAMA Jan 10 '13 at 12:51
1

I use latest version of adt and mysql-connector-java-5.1.17-bin.jar, this works for me :

Add the below code into your file after the line setContentView(R.layout.activity_main);

if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}

and use import android.os.StrictMode;

If you use new api, add @SuppressLint("NewApi");

GAMA
  • 5,958
  • 14
  • 79
  • 126
tjenen
  • 31
  • 2