database doesn’t result in any type of connection with the database.
Using DriverManager to Connect to a
Database
Once our application object has been created and initialized, the code attempts to build a connection to the database. This is an important step, and therefore we’ll spend some time discussing the connection code. If you look in the
connectToDB() method in our Hello object, you see that the connection from Java to the database is performed in a single line of code:
connection = DriverManager.getConnection(
"jdbc:mysql://localhost/accounts?user=&password=");
As you can see, the DriverManager is the catalyst used to create the connection to the database. This is consistent with its job of managing all JDBC drivers. When the getConnection() method is called, the DriverManager needs to decide what JDBC driver to use to connect to the database. Figure 5.1 shows how the DriverManager determines the proper JDBC driver to use with a given connec- tion request. DriverManager Connector/J Oracle SQLServer MySQL Oracle Application SQLServer
Figure 5.1 Determining the proper driver.
Let’s begin our discussion of obtaining a connection to the database by exam- ining the API for the DriverManager.
DriverManager API
DriverManager is a static class that exposes methods for handling connections to a database as well as administrative methods for JDBC drivers. The follow- ing methods are those we might be interested in using:
Connection getConnection(String URL)—The DriverManager uses a reg- istered driver in an attempt to build a connection to a specified database. Connection getConnection(String URL, Properties props)—The DriverManager uses a registered driver in an attempt to build a connection to the specified database using the properties provided in the Properties object.
Connection getConnection(String URL, String username, String password)—The DriverManager uses a registered driver in an attempt to build a connection to the specified database using the provided username and password.
Driver getDriver(String URL)—The method returns a registered driver that will potentially be used to connect to a database with the provided URL. Enumeration getDrivers()—The method returns all of the currently registered drivers.
int getLoginTimeout()—The method returns the maximum time in seconds that the current DriverManager will wait for a connection to a database.
void setLoginTimeout(int secs)—The method sets the maximum time in seconds that the current DriverManager will wait for a connection to the database.
These methods can be characterized into three groups: driver management, timeout management, and connection management.
Driver Management Methods
Once a driver (or set of drivers) has been registered with a DriverManager, you usually don’t have to do anything further with the driver. However, a few methods are available for obtaining and removing drivers from the DriverManager if you need to. A current list of registered drivers can be obtained using code like this:
Enumeration e = DriverManager.getDrivers(); while (e.hasMoreElements()) {
Driver d = (Driver)e.nextElement();
System.out.println("Driver Major Version = " + d.getMajorVersion());
}
Once a reference to a driver has been obtained, the deRegisterDriver() method can be used to remove the driver. In almost all cases, you won’t need to use any of this information unless you want to remove from the application all JDBC access to a particular database.
Timeout Management Methods
When connecting to a database—whether local or remote to the Java applica- tion—the application doesn’t know if the database system itself is currently online. There can be situations where a database is down for maintenance or the machine has crashed. A Java application has the option of setting a timeout value for the maximum time that the DriverManager will wait as it attempts to create a connection. The default timeout is 30 seconds before the driver throws a java.net.ConnectException exception. For situations where the database is on a remote machine, the timeout might need to be extended. The following code shows an example of setting a timeout of 90 seconds:
The setLoginTimeout() method accepts a single integer value representing the maximum timeout in seconds for a connection attempt. If you need to obtain the current timeout setting, use the getLoginTimeout() method. If you use this method without setting the timeout, a value of 0 will be returned, indicating that the system default timeout of 30 seconds should be used.
Connection Management Methods
The meat of the DriverManager object is found in the connection methods. A method called getConnection() is overloaded three times to provide numerous ways of supplying arguments to the DriverManager. The signatures for the methods are as follows:
Connection getConnection(String URL);
Connection getConnection(String URL, Properties info);
Connection getConnection(String URL, String user, String password);
In all three methods, the primary connection information is found in the first parameter of type URL (which we discuss in the next section). The first over- loaded method assumes that all of the information for the connection will be passed in the URL. The second method gets connection options from the Prop- erties parameter. The third method obtains connection information from the URL, but pulls the username and password for the database connection from the method parameters.