0

Using the mysql-connector-java.5.1.26-bin.jar to connect my android app to a remote mysql host. Problem is, anything above minSdkVersion 9 casuses the "Communications link failure". If I have it set to 9, it connects fine and gets database information. Here's my connection code:

try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection(url, user, pass);

        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery("select * from users");
        ResultSetMetaData rsmd = rs.getMetaData();
        while(rs.next()) {              
           ....
        }
    }
    catch(Exception e) {
        e.printStackTrace();
    }

Any ideas as to why this is happening?

  • This is not the recommended way of communicating between a Android device and a MySQL server. There is lots that can go wrong. See this link for suggestions: http://stackoverflow.com/questions/10679588/android-access-to-remote-sql-database/10682780#10682780 – Namphibian Oct 14 '13 at 22:31

2 Answers2

0

Turns out the problem was that I was trying to do a network connection in the main activity. For those that might have the same problem, look into using asynctask to complete the connection.

http://developer.android.com/reference/android/os/AsyncTask.html

0

Here is my Development Environment:

  • OS: Ubuntu14.04
  • IDE: Eclipse Luna
  • DB: MySQL5.1.73

Firstly,I put the code below in OnCreate method:

Class.forName("com.mysql.jdbc.Driver").newInstance();       
Connection conn = DriverManager.getConnection(url, username,
                        password);
Statement stmt = conn.createStatement();            
String sql = "select distinct Action from attributes";
ResultSet rs = stmt.executeQuery(sql);

The connection code is OK, But It will occur:

communication link failure.The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

So it must be wrong in something else. After trying various method, and solve it using android AsyncTask. Just put the code relative to operating mysql in Class that extends AsyncTask. Below is my code:

private class Connect extends AsyncTask<String, Void, Void> {

        @Override
        protected Void doInBackground(String... urls) {
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                Connection conn = DriverManager.getConnection(url, username,
                        password);
                Statement stmt = conn.createStatement();
                String sql = "select distinct Action from attributes";
                ResultSet rs = stmt.executeQuery(sql);

                List<String> actions = new ArrayList<String>();
                while (rs.next()) {
                    actions.add(rs.getString("Action"));
                }
            } catch (Exception e) {
                Log.d("MySQLConnection", e.getMessage());
            }
            return null;
        }

    }
xautjzd
  • 307
  • 1
  • 11
  • Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – chappjc Mar 03 '15 at 01:38