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 da
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
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.
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());
}
}
}
Yes. You must remember to put your JDBC connection code in an AsyncTask
,
otherwise you will BURN BURN BURN!
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