0

I am unable to connect a PostgreSQL db on Android. Using JDBC is for development purpose only and will change to proper web service.

  1. I have implemented the PostgreSQL driver in build.gradle as "implementation 'org.postgresql:postgresql:42.2.18.jre7"
  2. My PostgreSQL database server is listening to port: 5433
  3. My computer IP in my network is 192.168.1.103
  4. And the user name and password is set correct

The connection:

Connection con = null;
try
{
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    /* Register jdbc driver class. */
    Class.forName("org.postgresql.Driver");

    /* Create connection url. */
    String postgresConnUrl = "jdbc:postgresql://192.168.1.103:5433/lmsdb";

    Properties props = new Properties();
    props.setProperty("user","postgres");
    props.setProperty("password","xxxxxxx");
    props.setProperty("ssl","false");

    /* Get the Connection object. */
    con = DriverManager.getConnection(postgresConnUrl, props);
}catch(Exception ex)
{
    ex.printStackTrace();
}finally
{
    return con ;
}

When I change the IP in "postgresConnUrl" to 127.0.0.1 which is set in pg_hba, I get the error

org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections

And when I use my computer's IP address as shown in the connection code above, the connection con = DriverManager.getConnection(postgresConnUrl, props); is returning null. The log shows no exception or errors, but the con is null and I am unable to trace the problem. Any directions to the problem would be helpful.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

2 Answers2

0

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Hello Mark, I removed the finally block and yes the error is NoClassDefFoundError: Failed resolution of: Ljava/lang/management/ManagementFactory so does this mean Postgres JDBC will not work with Andriod? – Deeyo Pradhan Feb 12 '21 at 18:32
  • @DeeyoPradhan Yes, JDBC drivers are written with the JVM and JRE in mind, not Android. You'll find that most recent JDBC drivers will not work on Android. Maybe you are lucky and an older version of the driver works, but don't count on it. For this reason (and a lot of other reasons), the best advice is: don't use JDBC on Android – Mark Rotteveel Feb 12 '21 at 19:11
  • Yes I better not try it since it is taking much of my time and i will mark this as the answer since it helped me. Please let me know if you know of any guthub or sample of RESTFUL API project I could use for postgres as I am running out of time. Thank you very much. – Deeyo Pradhan Feb 13 '21 at 14:10
0

I tried using older version of JDBC and I was able to connect. The newer version of JDBC does not seem to be compatible with Android and result in error as you posted. But, still I can see many people are using JDBC as it is easy although Rest API is better option but requires additional skills.

Piyush068
  • 1
  • 2