views:

6124

answers:

5

Hi Everyone,

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

A: 

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.

+8  A: 

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 prject
  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
eflles
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.
AngerClown
A: 

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.

Vjeux
A: 

I too have the same problem .I m not able to get the connection to the Postgresql from GWT based eclipse project. Which jar file should I copy inside the "lib" folder: (Project\war\WEB-INF\lib). Please help me.

Thanks in advance

Rakesh.

Rakesh
Welcome at Stackoverflow! You've posted a *question* as an **answer**! :o You need to click the `Ask Question` button at the right top to post a question, not the `Post Your Answer` button at the bottom ;) Once done that, you should delete this "answer" using `delete` link. Feel free to include a link to this question in your own question and elaborate why the provided answers didn't help.
BalusC
A: 

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.

Jamie Carl