bernie2436
bernie2436

Reputation: 23901

trouble getting the right driver for postgres db in eclipse

I am new to enterprise java. I'm trying to get eclipse to connect to a postgres db. I've downloaded postgresql-9.2-1002.jdbc4.jar and postgresql-9.2-1002.jdbc3.jar and put them both in the plugins folder in eclipse. I have a db called personnel setup in pgAdmin3 (localhost:5432). It seems like my code is connecting to the db properly, based on these links...

I have code that I think will connect properly to the DB. But it throws the error shown below. The stack track says it is a driver issue.

Did I put the drivers in the right spot? Do I need to configure postgres to take "remote" connections? I am new to the environment so I am not sure what my next steps should be to debug. It seems like I downloaded the right drivers and put up the right url.

String url = "jdbc:postgresql://localhost:5432/personnel"; 
String user = "root";
String password = "secret";

 public String getDataFromDB(int personID){

       try {
            con = DriverManager.getConnection(url, user, password); //Errors out with Source not found.
                ...

prints the stack trace

    java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost:5432/personnel
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at myCon.personContactInfo(myCon.java:21)
at myTestConnection.main(myTestConnection.java:7)

Upvotes: 0

Views: 1624

Answers (1)

Craig Ringer
Craig Ringer

Reputation: 324265

I see two issues here:

  • You need either the JDBC3 or JDBC4 driver, not both. Use the JDBC3 driver if you're using JDK 5 or older. For anything newer use the JDBC4 driver.

  • The JDBC driver isn't an Eclipse plugin, so it isn't installed to the Eclipse plugins directory. You need to add it to your application's build-time and runtime classpath instead.

Exactly how to add PgJDBC to the classpath depends on the project type. If you're using a freeform ant project then it's likely to be putting the jar in the project lib directory. For Eclipse projects, do it via the Eclipse project properties. For m2eclipse/m2e projects you add PgJDBC as a Maven dependency in your pom.xml like any other.

Since it sounds like you're using a basic Eclipse project you'd use Project->Properties->Java Build Path->Libraries->Add External Jar as you wrote in your comment above.

Upvotes: 1

Related Questions