Need help to connect to PostgreSql
Hi all,
I'm a java newbie and need help to connect to a postgresql database from NetBeans.
I've managed to register the postgresql driver in service page in NetBeans and establish
a connection to my database, but how should I do it from my code.
This is my code copied from examples on the net:
Code Java:
private void btnOpenDbActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Connection conn = null;
try
{
// Class.forName("???");
conn = DriverManager.getConnection("jdbc:postgresql:5432//localhost/mapobjectdb",
"postgres", "secret");
}
catch (SQLException se)
{
System.out.println("Couldn't connect: print out a stack trace and exit.");
se.printStackTrace();
System.exit(1);
}
if (conn != null)
System.out.println("Connected to the database!");
else
System.out.println("Couldn't connect");
}
What am I supposed to enter in Class.forName
Regards
stab:(
Re: Need help to connect to PostgreSql
Try:
Code :
Class.forName("org.postgresql.Driver");
Re: Need help to connect to PostgreSql
Downloaded "JDBC4 Postgresql Driver, Version 9.0-801.zip", unzipped it, didn't get a jarfile, instead a filestructure like:
postgresql-9.0-801.jdbc4
META-INF
org
postgresql
a lot subdirectories + the following files
Driver$1.class
Driver$ConnectThread.class
Driver.class
PGConnection.class
PGNotification.class
PGRefCursorResultSet.class
PGResultSetMetaData.class
PGStatement.class
My CLASSPATH is like :
.;C:\Projekt\Java\postgresql-9.0-801.jdbc4\org\postgresql
Modified my code according to your suggestion to:
try
{
Class.forName("org.postgresql.Driver");
}
catch (ClassNotFoundException se)
{
System.out.println("Doesn't find driver for PostgreSQL");
se.printStackTrace();
System.exit(1);
}
but I still get "Doesn't find driver for PostgreSQL"
How am I supposed to specify the driver in Class.forName("???"); or is the error in my CLASSPATH?:(
Re: Need help to connect to PostgreSql
The links on this page point to the driver jar file which should be placed on your classpath. Don't unpack/unzip this file (which it looks like you've done based upon the directory structure you posted)