13

I am doing an Android application using JDBC to send data to database without using any web services. I did an experiment using Android 2.2 emulator and I am able to send data to MySQL DB (LOCALHOST). After that I tried to send using Android 2.2 device, I changed the path from jdbc:mysql://10.0.2.2:3306/ with jdbc:mysql://xx.xx.xx.xx:3306/.

xx.xx.xx.xx is from ipconfig of my localhost machine. But it is not working in the device. What could be the reason.

Main doubts:

  1. Do Android devices currently support JDBC?
  2. Will Android 2.2 support JDBC?
  3. If supported, which Android versions will support JDBC?
Behnam
  • 6,510
  • 6
  • 35
  • 65
sachi
  • 2,122
  • 7
  • 30
  • 46

5 Answers5

10

Yes. You must remember to put your JDBC connection code in an AsyncTask, otherwise you will BURN BURN BURN!

Behnam
  • 6,510
  • 6
  • 35
  • 65
  • 1
    Also Do NOT mistake "INTERNET" permission for the "uses INTERNET" permission. You need the latter to perform JDBC. – Behnam Sep 23 '13 at 13:02
1

Is Android devices support JDBC.

Will Android 2.2 support JDBC.

If supported, which Android versions will support JDBC.

NO android does not support locally MYSQL database so we can not use JDBC connection frmo android device , its support only sqlite database .

and if you want to use your database from server then simply make a web services for it , all the connection of database then handled from server .

however its help you to understand JDBC and android horrible !! SO Question

Community
  • 1
  • 1
dharmendra
  • 7,835
  • 5
  • 38
  • 71
0

Download MySQL JDBC Connector 3.0.17 and add it into you android project build path.

import java.sql.Connection;



import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import android.app.Activity;

import android.os.Bundle; import android.widget.TextView;

public class MysqlSample01Activity extends Activity {

private static final String url = "jdbc:mysql://<server>:<port>/<database>";
private static final String user = "<username>";
private static final String pass = "<password>";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    testDB();
}

public void testDB() {
    TextView tv = (TextView)this.findViewById(R.id.text_view);
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection(url, user, pass);
        /* System.out.println("Database connection success"); */

        String result = "Database connection success\n";
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery("select * from table_name");
        ResultSetMetaData rsmd = rs.getMetaData();

        while(rs.next()) {
            result += rsmd.getColumnName(1) + ": " + rs.getInt(1) + "\n";
            result += rsmd.getColumnName(2) + ": " + rs.getString(2) + "\n";
            result += rsmd.getColumnName(3) + ": " + rs.getString(3) + "\n";
        }
        tv.setText(result);
    }
    catch(Exception e) {
        e.printStackTrace();
        tv.setText(e.toString());
    }   

}

}

Akilan
  • 924
  • 1
  • 7
  • 19
  • Already i have added JAR file and i did as same as your example. But from device it's not working. How can i give the path in device. – sachi Apr 02 '13 at 05:52
  • 2
    Maybe ur IP is blocked by ur firewall u have to add exception in firewall setting and hav to port forward if ur using router – Akilan Apr 02 '13 at 06:04
  • 1
    If I use this sort of code, does that mean someone can de-compile my app and find the ip, port, dbname, user and password out? – TMH Apr 03 '14 at 14:00
0

It sounds like your Mysql installation only allows connections from local host. Update your Mysql config to allow connections from any host.

From what I can see java.sql is supported since API-level 1 and I don think the support will be removed any time soon.

/Thomas

0

Yes, you can connect to a database using JDBC in android. Just add this line to your gradle dependency:

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

And don't forget to 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 simple java jdbc program.

phanom9797
  • 43
  • 1
  • 10