Home » Android » java – Android JDBC not working: ClassNotFoundException on driver

java – Android JDBC not working: ClassNotFoundException on driver

Posted by: admin June 15, 2020 Leave a comment


I’m trying to use JDBC in my Android application to connect to a remote database to do inserts, queries, etc. I have successfully connected and done these things in a different JAVA project. So I figured since Android is Java, I could just port over the relevant code, add the same build path for the driver, etc. But it gives me the error:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

I really don’t think it’s a code issue since the same code works in a Java project (which I just execute in main()). But for reference here it is:

String url = "jdbc:mysql://localhost:3306/eventhub_test"; //
    String user = "root"; 
    String pass = "";

 SQLUtils sqlu = new SQLUtils(url, user, pass);

//the SQLUtils class I made:

public class SQLUtils {

private String CONNECTION_URL;
private String user;
private String pass;
private java.sql.Statement stmt; 
private java.sql.Connection conn;

public SQLUtils(String conn_url, String user, String pass) {
    this.CONNECTION_URL = conn_url; 
    this.user = user;
    this.pass = pass; 

public void init() throws IllegalAccessException, InstantiationException, ClassNotFoundException, SQLException {

    Class.forName ("com.mysql.jdbc.Driver").newInstance ();
    conn = DriverManager.getConnection(CONNECTION_URL, user, pass);
    stmt = conn.createStatement();


So I’m really confused here. Does JDBC not work with Android? If so, tell me what alternatives I should look into for remote MySQL database access.


How to&Answers:

Does JDBC not work with Android?

JDBC is infrequently used on Android, and I certainly would not recommend it.

IMHO, JDBC is designed for high-bandwidth, low-latency, highly-reliable network connections (e.g., desktop to database server, Web application server to database server). Mobile devices offer little of these, and none of them consistently.

If so, tell me what alternatives I should look into for remote MySQL database access.

Create a Web service around your database and access that from Android.

As side benefits, you improve security (vs. leaving your database open), can offload some business logic from the client, can better support other platforms (e.g., Web or Web-based mobile frameworks), etc.


I had a TON of trouble with this for some reason.
It is mentioned elsewhere that only 3.017 driver works, and since I made such detailed instructions I figured I’d share them. (My initial purpose was to give steps to reproduce error that I could use to ask a question here and elsewhere. No, I can’t begin to guess why I had so much trouble now looking back)

To: Get JDBC Driver in Android app

  • New Android Project – AndroidJDBCTest, Target Android4.03, minimum sdk 8 (Android 2.2), package name “com.test.AndroidJDBCTest”
  • Right click on project, new folder “libs”
  • Download Mysql JDBC Driver from here and extract it to your filesystem.
  • Browse to the root directory after extracting and drag and drop the jar mysql-connector-java-3.0.17-ga-bin.jar into /libs in your project in project explorer inside Eclipse., using the default “copy” setting for the drag and drop.
  • Right click on the Eclipse Project, Build Path-Configure Build Path, Add JAR under libraries tab – Browse to /AndroidJDBCTest/libs and the jar file and click ok
  • NOTE: It shows up now under “Referenced Libraries” node (if Eclipse is set to show it)
  • Add code from here to the end of onCreate() – basically Class.forName("com.mysql.jdbc.Driver").newInstance();
  • Attach Honeycomb device such as Motorola Xoom family Edition and run


Leaving aside the valid objections, you can make it work.

Firstly make sure you have a valid driver jar in you build path, I used


Secondly, if you are trying it in the emulator and your db is on you development machine, ‘localhost’ is no good in the URL. You need ‘’

My URL is :

String url = "jdbc:mysql://";

test being my db name.

I’ve got a table called ‘books’ with a column ‘title’ in it

The following code works for me:

public void init() throws IllegalAccessException, InstantiationException,
        ClassNotFoundException, SQLException {

    try {
        conn = DriverManager.getConnection(CONNECTION_URL, user, pass);
    } catch (java.sql.SQLException e1) {
    try {

        stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT title FROM books");
        String entry;
        while (rs.next()){
            entry = rs.getString(1);
    } catch (java.sql.SQLException e) {


Stepping through in the debugger, I see the titles in String entry


In theory you should be able to do that. You need to include the MySQL JDBC driver Jar in your project to make your code work (here is a similar post with solutions).

But it’s probably not a good idea. It’s better if you expose the database contents through a dedicated (REST) API on your server.

They don’t really give reasons in the link, but one of the main reasons why it’s a bad idea to expose your database directly is because of security concerns. Most of your users will behave nice and do only what they are supposed to do with their phone, but imagine one doesn’t.

If he has a valid login for your database then he/she might simply connect to your database, do all kinds of SELECTs or might even change/destroy data if the permissions allow him to.

So that’s why you should have a web server inbetween that validates and sanitizes any requests coming from the user in order to ensure that only the data you intended to hand out can be retrieved from your MySQL database.


Make sure that you have placed the connector inside the library folder of the project.
place it there,it will solve the problem


If you’re developing using Android Studio,
Adding this line to your gradle dependency does the magic.

 compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.44'

And you can change the version according to your need.

After adding this, gradle sync will be taking care of everything, you just have to connect to DB in the same way as you do in a simple java jdbc program.


Provided arguments are not always correct. Some can think of embedded db’s drivers which work through JDBC. Examples are Apache Derby and H2.
This answer proves ability to use jdbc.


try changing inthe gradle file

 targetSdkVersion 8