4

I want to connect to remote sql database via jdbc I work on Android Studio But when executing I got this error

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

So How can I add The JDBC Driver to Android Studio ??

user3814438
  • 61
  • 1
  • 1
  • 3
  • 1
    Please reconsider this. JDBC is not designed for use this way. In particular, you will wind up having to have database account data, including passwords, in your app, which is bad from a security standpoint. – CommonsWare Jul 16 '14 at 16:50
  • 1
    I don't know why people talk about this stupid security issue all the time. The OP could be writing a MySQL client, but everyone just presumes the OP doesn't notice such obvious security problem. – SOFe Nov 29 '17 at 13:37

2 Answers2

15
  1. Download JDBC and extract the .jar file
  2. Open Android Studio and your project of interest
  3. Open your Left Panel, in the 1.Project Tab
  4. Right Click on your application module folder and go to New > Module and there Select Import .JAR/.AAR Package.
  5. In the first field you must insert the directory of the previously extracted .jar file. You can browse your folders by clicking on the far right icon with the 3 dots (...)

Once you have selected the .jar file and your project Synchronizes you can move on to the next steps:

  1. Right Click again on your application module folder and select Open Module Settings
  2. In the left navigation pane of the newly opened window, go to the category Modules and select app and then the Dependencies tab.
  3. There click on the green plus (+), and select Module Dependencies. Select the mysql module.
  4. Click OK, and close all the opened windows. A Synchronization process should take place once more.
  5. If everything has been done correctly, under your app module folder you should see a mysql-connector-java module folder.
  6. Don't forget to include the classes you want in your code.

Troubleshooting
In my case I had to uninstall the module and re-install it. The reason was that the module was compiled with Java 1.8 instead of 1.7 (currently supported by Android Studio ver:1.5.1).

Rapsoulis
  • 232
  • 5
  • 13
0

You can see the official site of MySQL to download the [JDBC][2].

Then you only need to add the library to Android Studio like any other library.

Michael Kohne
  • 11,888
  • 3
  • 47
  • 79
AlonsoFloo
  • 505
  • 3
  • 10