Chapter 7. JDBC Interface

JDBC is a core API of Java 1.1 (and later) that provides Java applications with a standard set of interfaces through which database access can be done.

Red Hat Database provides type 4 JDBC drivers. Type 4 indicates that the driver is written in pure Java, and communicates with the database using the database's own network protocol. Because of this, the driver is platform independent; it can be used on any system that Java supports.

This chapter describes how to set up the JDBC drivers included with Red Hat Database and how to use the JDBC API. Red Hat Database's extensions to the JDBC driver are also described.

If you require further information on how to use JDBC, you can find the JDBC specification and tutorials at http://java.sun.com/products/jdbc

Setting up the JDBC Driver

Installing the Driver

The Red Hat Database JDBC drivers are distributed as jar archives. Once you have chosen one from the rh-postgresql-jdbc package, there is no need to unpack it. From hereon in, we will refer to the driver archive as rh-pgsql-jdbc2.jar.

Once you have obtained a driver you must make it available to your runtime environment. There are a couple of ways of doing this. The first method involves modifying your system's CLASSPATH environment variable. This can be done by modifying your shell's startup script or by passing a parameter to the java and javac programs when you run them. For example, if a user is using the bash shell, the following line could be put in their .bashrc file:

export CLASSPATH=/usr/share/pgsql/java/rh-pgsql-jdbc2.jar:.

Alternatively, either of the following could be used for running a Java program:
$ CLASSPATH=/usr/share/pgsql/java/rh-pgsql-jdbc2.jar:. java Bar
$ java -cp /usr/share/pgsql/java/rh-pgsql-jdbc2.jar:. Bar

Also included in the CLASSPATH is "." to indicate to java/javac that it should also look in the current directory for classes. This is useful when trying to run programs that have not been packaged into jar files.

The second method involves copying rh-pgsql-jdbc2.jar into the lib/ext directory of your runtime's tree. For example, if your runtime was installed in /usr/local/java/jre1.3.1, you could copy the driver to the /usr/local/java/jre1.3.1/lib/ext directory. If you had a developer's kit installed in /usr/local/java/jdk1.3.1, you would copy rh-pgsql-jdbc2.jar to the /usr/local/java/jdk1.3.1/jre/lib/ext directory. With this method, there is no need to modify the CLASSPATH variable.