Shay
Shay

Reputation: 1405

MySQL working with Java but not Android

I have a remote MySQL server that I want to connect to from my android device, I've tested the code on a regular java console application and it works perfectly fine

but when I run it on my Android emulator it throws SQLException: No suitable driver.

The code is the same, the MySQL details are the same, I used the same mysql jar for both of them, I'm clueless really.

Android code:

public class MySQL_handler {
    Connection con = null;
    Statement st = null;
    ResultSet rs = null;


    String url1,user,password;


    Thread thread;
    Runnable runnable;

    String data = "";

    public MySQL_handler() {
        user = "xx";
        password = "xx";
        url1 = "xx";

    }

    public String test(){
        final CountDownLatch latch = new CountDownLatch(1); 
        runnable = new Runnable() {

            @Override
            public void run() {

                try
                {
                    Log.v("S", url1);
                    Log.v("S", user);
                    Log.v("S", password);
                    con = DriverManager.getConnection(url1, user, password);
                    st = con.createStatement();
                    rs = st.executeQuery("SELECT CompName FROM Servers WHERE HostIP = '10.0.0.4'");

                    if (rs.next())
                    {
                        Log.v("sqlver",""+ rs.getString(1));
                        data = rs.getString(1);
                    }
                    else{
                        Log.v("sqlver","err");
                    }

                }
                catch (SQLException ex)
                {
                    Log.v("SQLEX", ".."+ex);

                }
                finally {
                    try
                    {
                        if (rs != null)
                        {
                            rs.close();
                        }
                        if (st != null)
                        {
                            st.close();
                        }
                        if (con != null)
                        {
                            con.close();
                        }
                        latch.countDown();

                    }
                    catch (SQLException ex)
                    {
                        Log.v("SQLEX", "...");
                        latch.countDown();
                    }
                }

            }
        };
        thread = new Thread(runnable);
        thread.start();
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return data;
    }
}

Upvotes: 0

Views: 57

Answers (1)

Jorge_B
Jorge_B

Reputation: 9872

Try loading your driver before

Class.forName("here your driver class name");

By the way, I must warn you that it is not the best practice in production to try and open a database connection from a mobile device (but go ahead if for an academic exercise)

Upvotes: 1

Related Questions