Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
1  
Did you place the .jar in a lib folder in stead of libs, by any chance? Since ADT r17, 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. –  MH. Jun 5 '12 at 19:22
    
Got it to work. A lot to learn :P Thanks! –  user1438048 Jun 5 '12 at 19:33

3 Answers 3

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

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.