Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.ClassNotFoundException: org.postgresql.Driver, Android

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.

like image 920
user1438048 Avatar asked Jun 05 '12 19:06

user1438048


2 Answers

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.

like image 72
Menelaos Kotsollaris Avatar answered Oct 25 '22 17:10

Menelaos Kotsollaris


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

like image 21
yangya Avatar answered Oct 25 '22 17:10

yangya