24

I am running Eclipse on Windows.

Following this tutorial I downloaded JDBC4, added it to my build path using Project>Properties>add External JAR, browsed for the file, it worked (.classpath file shows the correct lib path).

The package appears in my Referenced Libraries folder, so I continue the tutorial.

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;

    ....

    public void open ()
        {
    try {
        Class.forName("org.postgresql.Driver");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    try {
        conn = DriverManager.getConnection(url, username, password);
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

I think it would be as simple as that but I get hit with this big long stack trace starting with

    java.lang.ClassNotFoundException: org.postgresql.Driver

(I can provide more if needed)

I tried include org.postgresql.*; but that didn't help either. I have also tried the JDBC3 but no luck there either.

I looked at Driver JDBC PostgreSQL with Android which provided a vague answer saying I would be better off just using HTTP+JSON. Which I have never used.

I'm brand new to Android, postgresql, web development, so a simple answer would be appreciated.

Community
  • 1
  • 1
user1438048
  • 241
  • 1
  • 2
  • 3
  • 1
    Did you place the .jar in a `lib` folder in stead of `libs`, by any chance? Since [ADT r17](http://tools.android.com/recent/dealingwithdependenciesinandroidprojects), Libraries referenced from external locations will *not* be packaged into the .apk. The result is your project will compile fine, but at runtime you'll run into the `NoClassDefFoundError` because of the missing libraries. There are lots of similar Q&A's on SO by the way; e.g. [this one](http://stackoverflow.com/a/9820881/1029225). – MH. Jun 05 '12 at 19:22
  • Got it to work. A lot to learn :P Thanks! – user1438048 Jun 05 '12 at 19:33

9 Answers9

25

You need to add the PostgreSQL JDBC Driver in your project as mentioned in search.maven.org.

Gradle:

implementation 'org.postgresql:postgresql:42.2.10'

Maven:

<dependency>
  <groupId>org.postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <version>42.2.10</version>
</dependency>

You can also download the JAR and import to your project manually.

NOTE: Compile as used in this answer is deprecated. Replace with implementation as per 3.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Menelaos Kotsollaris
  • 5,776
  • 9
  • 54
  • 68
  • 1
    Are you sure this is the correct one to use? https://mvnrepository.com/artifact/org.postgresql/postgresql – Mygod Jan 10 '18 at 09:15
  • Trying to run `DriverManager.getConnection()`, at least with version 42.2.12, results in `java.lang.ClassNotFoundException: Didn't find class "java.lang.management.ManagementFactory"` – Sumit May 08 '20 at 17:06
  • @Sumit any solution of that error? getting same error – Anant Shah Jun 20 '20 at 12:56
4

You should put the jar package into the lib folder(WebContent-WEB-INF-lib),and right click on the jar package-build path-add to build path

yangya
  • 49
  • 1
  • 3
  • This driver does not work in android, ```java java.lang.NoClassDefFoundError: Failed resolution of: Ljava/lang/management/ManagementFactory; at org.postgresql.util.PGPropertyMaxResultBufferParser.adjustResultSize ``` AFIK there is no android driver for postgres – Warren MacEvoy Nov 11 '20 at 18:52
  • I did get an old version of the driver to load: `org.postgresql:postgresql:42.2.9.jre7` later versions fail on Android 29 (not tested on other versions of android) – Warren MacEvoy Dec 04 '20 at 13:33
0

It's a CLASSPATH issue; the PostgreSQL JDBC driver isn't available when the class loader tries to load it. You need to add it to your CLASSPATH correctly.

If it works in Eclipse, it's because adding a JAR to the build path is adding it to the CLASSPATH. You have to understand how CLASSPATH works without the Eclipse training wheels to help you.

duffymo
  • 305,152
  • 44
  • 369
  • 561
0

Downloading and adding the JDBC JAR file to the built path of the tool you are using may be a temporary solution as none of the above solutions worked out in my case.

Shilpa
  • 217
  • 4
  • 14
0

I faced the same problem; but the mistake I did was the postgre dependency I added only in plugins section; After adding the postgre dependency (the following) into the project dependencies section it worked.

<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901-1.jdbc4</version>    
</dependency>
  • Version `postgresql:postgresql:9.1-901-1.jdbc4` is extremely old. The latest version is `org.postgresql:postgresql:42.2.5`, see https://search.maven.org/search?q=g:org.postgresql%20AND%20a:postgresql – Mark Rotteveel Dec 14 '18 at 16:31
0

I tried a million things for gradel, and I just needed to add this line to the build.gradel file.

dependencies {
    compile group: 'org.postgresql', name: 'postgresql', version: '42.2.1'
}
chadmc
  • 81
  • 1
  • 7
0

My postgresql driver was working well until I did some developments on my project: added some new files included mapper file because I used Spring and MyBatis. Suddenly, when I tried to run it on server--tomcat--I got the error that org.postgresql.Driver could not be loaded.

Solution on my case: Restart the Eclipse. Maybe in some ways, Eclipse lost contact with the driver. So it needed to be refreshed by restarting it.

gatan
  • 31
  • 4
0

Add JDBC Driver jar file in your classpath

Go to https://jdbc.postgresql.org/download/

download the jar file

Right click on java project Click Run As Run Configuration Click classpath tab Click your project name choose add external jar from right side panel locate the file and click apply Run

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 24 '23 at 13:26
-2

Assuming your dependencies are correctly configured (see libs directory in Android SDK version 17 or above, as pointed out in a comment), you should be able to get the 9.2 driver to work by registering it explicitly with the driver manager.

Instead of:

Class.forName("org.postgresql.Driver");

Use:

DriverManager.register(new org.postgresql.Driver());

(I've also tried with the 9.3-1100-jdbc41 jar, but this wouldn't work.)

Please note that, as explained by Craig Ringer in an answer to a duplicate question, using JDBC from Android is rarely a good idea, because JDBC connections aren't well suited to network usage patterns generally used by Android devices.

Community
  • 1
  • 1
Bruno
  • 119,590
  • 31
  • 270
  • 376