Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect Android with MySQL using Mysql JDBC driver [duplicate]

I want to get data from table in MySQL and to show it in TextView, but I have some problems when I try to connect with database.
I'm using Eclipse for Android, and when I try to get data from MySQL in Java Project it works, but when I use Android Project it doesn't work.
Does anyone know how can I connect MySQL with Android Project using MySQL JDBC driver?
Or to give me other advice how to connect Android Project with MySQL?

like image 552
Balsa Bojic Avatar asked Jun 19 '12 16:06

Balsa Bojic


People also ask

Can I use JDBC in Android?

The JDBC API is an alternative to the drop-in replacement. It is possible to build Berkeley DB SQL for Android in such a way that a JDBC API is exposed to Android application developers. This is done using the Android NDK. This section describes how to build and use the BDB JDBC driver for Android.

Can Android app connect to MySQL database?

This is very useful in case you have a webserver, and you want to access its data on your android application. MYSQL is used as a database at the webserver and PHP is used to fetch data from the database.

How do I fix no suitable driver found for JDBC MySQL?

In brief, we can say that such an error occurs when no JDBC JAR file is added to the classpath of Java. Just we need to add the JAR file to the classpath and then execute the code. The code will hopefully get executed with success.

Which JDBC driver is used for MySQL?

Driver in MySQL Connector/J is com. mysql. cj. jdbc.


2 Answers

If you want to connect to Mysql database from Android you just need to follow these steps:

  • Download the driver mysql-connector-java-3.0.17-ga-bin.jar
  • Paste it to the folder libs in your android project.
  • Click ConfigureBuildPath->add jar to include the jar to the project.
  • Now you have the driver, but you also need to give permissions in the androidManifest.xml for INTERNET.
  • Use the next code for connecting:

    try{
        Class.forName("com.mysql.jdbc.Driver").newInstance();
    }catch(Exception e){
        System.err.println("Cannot create connection");
    }
    try{
        connection = DriverManager.getConnection("jdbc:mysql://192.168.xx.xx:3306/dbname","root","password");
        Statement statement = connection.createStatement();
    
        String query = "SELECT column1, column2 FROM table1 WHERE column3 = ";
        query = query +"'" +variable+"'";           
        ResultSet result = statement.executeQuery(query);
    }catch(Exception e){
        System.err.println("Error");
    } 
    

Advice: If the instance of the drivers doesn't give any errors but you get an exception with the connection you should try to remove the Target SDK version from your manifest, as for some versions this gives problems.

like image 166
Jav_Rock Avatar answered Sep 28 '22 20:09

Jav_Rock


Android by default does not support MySQL. It has an in-built database i.e SQLite. If you are trying to access MySQL database remotely, you should expose interface to this database with any standard web service. E.g you could create RESTful Web Service on Server Side which is written using Java/PHP etc. and MySQL Connector. (Which you have already done!) And your Android App could talk to this service with the URL generated using this web service.

Again, this question has been repeated previously, so you can check those solutions.

like image 28
saurcery Avatar answered Sep 28 '22 19:09

saurcery