0

I'm trying to connect my android application to a server (PostgreSQL) with JDBC Driver, but i have this error :

java.lang.ClassNotFoundException: org.postgresql.Driver
...
Caused by: java.lang.NoClassDefFoundError: org/postgresql/Driver
... 12 more
Caused by: java.lang.ClassNotFoundException: org.postgresql.Driver
...
java.sql.SQLException: No suitable driver

I tried many things, like add the address of the driver in the Path, but nothing work. I followed this tutorial : http://appliedcoffeetechnology.tumblr.com/post/10657124340 and added the driver JDBC4 (i also tried the JDBC3), in the Build Path.

Everybody can help me ?

toshiro92
  • 1,287
  • 5
  • 28
  • 42
  • 1
    http://www.youtube.com/watch?v=xHXn3Kg2IQE – Snicolas May 03 '12 at 16:39
  • i love this presentation, it's a pity that there is no sample code for most advanced resolution(with account API)... – Selvin May 03 '12 at 16:43
  • If you are using Android SDK r17 or later, make sure jdbc jar is put in libs/ folder. SDK now will automatically add it to classpath (menas you don'need manually add it to build path yourself). – yorkw May 03 '12 at 22:32
  • Ok, it's a interesting presentation, but it can't solve my problem of recognition in my code. Any ideas of why my driver can't be found ? – toshiro92 May 03 '12 at 22:38
  • http://stackoverflow.com/questions/1728476/does-android-support-jdbc – Selvin May 03 '12 at 22:53

1 Answers1

6

While not the strict answer to your question, I do have a suggestion.

Don't try to use JDBC on the Android device directly. You'll save a lot of hassle that way. I wrote about that in more detail on the "JDBC vs Web Service for Android" question.

Write your database logic on a web-accessible application server and talk to that application server via HTTP+JSON, SOAP, XML-RPC, or similar. This will be a lot more bandwidth efficient and you can make your app a lot more tolerant of issues with connectivity that way. It also saves you from having to expose your database server directly to the Internet - not much of a worry with PostgreSQL so long as you use SSL, but still better not to have to do at all.

Using JAX-RS on JBoss AS 7, Tomcat 7, or similar you should be able to put together a web RESTful XML/JSON services API for your app pretty easily. People also seem to put REST/JSON APIs together pretty quickly with PHP.

You can write a JSON/REST web API in pretty much any language you like with varying degrees of ease. Just search for REST server yourlanguagename.

"Kaw" has pointed out in a deleted answer that there are also virtual JDBC drivers that tunnel requests over HTTP. These may be suitable for some applications.

Community
  • 1
  • 1
Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
  • Ok, thanks for precision's :) – toshiro92 May 04 '12 at 11:47
  • You were right; it's not an answer to the question. There ARE times when connecting directly to a database is appropriate, even if you don't think so. Also, Postgres, in particular, has very strong security so the usual port exposure concerns aren't relevant. – Richard T Apr 12 '13 at 14:38
  • @RichardT Android has come a way since I wrote this but I still think going via web services is almost always going to be the right answer. Even if you're only within an area with good WiFi it's still a pain dealing with stalls and dropouts in the JDBC connection. Much nicer to be using something stateless with idempotent requests, so you can just re-issue it if it's taking too long. I'd still like a proper answer to this question. – Craig Ringer Apr 13 '13 at 00:52
  • @RichardT Oh, and I know it's just bad luck and timing, but http://www.postgresql.org/about/news/1456/ is pretty painful. It isn't actually a security argument since it's just as possible to have a hole in whatever app server / web server you're using to serve the web api calls, so I actually agree with you about the usual port exposure concerns not being an issue *in general*. Right now, though, if you have an exposed Pg database patch it *yesterday*. – Craig Ringer Apr 13 '13 at 01:31
  • 1
    Still does not look like the answer to the question ... – Audrius Meškauskas May 28 '13 at 19:55
  • @AudriusMeškauskas Nobody's offered anything better, and if someone asked me "how do I sail my car across the ocean" I wouldn't give them a detailed tutorial on how to convert it for amphibious use, I'd tell them "get a boat". – Craig Ringer May 28 '13 at 23:31
  • @RingCraig Amphibious cars have certain uses and somebody may ask a question how to build one. – Audrius Meškauskas May 29 '13 at 05:01
  • [Here](http://stackoverflow.com/a/21812893/372643) is how to get the PostgreSQL JDBC driver 9.2 to work on Android, although I agree, it's generally not the right approach. – Bruno Feb 16 '14 at 15:04
  • @CraigRinger I'm fairly new to all of this. I have been doing a lot of research and have come to the conclusion that using a web application server is the best way to get data from an android app to my database. Because of my lack of experience in this I am having trouble finding tutorials on setting this up. What would you recommend searching in order to better learn how to do this? – Regis Nov 06 '17 at 21:05
  • 1
    @Regis Google. "CRUD REST API [my preferred programming language here]" – Craig Ringer Nov 07 '17 at 02:18