Reputation: 355
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
Upvotes: 10
Views: 45261
Reputation: 6856
This is how I have made a connection: (I do not know if this is "best practice", but it works.)
Importing the driver:
Java build path
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
Upvotes: 21
Reputation: 4849
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"/>
Upvotes: 0
Reputation: 1204
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.
Upvotes: 2
Reputation: 5824
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.
Upvotes: 1
Reputation:
Here's one way to get PostgreSQL connectivity to your application:
org.postgresql.ds.PGSimpleDataSource
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.
Upvotes: -1