Home > postgresql-jdbc

postgresql-jdbc

Postgresql-jdbc is a project mainly written in Java, based on the BSD-3-Clause license.

Fork of postgresql-jdbc. See the no-autocommit-exceptions branch if you happen to need a version of the driver without them. The only reason you should need this is to support (buggy) legacy code.

This is a simple readme describing how to compile and use the jdbc driver.


This isn't a guide on how to use JDBC - for that refer to Sun's website:

http://java.sun.com/products/jdbc/

For problems with this driver, refer to driver's home page:

http://jdbc.postgresql.org/

and associated mailing list:

http://archives.postgresql.org/pgsql-jdbc/

COMPILING

To compile you will need to have Ant installed. To obtain Ant go to http://ant.apache.org/index.html and download the binary. Being pure java it will run on virtually all java platforms. If you have any problems please email the jdbc list.

Once you have Ant, simply run ant in the top level directory. This will compile the correct driver for your JVM, and build a .jar file (Java ARchive) called postgresql.jar.

REMEMBER: Once you have compiled the driver, it will work on ALL platforms that support that version of the API. You don't need to build it for each platform.

If you are having problems, prebuilt versions of the driver are available at http://jdbc.postgresql.org/


INSTALLING THE DRIVER

To install the driver, the postgresql.jar file has to be in the classpath.

ie: under LINUX/SOLARIS (the example here is my linux box):

export CLASSPATH=.:/usr/local/pgsql/share/java/postgresql.jar

USING THE DRIVER

To use the driver, you must introduce it to JDBC. Again, there's two ways of doing this:

1: Hardcoded.

This method hardcodes your driver into your application/applet. You introduce the driver using the following snippet of code:

try {
  Class.forName("org.postgresql.Driver");
} catch(Exception e) {
  // your error handling code goes here
}

Remember, this method restricts your code to just the postgresql database. However, this is how most people load the driver.

2: Parameters

This method specifies the driver from the command line. When running the application, you specify the driver using the option:

-Djdbc.drivers=org.postgresql.Driver

eg: This is an example of running one of my other projects with the driver:

java -Djdbc.drivers=org.postgresql.Driver uk.org.retep.finder.Main

note: This method only works with Applications (not for Applets). However, the application is not tied to one driver, so if you needed to switch databases (why I don't know ;-) ), you don't need to recompile the application (as long as you havent hardcoded the url's).


JDBC URL syntax

The driver recognises JDBC URL's of the form:

jdbc:postgresql:database

jdbc:postgresql://host/database

jdbc:postgresql://host:port/database

Also, you can supply both username and passwords as arguments, by appending them to the URL. eg:

jdbc:postgresql:database?user=me
jdbc:postgresql:database?user=me&password=mypass

Notes:

1) If you are connecting to localhost or 127.0.0.1 you can leave it out of the URL. ie: jdbc:postgresql://localhost/mydb can be replaced with jdbc:postgresql:mydb

2) The port defaults to 5432 if it's left out.


That's the basics related to this driver. You'll need to read the JDBC Docs on how to use it.

Previous:appmon