Driver Basics

Loading a Driver

The majority of interfaces and pre-defined classes are found in the java.sql package. Thus, when writing a class that will use JDBC, it is necessary to import everything (or only certain classes) from java.sql in the preamble of the source file. For example:
import java.sql.*;

Your code must load an appropriate JDBC driver into the Java virtual machine. You can do this with a single call to the forName method of the Class class, such as in the following line of code:
Class.forName("org.postgresql.Driver");

Note

If the driver is not available, the forName() method will throw a ClassNotFoundException.

Another way of loading a driver is by specifying the driver class when running java:
$ java -Djdbc.drivers=org.postgresql.Driver Bar

Whatever method you use, the specified driver will be loaded and registered with the JDBC DriverManager. The main purpose of the DriverManager is to keep track of loaded drivers and produce database connections upon request. It ensures that programs do not need to concern themselves with drivers (other than loading them).

Opening a Connection

Connections to databases are represented by Connection objects, and they are obtained by asking the DriverManager for one via the getConnection method. The most common version of getConnection takes three arguments: the database URL, the user to connect as, and that user's password. Part of the database URL is used by the DriverManager to decide which driver to use for opening the connection. For Red Hat Database, the URL takes one of the following forms:

jdbc:postgresql:database
jdbc:postgresql://host/database
jdbc:postgresql://host:port/database

where:

If both host and port are omitted (that is, the first form of the URL is used), the driver will assume that the database server is running on the same computer that the Java application is running on and listening on the default port (5432). A driver given a URL of the second form, with just the server name and database specified, will try to connect to the database on the specified host on the default port. Here is an example of requesting a Connection object from the DriverManager to database foo:
String url = "jdbc:postgresql:foo";
String user = "user", pass = "";
Connection con = DriverManager.getConnection(url, user, pass);

Another version of getConnection takes two arguments: the database URL and a java.utils.Properties object that contains the connection parameters. The URL is the same as for the three-argument getConnection. For the parameters, user is required and password is often needed.

Here is an example of using the two-argument version of getConnection to open a database connection:

String url="jdbc:postgresql:foo";
java.utils.Properties props=new java.utils.Properties();
props.setProperty("user", "user")
props.setProperty("password", " ")
Connection con=DriverManager.getConnection(url,props);

Closing the Connection

Closing a connection involves calling the close() method of the Connection object that represents the database connection that you would like to close. For example:
con.close();