Java DriverManager class
DriverManager is a static class in the Java™ 2 Plaform, Standard Edition (J2SE) and Java SE Development Kit (JDK). DriverManager manages the set of Java Database Connectivity (JDBC) drivers that are available for an application to use.
Applications can use multiple JDBC drivers concurrently if necessary. Each application specifies a JDBC driver by using a Uniform Resource Locator (URL). By passing a URL for a specific JDBC driver to the DriverManager, the application informs the DriverManager about which type of JDBC connection should be returned to the application.
Prior to JDBC 4.0, the DriverManager must be made aware of the available
JDBC drivers so it can hand out connections. By making a call to the Class.forName method, it loads
a class into the running Java virtual machine (JVM) based on its string name that is passed into the
method. The following is an example of the class.forName method being used to load the native JDBC
driver:
Example: Load the native JDBC driver
![]()
// Load the native JDBC driver into the DriverManager to make it // available for getConnection requests. Class.forName("com.ibm.db2.jdbc.app.DB2Driver");
JDBC drivers are designed to tell the DriverManager about themselves automatically
when their driver implementation class loads.
In JDBC 4.0, JDBC driver are automatically loaded and the Class.forName call is
not necessary.
Once the native JDBC driver is available for the DriverManager with which to work,
the following line of code requests a Connection object using the native JDBC URL: 
Example: Request a Connection object
// Get a connection that uses the native JDBC driver. Connection c = DriverManager.getConnection("jdbc:db2:*local");
The simplest form of JDBC URL is a list of three values that are
separated by colons. The first value in the list represents the protocol
which is always jdbc for JDBC URLs. The second value
is the subprotocol and db2 or db2iSeries is
used to specifiy the native JDBC driver. The third value is the system
name to establish the connection to a specific system. There are two
special values that can be used to connect to the local database.
They are *LOCAL and localhost (both are case insensitive). A specific
system name can also be provided as follows:
Connection c = DriverManager.getConnection("jdbc:db2:rchasmop");
This creates a connection to the rchasmop system. If the system to which you are trying to connect is a remote system (for example, through the Distributed Relational Database Architecture), the system name from the relational database directory must be used.
Properties
The DriverManager.getConnection method takes a single string URL indicated previously and is only one of the methods on DriverManager to obtain a Connection object. There is also another version of the DriverManager.getConnection method that takes a user ID and password. The following is an example of this version:
Example: DriverManager.getConnection method taking a user ID and password
// Get a connection that uses the native JDBC driver. Connection c = DriverManager.getConnection("jdbc:db2:*local", "cujo", "newtiger");
The line of code attempts to connect to the local database as user cujo with password newtiger no matter who is running the application. There is also a version of the DriverManager.getConnection method that takes a java.util.Properties object to allow further customization. The following is an example:
Example: DriverManager.getConnection method taking a java.util.Properties object
// Get a connection that uses the native JDBC driver. Properties prop = new java.util.Properties(); prop.put("user", "cujo"); prop.put("password","newtiger"); Connection c = DriverManager.getConnection("jdbc:db2:*local", prop);
The code is functionally equivalent to the version previously mentioned that passed the user ID and password as parameters.
Refer to Connection properties for a complete list of connection properties for the native JDBC driver.
URL properties
Another way to specify properties
is to place them in a list on the URL object itself. Each property
in the list is separated by a semi-colon and the list must be of the
form property name=property value. This is just a
shortcut and does not significantly change the way processing is performed
as the following example shows:
Example: Specify URL properties
// Get a connection that uses the native JDBC driver. Connection c = DriverManager.getConnection("jdbc:db2:*local;user=cujo;password=newtiger");
The code is again functionally equivalent to the examples mentioned previously.
If a property value is specified in both a properties object and on the URL object, the URL version takes precedence over the version in the properties object. The following is an example:
Example: URL properties
// Get a connection that uses the native JDBC driver. Properties prop = new java.util.Properties(); prop.put("user", "someone"); prop.put("password","something"); Connection c = DriverManager.getConnection("jdbc:db2:*local;user=cujo;password=newtiger", prop);