Reputation: 2255
I am new to jdbc and android. So pardon me if the question seems silly.
I am trying to connect to a MySql database from an app.
I went through the jdbc tutorial and wrote the following code:
public static void connectToServer ()
{
Connection conn = null;
try
{
//Connect to the database
String userName = "********";
String password = "********";
String url = "jdbc:mysql://my.domain.name/myDBname";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
conn = DriverManager.getConnection (url, userName, password);
Log.e(tag,"Database connection established");
// Query the database
Statement s = conn.createStatement ();
String query = "INSERT INTO myTableName (A,B,C)" +
"VALUES ('a','b','c')");
Log.e(tag,query);
s.executeQuery (query);
s.close ();
}
catch (Exception e)
{
Log.e(tag,"Database Connection Failed");
Log.e(tag,e.getMessage());
}
finally
{
if (conn != null)
{
try
{
conn.close ();
Log.e(tag,"Database connection terminated");
}
catch (Exception e) { /* ignore close errors */ }
}
}
}
My manifest code:
< uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
< uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
The logcat is:
....
Database connection terminated //my output
com.mysql.jdbc.Driver // output from exception e.getMessage()
What am I doing wrong?
Is there a difference between connecting from a java application and android app?
EDIT : Added manifest permissions
Upvotes: 2
Views: 4064
Reputation: 9242
Be sure you are requesting internet permissions in your manifest.
EDIT: To do this, add <uses-permission android:name="android.permission.INTERNET" />
to your manifest.
Just found this link. Not sure if this can be done easily.
Also something else to look into is CouchDB. Its REST based, so you wouldn't have to build a php service around the MySQL DB - its purely REST.
Upvotes: 3
Reputation: 3521
Why not spend some time learning android.database.sqlite instead? IMHO, it'll be worth it in the long run.
Good luck.
Upvotes: 0
Reputation: 1931
I know that this has worked for some. Make sure you use the JDBC connector mysql-connector-java-3.0.17-ga-bin.jar. Google for it.
Upvotes: 0