10

I'm trying to get a postgres jdbc connection working in eclipse. It would be nice to use the Data Source Explorer, but for now I'm just trying to get a basic connection. What I have done so far is download the postgres JDBC connector. I then tried two different things. First, Preferences-> Data Management, I tried to add the postgres connector. Second, I added the jar to my project and tried to load the driver using Class.forName("org.postgresql.Driver"); but neither worked. Does anyone have any ideas?

Thanks, Charlie

1
  • Define "neither worked". Did you get a message, or what? Commented Oct 14, 2008 at 2:08

5 Answers 5

21

This is how I have made a connection: (I do not know if this is "best practice", but it works.)

Importing the driver:

  1. Right click on your project
  2. Choose property
  3. Choose Java build path
  4. Choose Add external JARS.. and select the location to the JDBC driver.

Here is my code:

try{
    Class.forName("org.postgresql.Driver");
    } catch (ClassNotFoundException cnfe){
      System.out.println("Could not find the JDBC driver!");
      System.exit(1);
    }
Connection conn = null;
try {
    conn = DriverManager.getConnection
                   (String url, String user, String password);
     } catch (SQLException sqle) {
       System.out.println("Could not connect");
       System.exit(1);
     }

The url can be of one of the following formats:

jdbc:postgresql:database
jdbc:postgresql://host/database
jdbc:postgresql://host:port/database
Sign up to request clarification or add additional context in comments.

1 Comment

More of a general Eclipse comment, but you can also create a User Library from the Add Library in the build path properties dialog. That way you can reference the same jar file from multiple projects. When you upgrade to a new version, you only need change the jar name in 1 place.
2

I was also having this problem as well and Vjeux's answer helped point me in the right direction.

I have a local copy of Tomcat6 that was installed and is managed by Eclipse. It was installed into '$HOME/bin/tomcat6'. To get the PostgreSQL JDBC driver working I simply copied my postgresql.jar file into the '$HOME/bin/tomcat6/lib' directory.

Also, if you don't know where to get the driver from in the first place, try this. I'm running Ubuntu so I ran 'sudo apt-get install libpg-java' which installed the driver into '/usr/share/java/postgresql.jar' and so I just copied it from there.

Comments

1

I had the same problem using GWT.

I fixed it by copying the jar file inside the "lib" folder: (Project\war\WEB-INF\lib). When you add a jar to the Build Path it seems to do the link statically, however we want the lib at run time!

Hope it fixes your problem.

Comments

0

you can write this code in persistence.xml

      <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/yourDataBaseName"/>
        <property name="javax.persistence.jdbc.user" value="postgres"/>
        <property name="javax.persistence.jdbc.password" value="yourPassword"/>

Comments

-1

Here's one way to get PostgreSQL connectivity to your application:

  1. Get an instance of org.postgresql.ds.PGSimpleDataSource
  2. Setup it with values matching to your database (see methods below)
  3. Proceed using the DataSource as you would use any other, I'd assume at this point you'd be interested in the DataSource.getConnection() method.

The proprietary methods for configuring this particular DataSource are setServerName(), setDatabaseName(), setUser() and setPassword().

I wouldn't recommend doing this for anything else than testing though and it's possible your problem lies in the way you're trying to get an instance of the object using Class.forName() There's almost a dozen different ways to get an instance of an object with subtle differences, I suggest Googling for it since it is a subject a lot of people have already written about all over the Internet.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.