Driver JDBC PostgreSQL with Android
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.
Postgresql JDBC connection error on Android
DriverManager.getConnection(...)
cannot return null
. Unless there is a severe bug in the implementation, it always returns a non-null connection or throws a SQLException
(or RuntimeException
or Error
as thrown by drivers).
The PostgreSQL driver is likely causing an Error
to be thrown (e.g. a NoClassDefFoundError
, because the PostgreSQL JDBC driver uses Java features or classes not present on Android). This is hidden by the fact that your finally
block will unconditionally return con
, and you only catch and log instances of Exception
, but not Error
. See also Adding return in finally hides the exception
Remove your catch
and finally
block and let any exception or other Throwable
bubble up to the caller, instead of replacing an explicit exception or error with a hidden error (returning null
), alternatively have the catch block wrap the exception in a custom exception and throw that custom exception.
I recommend you don't use JDBC on Android. Most recent JDBC drivers are using Java features or classes not present on Android, which can cause all kinds of issues. You mention you're doing this for development purposes, but I would suggest that creating or mocking your REST API and using that from the start will save you a lot of headaches and unnecessary development work.
How to connect to a PostgreSQL server via JDBC in Android?
If a direct connection between mobile devices and the database server is really what you want (no security concerns) then you wouldn't need any additional library? Just deploy the JDBC driver on android, I guess...?
An other option would be to create a mini web server in front of the PosgreSQL server, containing the JDBC driver. Then you could define a simple protocol of sending/receiving SQL/resultsets via HTTP. The advantage of this is, your application footprint on the mobile device is smaller.
Android postgresql connection
The exception says that you try to establish a network connection on the main thread. This is not allowed in strict mode because this would make your app unresponsive.
You have two options:
Use a new Thread or AsyncTask to connect to your database
Modify the thread policy (only recommended to test the connection process)
StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder().permitAll().build())
postgres JDBC drivers in Android
Following "Android Studio for Beginners - Part 4", even postgres JDBC drivers compile, when you compile clicking on "Build" -> "Build APK(s)".
I had to manually move the APK to my Smartphone and install it there, and it worked as expected. Connection to my postgres database could be established and the results of a simple SELECT - query were shown.
I have not tested if the latest JDBC drivers for JRE8 work that way aswell.
Though this sure is lightyears from any elegant solution,- at least it worked and maybe saves someone out there a week of research or coding some RESTful service, which sure is the preferred way to go. But for some quick and dirty testing,... ;)
Related Topics
String-Date Conversion with Nanoseconds
Prevent Webview from Displaying "Web Page Not Available"
"Hello World" Android App with as Few Files as Possible, No Ide, and Text Editor Only
Nullpointerexception on Button.Findviewbyid()
Onrequestpermissionsresult Not Being Called in Dialog Fragment
How to Override the Cipherlist Sent to the Server by Android When Using Httpsurlconnection
Read/Write .Txt File with Special Characters
Jruby: Watir Is Hanging When Launching Browser
Spark Error - Unsupported Class File Major Version
Setting the Default Font of Swing Program
Polygon Touch Detection Google Map API V2
Android App Bundle Introduces Resource Not Found Crash in Android App
How to Do an Https Post from Android
On Zoom Event for Google Maps on Android