<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >JDBC Interface</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REV="MADE" HREF="mailto:pgsql-docs@postgresql.org"><LINK REL="HOME" TITLE="PostgreSQL 7.4.1 Documentation" HREF="index.html"><LINK REL="UP" TITLE="Client Interfaces" HREF="client-interfaces.html"><LINK REL="PREVIOUS" TITLE="Internals" HREF="ecpg-develop.html"><LINK REL="NEXT" TITLE="Initializing the Driver" HREF="jdbc-use.html"><LINK REL="STYLESHEET" TYPE="text/css" HREF="stylesheet.css"><META NAME="creation" CONTENT="2003-12-22T03:48:47"></HEAD ><BODY CLASS="CHAPTER" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="5" ALIGN="center" VALIGN="bottom" >PostgreSQL 7.4.1 Documentation</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="ecpg-develop.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="ecpg.html" >Fast Backward</A ></TD ><TD WIDTH="60%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="top" ><A HREF="information-schema.html" >Fast Forward</A ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="top" ><A HREF="jdbc-use.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="CHAPTER" ><H1 ><A NAME="JDBC" ></A >Chapter 31. <ACRONYM CLASS="ACRONYM" >JDBC</ACRONYM > Interface</H1 ><DIV CLASS="TOC" ><DL ><DT ><B >Table of Contents</B ></DT ><DT >31.1. <A HREF="jdbc.html#JDBC-SETUP" >Setting up the <ACRONYM CLASS="ACRONYM" >JDBC</ACRONYM > Driver</A ></DT ><DD ><DL ><DT >31.1.1. <A HREF="jdbc.html#JDBC-BUILD" >Getting the Driver</A ></DT ><DT >31.1.2. <A HREF="jdbc.html#JDBC-CLASSPATH" >Setting up the Class Path</A ></DT ><DT >31.1.3. <A HREF="jdbc.html#JDBC-PREPARE" >Preparing the Database Server for <ACRONYM CLASS="ACRONYM" >JDBC</ACRONYM ></A ></DT ></DL ></DD ><DT >31.2. <A HREF="jdbc-use.html" >Initializing the Driver</A ></DT ><DD ><DL ><DT >31.2.1. <A HREF="jdbc-use.html#JDBC-IMPORT" >Importing <ACRONYM CLASS="ACRONYM" >JDBC</ACRONYM ></A ></DT ><DT >31.2.2. <A HREF="jdbc-use.html#JDBC-LOAD" >Loading the Driver</A ></DT ><DT >31.2.3. <A HREF="jdbc-use.html#JDBC-CONNECT" >Connecting to the Database</A ></DT ><DT >31.2.4. <A HREF="jdbc-use.html#JDBC-DISCONNECT" >Closing the Connection</A ></DT ></DL ></DD ><DT >31.3. <A HREF="jdbc-query.html" >Issuing a Query and Processing the Result</A ></DT ><DD ><DL ><DT >31.3.1. <A HREF="jdbc-query.html#JDBC-QUERY-WITH-CURSOR" >Getting results based on a cursor</A ></DT ><DT >31.3.2. <A HREF="jdbc-query.html#AEN24307" >Using the <CODE CLASS="CLASSNAME" >Statement</CODE > or <CODE CLASS="CLASSNAME" >PreparedStatement</CODE > Interface</A ></DT ><DT >31.3.3. <A HREF="jdbc-query.html#AEN24333" >Using the <CODE CLASS="CLASSNAME" >ResultSet</CODE > Interface</A ></DT ></DL ></DD ><DT >31.4. <A HREF="jdbc-update.html" >Performing Updates</A ></DT ><DT >31.5. <A HREF="jdbc-callproc.html" >Calling Stored Functions</A ></DT ><DD ><DL ><DT >31.5.1. <A HREF="jdbc-callproc.html#AEN24386" >Using the <CODE CLASS="CLASSNAME" >CallableStatement</CODE > Interface</A ></DT ><DT >31.5.2. <A HREF="jdbc-callproc.html#AEN24396" >Obtaining <CODE CLASS="CLASSNAME" >ResultSet</CODE > from a stored function</A ></DT ></DL ></DD ><DT >31.6. <A HREF="jdbc-ddl.html" >Creating and Modifying Database Objects</A ></DT ><DT >31.7. <A HREF="jdbc-binary-data.html" >Storing Binary Data</A ></DT ><DT >31.8. <A HREF="jdbc-ext.html" ><SPAN CLASS="APPLICATION" >PostgreSQL</SPAN > Extensions to the <ACRONYM CLASS="ACRONYM" >JDBC</ACRONYM > <ACRONYM CLASS="ACRONYM" >API</ACRONYM ></A ></DT ><DD ><DL ><DT >31.8.1. <A HREF="jdbc-ext.html#AEN24522" >Accessing the Extensions</A ></DT ><DT >31.8.2. <A HREF="jdbc-ext.html#AEN24770" >Geometric Data Types</A ></DT ><DT >31.8.3. <A HREF="jdbc-ext.html#AEN24799" >Large Objects</A ></DT ></DL ></DD ><DT >31.9. <A HREF="jdbc-thread.html" >Using the Driver in a Multithreaded or a Servlet Environment</A ></DT ><DT >31.10. <A HREF="jdbc-datasource.html" >Connection Pools and Data Sources</A ></DT ><DD ><DL ><DT >31.10.1. <A HREF="jdbc-datasource.html#JDBC-DS-INTRO" >Overview</A ></DT ><DT >31.10.2. <A HREF="jdbc-datasource.html#JDBC-DS-CPDS" >Application Servers: <CODE CLASS="CLASSNAME" >ConnectionPoolDataSource</CODE ></A ></DT ><DT >31.10.3. <A HREF="jdbc-datasource.html#JDBC-DS-DS" >Applications: <CODE CLASS="CLASSNAME" >DataSource</CODE ></A ></DT ><DT >31.10.4. <A HREF="jdbc-datasource.html#JDBC-JNDI" >Data Sources and <ACRONYM CLASS="ACRONYM" >JNDI</ACRONYM ></A ></DT ></DL ></DD ><DT >31.11. <A HREF="jdbc-reading.html" >Further Reading</A ></DT ></DL ></DIV ><A NAME="AEN24099" ></A ><A NAME="AEN24101" ></A ><P > <ACRONYM CLASS="ACRONYM" >JDBC</ACRONYM > is a core <ACRONYM CLASS="ACRONYM" >API</ACRONYM > of Java 1.1 and later. It provides a standard set of interfaces to <ACRONYM CLASS="ACRONYM" >SQL</ACRONYM >-compliant databases. </P ><P > <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > provides a <I CLASS="FIRSTTERM" >type 4</I > <ACRONYM CLASS="ACRONYM" >JDBC</ACRONYM > 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. </P ><P > This chapter is not intended as a complete guide to <ACRONYM CLASS="ACRONYM" >JDBC</ACRONYM > programming, but should help to get you started. For more information refer to the standard <ACRONYM CLASS="ACRONYM" >JDBC</ACRONYM > <ACRONYM CLASS="ACRONYM" >API</ACRONYM > documentation. Also, take a look at the examples included with the source. </P ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="JDBC-SETUP" >31.1. Setting up the <ACRONYM CLASS="ACRONYM" >JDBC</ACRONYM > Driver</A ></H1 ><P > This section describes the steps you need to take before you can write or run programs that use the <ACRONYM CLASS="ACRONYM" >JDBC</ACRONYM > interface. </P ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="JDBC-BUILD" >31.1.1. Getting the Driver</A ></H2 ><P > Precompiled versions of the driver can be downloaded from the <A HREF="http://jdbc.postgresql.org" TARGET="_top" ><SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > <ACRONYM CLASS="ACRONYM" >JDBC</ACRONYM > web site</A >. </P ><P > 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 <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > <A HREF="installation.html" >installation instructions</A >. After installation, the driver should be found in <TT CLASS="FILENAME" ><VAR CLASS="REPLACEABLE" >PREFIX</VAR >/share/java/postgresql.jar</TT >. The resulting driver will be built for the version of Java you are running. If you build with a 1.1 <ACRONYM CLASS="ACRONYM" >JDK</ACRONYM > you will build a version that supports the <ACRONYM CLASS="ACRONYM" >JDBC</ACRONYM > 1 specification, if you build with a 1.2 or 1.3 <ACRONYM CLASS="ACRONYM" >JDK</ACRONYM > you will build a version that supports the <ACRONYM CLASS="ACRONYM" >JDBC</ACRONYM > 2 specification, and finally if you build with a 1.4 <ACRONYM CLASS="ACRONYM" >JDK</ACRONYM > you will build a version that supports the <ACRONYM CLASS="ACRONYM" >JDBC</ACRONYM > 3 specification. </P ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="JDBC-CLASSPATH" >31.1.2. Setting up the Class Path</A ></H2 ><A NAME="AEN24139" ></A ><A NAME="AEN24141" ></A ><P > To use the driver, the JAR archive (named <TT CLASS="FILENAME" >postgresql.jar</TT > if you built from source, otherwise it will likely be named <TT CLASS="FILENAME" >pg7.4jdbc1.jar</TT >, <TT CLASS="FILENAME" >pg7.4jdbc2.jar</TT >, or <TT CLASS="FILENAME" >pg7.4jdbc3.jar</TT > for the <ACRONYM CLASS="ACRONYM" >JDBC</ACRONYM > 1, <ACRONYM CLASS="ACRONYM" >JDBC</ACRONYM > 2, and <ACRONYM CLASS="ACRONYM" >JDBC</ACRONYM > 3 versions respectively) needs to be included in the class path, either by putting it in the <TT CLASS="ENVAR" >CLASSPATH</TT > environment variable, or by using flags on the <TT CLASS="COMMAND" >java</TT > command line. </P ><P > For instance, assume we have an application that uses the <ACRONYM CLASS="ACRONYM" >JDBC</ACRONYM > driver to access a database, and that application is installed as <TT CLASS="FILENAME" >/usr/local/lib/myapp.jar</TT >. The PostgreSQL <ACRONYM CLASS="ACRONYM" >JDBC</ACRONYM > driver installed as <TT CLASS="FILENAME" >/usr/local/pgsql/share/java/postgresql.jar</TT >. To run the application, we would use: </P><PRE CLASS="PROGRAMLISTING" >export CLASSPATH=/usr/local/lib/myapp.jar:/usr/local/pgsql/share/java/postgresql.jar:. java MyApp</PRE ><P> </P ><P > Loading the driver from within the application is covered in <A HREF="jdbc-use.html" >Section 31.2</A >. </P ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="JDBC-PREPARE" >31.1.3. Preparing the Database Server for <ACRONYM CLASS="ACRONYM" >JDBC</ACRONYM ></A ></H2 ><P > Because Java only uses TCP/IP connections, the <SPAN CLASS="APPLICATION" >PostgreSQL</SPAN > server must be configured to accept TCP/IP connections. This can be done by setting <TT CLASS="LITERAL" >tcpip_socket = true</TT > in the <TT CLASS="FILENAME" >postgresql.conf</TT > file or by supplying the <VAR CLASS="OPTION" >-i</VAR > option flag when starting <TT CLASS="COMMAND" >postmaster</TT >. </P ><P > Also, the client authentication setup in the <TT CLASS="FILENAME" >pg_hba.conf</TT > file may need to be configured. Refer to <A HREF="client-authentication.html" >Chapter 19</A > for details. The <ACRONYM CLASS="ACRONYM" >JDBC</ACRONYM > driver supports the <TT CLASS="LITERAL" >trust</TT >, <TT CLASS="LITERAL" >ident</TT >, <TT CLASS="LITERAL" >password</TT >, <TT CLASS="LITERAL" >md5</TT >, and <TT CLASS="LITERAL" >crypt</TT > authentication methods. </P ></DIV ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="ecpg-develop.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="jdbc-use.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Internals</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="client-interfaces.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Initializing the Driver</TD ></TR ></TABLE ></DIV ></BODY ></HTML >