NOTE: The material in this chapter is based on JDBCtm API Tutorial and Reference, Second Edition: Universal Data Access for the Javatm 2 Platform, published by Addison Wesley as part of the Java series, ISBN 0-201-43328-1.
DriverManager class is
the traditional management layer of JDBC, working between the user
and the drivers. It keeps track of the drivers that are available
and handles establishing a connection between a database and the
appropriate driver. In addition, the
class attends to things like driver login time limits and the
printing of log and tracing messages.
Note that the
package, otherwise known as the JDBC 2.0 Standard Extension API,
DataSource interface as an alternate and
preferred means of connecting to a data source. However, the
DriverManager facility can still be used with drivers
For simple applications, the only method
DriverManager class that a general programmer
needs to use directly is
As its name implies, this method establishes a connection to a
database. An application may call the
registerDriver as well as the
connect, but in most cases it is better to let
DriverManager class manage the details of
establishing a connection.
maintains a list of
Driver classes that have
registered themselves by calling the method
classes should be written with a static section (a static
initializer) that creates an instance of the class and then
registers it with the
DriverManager class when it is
loaded. Thus, a user would not normally call
DriverManager.registerDriver directly; it should be
called automatically by a
Driver class when it is
Driver class is loaded, and therefore
automatically registered with the
one of two ways:
Class.forName. This explicitly loads the driver class. Since it does not depend on any external setup, this way of loading a driver is the recommended one for using the
DriverManagerframework. The following code loads the class
acme.db.Driver has been written so that loading
it causes an instance to be created and also calls
DriverManager.registerDriver with that instance as the
parameter (as it should do), then it is in the
DriverManager's list of drivers and available for
creating a connection.
Driverclass to the
jdbc.drivers. This is a list of driver classnames, separated by colons, that the
DriverManagerclass loads. When the
DriverManagerclass is initialized, it looks for the system property "
jdbc.drivers," and if the user has entered one or more drivers, the
DriverManagerclass attempts to load them. The following code illustrates how a programmer might enter three driver classes in ~/.hotjava/properties (HotJava loads these into the system properties list on startup):
The first call to a
DriverManagermethod will automatically cause these driver classes to be loaded.
Note that this second way of loading
drivers requires a preset environment that is persistent. If there
is any doubt about that being the case, it is safer to call the
Class.forName to explicitly load each driver.
This is also the right method to use to bring in a particular
driver since once the
DriverManager class has been
initialized, it will never recheck the
In both of these cases, it is the
responsibility of the newly-loaded
Driver class to
register itself by calling
DriverManager.registerDriver. As mentioned, this
should be done automatically when the class is loaded.
For security reasons, the JDBC management
layer will keep track of which class loader provided which driver.
Then when the
DriverManager class is opening a
connection, it will use only drivers that come from the local file
system or from the same class loader as the code issuing the
request for a connection.
Driver classes have
been loaded and registered with the
class, they are available for establishing a connection with a
database. When a request for a connection is made with a call to
DriverManager.getConnection method, the
DriverManager tests each driver in turn to see if it
can establish a connection.
It may sometimes be the case that more
than one JDBC driver is capable of connecting to a given URL. For
example, when connecting to a given remote database, it might be
possible to use a JDBC-ODBC bridge driver, a
JDBC-to-generic-network-protocol driver, or a driver supplied by
the database vendor. In such cases, the order in which the drivers
are tested is significant because the
will use the first driver it finds that can successfully connect to
the given URL.
to use each driver in the order it was registered. (The drivers
jdbc.drivers are always registered first.)
It will skip any drivers that are untrusted code unless they have
been loaded from the same source as the code that is trying to open
It tests the drivers by calling the method
Driver.connect on each one in turn, passing them the
URL that the user originally passed to the method
DriverManager.getConnection. The first driver that
recognizes the URL makes the connection.
Class.forName("jdbc.odbc.JdbcOdbcDriver"); //loads the driver String url = "jdbc:odbc:fred"; Connection con = DriverManager.getConnection( url, "userID", "passwd");
With the addition of the JDBC 2.0
Standard Extension API, a
DataSource object can be
used to establish a connection with a data source. The
DriverManager can still be used, but a
DataSource object offers several advantages over the
DriverManager and is the preferred alternative.
Developers who are writing Enterprise JavaBeans components,
however, should always use a
DataSource object instead
DriverManager. Using a properly implemented
DataSource object is the only way to get connections
that are pooled and that can participate in distributed
DriverManager methods are
static, which means that they operate on the
class as a whole and not on particular instances. In fact, the
DriverManager is declared
private to prevent users from instantiating it.
Logically, there is one instance of the
class. This means that methods are called by qualifying them with
DriverManager, as in the following line of code.
Copyright © 1993, 2011, Oracle and/or its affiliates. All rights reserved.