Chapter 13. Using PostgreSQL from a Java Client Application

If you read the previous chapter, you know that ODBC is a technology that can connect a single application to multiple databases without making any changes to the application. ODBC is popular in the C, C++, Visual Basic, and VBA worlds. The folks at Sun Microsystems developed a similar technology for Java applications: JDBC. Many people will tell you that JDBC is an acronym for "Java Database Connectivity," but according to Sun, "…JDBC is the trademarked name and is not an acronym."

JDBC is an API that makes it easy for Java applications to connect to a database, send commands to the database, and retrieve the results. JDBC is packaged as a collection of classes[1]. To start working with JDBC, you use the DriverManager class to obtain a Driver object. After you have a Driver, you make a connection to the database, which results in a Connection object. Using a Connection, you can create Statement. When you execute a command (using a Statement object), you get back a ResultSet. JDBC also provides classes that let you retrieve ResultSetMetaData and DatabaseMetaData.

[1] I use the term class rather loosely in this chapter. JDBC is actually a collection of classes and interfaces. The distinction is not important to JDBC application developers?we use interfaces as if they were classes. Programmers who are building new JDBC drivers will need to understand the distinction.

In this chapter, I won't try to explain all the features of Java's JDBC technology?covering that topic thoroughly would easily require another book. Instead, I'll show you how to use the PostgreSQL JDBC driver. I'll briefly discuss each of the classes I mentioned earlier and show you how to use them.

    Part II: Programming with PostgreSQL