The error I am getting is java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
even though I have already added the .jar file by going to file -> project structure -> import .JAR Package.
What could be the issue here?
Image of module inside project structure:
-
have you tried this Connecting to MySQL from Android with JDBC: http://stackoverflow.com/questions/12233145/connecting-to-mysql-from-android-with-jdbc – Lay Leangsros Nov 27 '15 at 09:10
-
1Android studio cannot load the JDBC driver to connect to anything, that is my issue. Even though I have added the jar file, it is not working and I am getting a class not found exception. – Mike Nov 27 '15 at 09:33
3 Answers
Try putting jar file in your lib directory
, then re-starting tomcat...
problem is Class.forName("com.mysql.jdbc.Driver");
it tries to load the driver, but it is not getting it, this is the reason you are getting java.lang.ClassNotFoundException.
check that your jarfile should be in class path

- 505
- 1
- 4
- 12
-
1Okay I've removed the module `mysql-connector-java-5.1.37` and I've added the .jar file directly into my libs folder in this path `project -> app -> libs` and now I am getting a new error `Error:Execution failed for task ':app:transformClassesWithDexForDebug'. > com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_45\bin\java.exe'' finished with non-zero exit value 1` – Mike Nov 27 '15 at 09:29
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
Why are you trying to access MySQL DB from Android native? First it might work but it is not recomended. You can do it easily with PHP
and JSON
responses.
Check those:
http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql http://www.tutorialspoint.com/android/android_php_mysql.htm http://sampleprogramz.com/android/mysqldb.php
You can also download my project with HttpURLConnection
since org.apache
library has been deprecated.

- 3,222
- 6
- 28
- 60