0

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)
Community
  • 1
  • 1
bernie2436
  • 22,841
  • 49
  • 151
  • 244
  • can you paste the stacktrace ? – Chris Mar 26 '13 at 05:29
  • @Chris i assumed the url was the problem. seems to be the driver. does it go in the plugins folder in eclipse? also, postgres gave me two options for download. any harm in putting them both in the eclipse plugins folder? – bernie2436 Mar 26 '13 at 05:34
  • Why do you need the JDBC3 driver? Are you using an ancient JVM? – Craig Ringer Mar 26 '13 at 05:40
  • @CraigRinger i am using eclipse helios because it works with a particular tutorial assigned as part of a distributed software class. – bernie2436 Mar 26 '13 at 05:41
  • @akh2103 Er, yes, but that's not what I asked about. The JDBC3 driver is supplied for old Java VMs. If you're using a JDBC4-capable JVM you should just use the JDBC4 driver. You never need both drivers. As for getting your program to "see" the driver - it must be on your program's runtime and compile time classpath. How to set that depends on how your Eclipse project is set up - Maven, custom Ant build, Eclipse project, etc. – Craig Ringer Mar 26 '13 at 05:43
  • @CraigRinger thanks for the help. i got it to work by adding the external jar to the project's classpath. Project->Properties->Java Build Path->Libraries->Add External Jar. If you want to write that as an answer I will accept. – bernie2436 Mar 26 '13 at 06:13

1 Answers1

1

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.

Craig Ringer
  • 307,061
  • 76
  • 688
  • 778