2

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

Brahadeesh
  • 2,245
  • 8
  • 40
  • 58

3 Answers3

3

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.

Community
  • 1
  • 1
Jack
  • 9,156
  • 4
  • 50
  • 75
  • Please check if the permissions I added are enough. – Brahadeesh Aug 02 '11 at 15:08
  • @Jack - fixed the blank manifest code, but the OP wants you to fix his problem (as opposed to him fixing it) :( – KevinDTimm Aug 02 '11 at 15:14
  • No it needs to be – Jack Aug 02 '11 at 15:14
  • I added the permission and I am getting the exact same result. Database connection terminated. – Brahadeesh Aug 02 '11 at 15:15
  • Have you tested from a different environment, say your local computer with Java? – Jack Aug 02 '11 at 15:17
  • no. I have not. But I have not had any problems with the database when I accessed it using php. So there is no problem with the database. – Brahadeesh Aug 02 '11 at 15:19
  • Right - see the link I appended at the end of my post. It basically says "You cannot import a JAR implementing java.* classes easily. And, JDBC would need to be ported to Android, since it probably relies upon classes in JavaSE that Android lacks." – Jack Aug 02 '11 at 15:20
  • Will check it out and let you know. – Brahadeesh Aug 02 '11 at 15:22
  • @Jack let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/2068/discussion-between-brahadeesh-and-jack) – Brahadeesh Aug 02 '11 at 15:26
0

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.

IZI_Shadow_IZI
  • 1,921
  • 4
  • 30
  • 59
  • I have mysql-connector-java-5.1.17 inside the eclipse folder. Is that ok? If its the wrong location, where should I have it? Also I guess the jdbc drivers come with the jdk1.7.0 jre7 package. So do i need to have the driver separately too? – Brahadeesh Aug 02 '11 at 15:24
  • you added it as an external jar? – IZI_Shadow_IZI Aug 02 '11 at 15:35
  • No. Could you please tell me how to do that? – Brahadeesh Aug 02 '11 at 15:36
  • alright well right click on your project folder...then select build path--> configure build path --> add external jar --> and then navigate to where the jar is on your computer and add it. So in your case navigate to the mysql-connector.jar – IZI_Shadow_IZI Aug 02 '11 at 15:38
  • hi I am getting - Conversion to Dalvik format failed with error. – Brahadeesh Aug 02 '11 at 15:57
0

Why not spend some time learning android.database.sqlite instead? IMHO, it'll be worth it in the long run.

Good luck.

Atul Goyal
  • 3,511
  • 5
  • 39
  • 59
  • I am trying to store it outside the phone so that anyone with credentials can access it. Thank you for the suggestion though. – Brahadeesh Aug 08 '11 at 16:25