Problem in integrating Android application with Postgresql database

  • Replies:3
Ashish Nayal
  • Forum posts: 2

May 16, 2012, 12:31:10 PM via Website

I am developing a Android application with target Android 2.1.

In this application I try to integrate my application with Postgresql
database using jdbc driver(Drivers with which I have tested the application are:

postgresql-9.0-801.jdbc4.jar
postgresql-9.0-801.jdbc3.jar
postgresql-8.3-606.jdbc4


Also I have added in my application AndroidManifest.xml file following
line:

<uses-permission android:name="android.permission.INTERNET" />


But on running android emulator I am getting this error:

Caused by: java.lang.NoClassDefFoundError: org.postgresql.Driver

Please suggest me some work around to solve this problem

Reply
Ashish Nayal
  • Forum posts: 2

May 17, 2012, 7:00:54 AM via Website

Jeremiah
JDBC is not directly importable to android. You need to use android.database.sqlite.
http://stackoverflow.com/questions/1728476/does-android-support-jdbc

But on doing a little search on this I found that some people are able to solve this problem.
I will we very thankful if you can suggest some solution.

Reply
Jeremiah
  • Forum posts: 775

May 17, 2012, 12:06:59 PM via Website

One solution posted here: http://groups.google.com/group/android-developers/browse_thread/thread/cf3dea94d2f6243c

This is the workaround:
try {
String db = "jdbc:sqlite:" + getFilesDir() + "/test.db";
Class.forName("SQLite.JDBCDriver");
Connection conn = DriverManager.getConnection(db);
Statement stat = conn.createStatement();
stat.executeUpdate("create table primes (number int);");
stat.executeUpdate("insert into primes values (2);");
stat.executeUpdate("insert into primes values (3);");
stat.executeUpdate("insert into primes values (5);");
stat.executeUpdate("insert into primes values (7);");
ResultSet rs = stat.executeQuery("select * from primes");
boolean b = rs.first();
while (b) {
Log.d("JDBC", "Prime=" + rs.getInt(1));
b = rs.next();
}
conn.close();
} catch (Exception e) {
Log.e("JDBC", "Error", e);
}

Reply