Error of connection jdbc postgreSQL Android Studio - Failed resolution of: Ljava/lang/management/ManagementFactory;
Asked Answered
G

4

6

I don't understand why the code doesn't works on Android, it's the same code in Java and it's works on my laptop. I've already search on Google, Stackoverflow, and Youtube, but I didn't find the reason. On all Youtube tutorials, this type of code works on android. Example : https://www.youtube.com/watch?v=lM2vDPPx4Xg or https://www.youtube.com/watch?v=MnmEXqfV5BU

Here is the code :

@Override
    protected Void doInBackground(Void... voids) {
        try{
            Class c=Class.forName("org.postgresql.Driver");
            Driver driver=(Driver) c.newInstance();
            DriverManager.registerDriver(driver);
            String url="jdbc:postgresql://url?sslmode=verify-ca&sslfactory=org.postgresql.ssl.DefaultJavaSSLFactory";
            String username="username";
            String password="pwd";

            this.conn=DriverManager.getConnection(url,username,password);

            //statement for make request
            this.stmt=this.conn.createStatement();
            System.out.println("Connecté !");
            conn_load=false;
        }catch (Exception e){
            System.out.println("Connexion problem");
            System.out.println(e.getCause());
        }
        return null;
    }

In the windows terminal the code works but not on android :

Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Ljava/lang/management/ManagementFactory;
        at org.postgresql.util.PGPropertyMaxResultBufferParser.adjustResultSize(PGPropertyMaxResultBufferParser.java:200)
        at org.postgresql.util.PGPropertyMaxResultBufferParser.parseProperty(PGPropertyMaxResultBufferParser.java:37)
        at org.postgresql.core.PGStream.setMaxResultBuffer(PGStream.java:643)
        at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:102)
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:197)
        at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
        at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:217)
        at org.postgresql.Driver.makeConnection(Driver.java:458)
        at org.postgresql.Driver.connect(Driver.java:260)
        at java.sql.DriverManager.getConnection(DriverManager.java:569)
        at java.sql.DriverManager.getConnection(DriverManager.java:219)
        at com.devoteam.recrutement.model.bdd.BDD.doInBackground(BDD.java:54)
        at com.devoteam.recrutement.model.bdd.BDD.doInBackground(BDD.java:12)
        at android.os.AsyncTask$2.call(AsyncTask.java:333)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
        at java.lang.Thread.run(Thread.java:764) 
     Caused by: java.lang.ClassNotFoundException: Didn't find class "java.lang.management.ManagementFactory" on path: DexPathList[[zip file "/data/app/com.devoteam.recrutement-B8V00xTM4oetyFeYswlxVQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.devoteam.recrutement-B8V00xTM4oetyFeYswlxVQ==/lib/arm, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at org.postgresql.util.PGPropertyMaxResultBufferParser.adjustResultSize(PGPropertyMaxResultBufferParser.java:200) 
        at org.postgresql.util.PGPropertyMaxResultBufferParser.parseProperty(PGPropertyMaxResultBufferParser.java:37) 
        at org.postgresql.core.PGStream.setMaxResultBuffer(PGStream.java:643) 
        at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:102) 
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:197) 
        at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) 
        at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:217) 
        at org.postgresql.Driver.makeConnection(Driver.java:458) 
        at org.postgresql.Driver.connect(Driver.java:260) 
        at java.sql.DriverManager.getConnection(DriverManager.java:569) 
        at java.sql.DriverManager.getConnection(DriverManager.java:219) 
        at com.devoteam.recrutement.model.bdd.BDD.doInBackground(BDD.java:54) 
        at com.devoteam.recrutement.model.bdd.BDD.doInBackground(BDD.java:12) 
        at android.os.AsyncTask$2.call(AsyncTask.java:333) 
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
        at java.lang.Thread.run(Thread.java:764) 
Gretel answered 3/8, 2020 at 9:26 Comment(3)
Jar is not loaded from classpath or jar is missingDandrea
You shouldn't use JDBC drivers from Android. JDBC drivers are developed for Java and the Java Virtual Machine and can expect classes that are not available on Android (in this case java.lang.management.ManagementFactory). if you want to connect to a database from Android, it is advisable to use a (rest) service (eg written in Java) to mediate between your Android application and the database.Dunkin
What version of driver are you using?Ecclesiastical
W
1

Same problem using version 42.3.4. In this blog post, they are using 42.2.5 with success. I changed to 42.2.5 JDBC 42 and could connect.

Willywilly answered 29/4, 2022 at 16:33 Comment(1)
Not sure why this was down-voted. I did exactly what he said, changed my dependencies in my build.gradle to 'org.postgresql:postgresql:42.2.5' and it started working right away with only that change. I was referencing '42.3.1'.Quigley
V
0

The web page https://jdbc.postgresql.org/download.html mentions that the latest versions of JDBC 4 do not implement all methods.

JDK 8 - JDBC 4.2 Support for JDBC4 methods is not complete, but the majority of methods are implemented.

In my case I tried using the JDBC 4.2.9 version https://jdbc.postgresql.org/download/postgresql-42.2.9.jar.

Add the postgresql-42.2.9.jar file to the libs folder: MyApplication\app\libs

And explicitly specify the version in the file: build.gradle

dependencies {
     implementation 'org.postgresql:postgresql:42.2.9'
}
Via answered 6/8, 2022 at 20:4 Comment(0)
M
0

Many thanks guys ;)

After a lot of reading I have found the way. Let me to summarise it ;)

As Angel wrote (many many thanks) Add the postgresql-42.2.9.jar file to the libs folder: MyApplication\app\libs

And explicitly specify the version in the file: build.gradle

dependencies {
     implementation 'org.postgresql:postgresql:42.2.9'
}

Then in the app:

  1. Need to enable Strict Mode

     val policy = ThreadPolicy.Builder().permitAll().build()
     StrictMode.setThreadPolicy(policy)
    
  2. set variables for connection

     val jdbcUrl = "jdbc:postgresql://x.x.x.x:5432/DatabaseName"
     val user = "user"
     val password = "password"
    
  3. connect

     val connection = DriverManager
         .getConnection(jdbcUrl, user, password)
    
Mcgowan answered 28/11, 2022 at 9:38 Comment(0)
G
0

the problem is with library compatibility 'org.postgresql:postgresql:42.2.9' just change in build.gradle from 'org.postgresql:postgresql:42.2.9' to 'postgresql:postgresql:9.1-901-1.jdbc4'

Glomerule answered 12/2 at 14:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.