0

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;
    }
}
1
  • Please check whether you have the correct path in your JAR libraries for this purpose (it has to be within the project folder) Commented Feb 13, 2014 at 19:33

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

6 Comments

That's not the problem. Seems like OP gets the connection through DriverManager.getConnection(url1, user, password); thus not need to open the connections manually =\
The problem is DriverManager.getConnection represents handling a connection from your java client code: bad idea in a client application, since it will exhaust your db resources with enough open clients. Just fair warning! :) And try that Class.forName with the name of your driver; it will get your code running
In fact, trying to connect an android device directly with a database engine like MySQL is a bad idea to begin with... you should do it through a service layer from an external resource like REST web services in another server running the services in PHP, Java or another platform. Anyway, assuming the jar to connect with mysql is in the build path, adding this line will make it work.
@LuiggiMendoza I know it's a bad idea but it's the only way I have really since I have to show this project on the school's computer so there is not way for me to create a PHP page to get the mysql data from, anyways the line worked, thanks.
@Shay you could just start a jetty or tomcat application server to produce the web service and keep everything Java-ish.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.