0
votes

I'm trying to get an app I'm developing to connect to a Postgresql server. I've downloaded postgresql-42.2.5.jre7 from the official Postgresql JDBC driver page, dropped the jar in the libs directory, added a line to the classpath so it will compile. Everything works fine until I actually try to make a connection, at which point I get this error:

Caused by: java.lang.ClassNotFoundException: Didn't find class "java.util.Locale$Category" on path: DexPathList[[zip file "/data/app/swca.lithicdebitagetally-2/base.apk"],nativeLibraryDirectories=[/data/app/swca.lithicdebitagetally-2/lib/arm, /vendor/lib, /system/lib]]
            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
            at org.postgresql.util.GT.<init>(GT.java:35)
            at org.postgresql.util.GT.<clinit>(GT.java:21)
            at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:292)
            at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
            at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195)
            at org.postgresql.Driver.makeConnection(Driver.java:454)
            at org.postgresql.Driver.connect(Driver.java:256)
            at java.sql.DriverManager.getConnection(DriverManager.java:179)
            at java.sql.DriverManager.getConnection(DriverManager.java:213)
            at swca.lithicdebitagetally.UploadData.doInBackground(UploadData.java:31)
            at swca.lithicdebitagetally.UploadData.doInBackground(UploadData.java:14)
            at android.os.AsyncTask$2.call(AsyncTask.java:295)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
            at java.lang.Thread.run(Thread.java:818)
    Suppressed: java.lang.ClassNotFoundException: java.util.Locale$Category
            at java.lang.Class.classForName(Native Method)
            at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
            at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
            ... 18 more
     Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

I'm really stumped as to why its shooting this, though it may have something to do with the version of the driver I'm using. (It seems like I should be using 42.2.5 which is supposed to be Java 8 compatible but I received a compile error whenever I used it).

Here is code that calls it:

protected String doInBackground(String... params){
    Connection connection = null;
    String result = "success";

    try{
        Class.forName("org.postgresql.Driver");
    }

    catch(ClassNotFoundException e)
    {
        e.printStackTrace();
        result = "fail";
    }

    try {
        connection = DriverManager.getConnection(
                "jdbc:postgresql://**.*.****.***:***/*****", "****",
                "****");
    } catch (SQLException e) {
        System.out.println("Connection Failed! Check output console");
        e.printStackTrace();
        result = "fail";

    }

    return result;
}
1
java.util.Locale$Category requires API Level 24+. But using JDBC from Android to remote servers is a bad idea even if you can make it work. Use REST services instead. - mnesarco

1 Answers

1
votes

It seems you're import JDBC driver wrong. If you're using Java 8 you should download the 42.2.5 JDBC driver.

I just test here with Java 8, Postgre JDBC driver 42.2.5 on Eclipse IDE and it worked.

I believe that insert a line in the classpath is not the best way to manage dependencies. You can use maven or any other dependence manage system to do it. Or simple add to buildpath with some clicks in your IDE project folder.