This page in other versions: Unsupported versions: 7.1 / 7.2 / 7.3 / 7.4

Chapter 8. JDBC Interface

Author: Originally written by Peter T. Mount (), the original author of the JDBC driver.

JDBC is a core API of Java 1.1 and later. It provides a standard set of interfaces to SQL-compliant databases.

PostgreSQL provides a type 4 JDBC Driver. Type 4 indicates that the driver is written in Pure Java, and communicates in the database system's own network protocol. Because of this, the driver is platform independent; once compiled, the driver can be used on any system.

This chapter is not intended as a complete guide to JDBC programming, but should help to get you started. For more information refer to the standard JDBC API documentation. Also, take a look at the examples included with the source. The basic example is used here.

8.1. Setting up the JDBC Driver

8.1.1. Getting the Driver

Precompiled versions of the driver can be downloaded from the PostgreSQL JDBC web site.

Alternatively you can build the driver from source. Although you should only need to do this if you are making changes to the source code.

Starting with PostgreSQL version 7.1, the JDBC driver is built using Ant, a special tool for building Java-based packages. You should download Ant from the Ant web site and install it before proceeding. Precompiled Ant distributions are typically set up to read a file .antrc in the current user's home directory for configuration. For example, to use a different JDK than the default, this may work:

JAVA_HOME=/usr/local/sun-jdk1.3
JAVACMD=$JAVA_HOME/bin/java

To build the driver, add the --with-java option to your configure command line, e.g.,

$ ./configure --prefix=xxx --with-java ...

This will build and install the driver along with the rest of the PostgreSQL package when you issue the make/gmake and make/gmake install commands. If you only want to build the driver and not the rest of PostgreSQL, change into the directory src/interfaces/jdbc and issue the respective make/gmake command there. Refer to the PostgreSQL installation instructions for more information about the configuration and build process.

When building the driver from source the jar file that is created will be named postgresql.jar. The build will create this file in the src/interfaces/jdbc/jars directory. The resulting driver will be built for the version of Java you are running. If you build with a 1.1 JDK you will build a version that supports the jdbc1 specification, if you build with a Java2 JDK (i.e. JDK1.2 or JDK1.3) you will build a version that supports the jdbc2 specification.

Note: Do not try to build the driver by calling javac directly, as the driver uses some dynamic loading techniques for performance reasons, and javac cannot cope. Do not try to run ant directly either, because some configuration information is communicated through the makefiles. Running ant directly without providing these parameters will result in a broken driver.

8.1.2. Setting up the Class Path

To use the driver, the jar archive (named postgresql.jar if you built from source, otherwise it will likely be named jdbc7.2-1.1.jar or jdbc7.2-1.2.jar for the jdbc1 and jdbc2 versions respectively) needs to be included in the class path, either by putting it in the CLASSPATH environment variable, or by using flags on the java command line. By default, the jar archive is installed in the directory /usr/local/pgsql/share/java. You may have it in a different directory if you used the --prefix option when you ran configure, or if you are using a binary distribution that places it in some different location.

For instance, I have an application that uses the JDBC driver to access a large database containing astronomical objects. I have the application and the JDBC driver installed in the /usr/local/lib directory, and the Java JDK installed in /usr/local/jdk1.3.1. To run the application, I would use:

export CLASSPATH=/usr/local/lib/finder.jar(1):/usr/local/pgsql/share/java/postgresql.jar:.
java Finder
(1)
finder.jar contains the Finder application.

Loading the driver from within the application is covered in Section 8.2.

8.1.3. Preparing the Database for JDBC

Because Java only uses TCP/IP connections, the PostgreSQL server must be configured to accept TCP/IP connections. This can be done by setting tcpip_socket = true in the postgresql.conf file or by supplying the -i option flag when starting postmaster.

Also, the client authentication setup in the pg_hba.conf file may need to be configured. Refer to the Administrator's Guide for details. The JDBC Driver supports trust, ident, password, md5, and crypt authentication methods.

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group