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

孤街浪徒 提交于 2019-11-26 12:30:49

问题


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.


回答1:


You need to add the PostgreSQL JDBC Driver in your project as mentioned in Mvnrepository.

Gradle:

// http://mvnrepository.com/artifact/postgresql/postgresql
compile group: 'postgresql', name: 'postgresql', version: '9.0-801.jdbc4'

Maven:

<dependency>
    <groupId>postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.0-801.jdbc4</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.




回答2:


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




回答3:


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.




回答4:


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.




回答5:


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>



回答6:


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.



来源:https://stackoverflow.com/questions/10903481/java-lang-classnotfoundexception-org-postgresql-driver-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!