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.
This section describes the steps you need to take before you can write or run programs that use the JDBC interface.
Precompiled versions of the driver can be downloaded from the PostgreSQL JDBC web site.
Alternatively you can build the driver from source, but you should only need to do this if you are making changes to the source code. For details, refer to the PostgreSQL installation instructions. After installation, the driver should be found in PREFIX/share/java/postgresql.jar. 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 JDBC 1 specification, if you build with a 1.2 or 1.3 JDK you will build a version that supports the JDBC 2 specification, and finally if you build with a 1.4 JDK you will build a version that supports the JDBC 3 specification.
To use the driver, the JAR archive (named postgresql.jar if you built from source, otherwise it will likely be named pg7.4jdbc1.jar, pg7.4jdbc2.jar, or pg7.4jdbc3.jar for the JDBC 1, JDBC 2, and JDBC 3 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.
For instance, assume we have an application that uses the JDBC driver to access a database, and that application is installed as /usr/local/lib/myapp.jar. The PostgreSQL JDBC driver installed as /usr/local/pgsql/share/java/postgresql.jar. To run the application, we would use:
export CLASSPATH=/usr/local/lib/myapp.jar:/usr/local/pgsql/share/java/postgresql.jar:. java MyApp
Loading the driver from within the application is covered in Section 31.2.
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 Chapter 19 for details. The JDBC driver supports the trust, ident, password, md5, and crypt authentication methods.