A JDBC application can establish a connection to a data
source using the JDBC DriverManager interface,
which is part of the java.sql package.
The steps for establishing a connection are:
- Load the JDBC driver by invoking the Class.forName method.
If you are using JDBC 4.0 or later, you do not need to
explicitly load the JDBC driver.
For the
IBM® Data Server Driver for JDBC and SQLJ,
you load the driver by invoking the
Class.forName method
with the following argument:
com.ibm.db2.jcc.DB2Driver
For compatibility with previous JDBC drivers, you
can use the following argument instead:
COM.ibm.db2os390.sqlj.jdbc.DB2SQLJDriver
The following code demonstrates loading the
IBM Data Server Driver for JDBC and SQLJ:
try {
// Load the IBM Data Server Driver for JDBC and SQLJ with DriverManager
Class.forName("com.ibm.db2.jcc.DB2Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
The
catch block is used to print an
error if the driver is not found.
- Connect to a data source by invoking the DriverManager.getConnection method.
You can use one of the following forms of
getConnection:
getConnection(String url);
getConnection(String url, user, password);
getConnection(String url, java.util.Properties info);
For IBM Data Server Driver for JDBC and SQLJ type 4 connectivity,
the getConnection method must specify a user ID
and password, through parameters or through property values.
The url argument
represents a data source, and indicates what type of JDBC connectivity
you are using.
The info argument is an object of type java.util.Properties that
contains a set of driver properties for the connection. Specifying
the info argument is an alternative to specifying property=value; strings
in the URL. See "Properties for the IBM Data
Server Driver for JDBC and SQLJ" for the properties that you
can specify.
There are several ways to specify a user ID and
password for a connection:
- Use the form of the getConnection method that
specifies url with property=value;
clauses, and include the user and password properties in the URL.
- Use the form of the getConnection method that
specifies user and password.
- Use the form of the getConnection method that
specifies info, after setting the user and password properties
in a java.util.Properties object.
Examples
Example: Establishing a connection
and setting the user ID and password in a URL: String url = "jdbc:db2://myhost:5021/mydb:" +
"user=dbadm;password=dbadm;";
// Set URL for data source
Connection con = DriverManager.getConnection(url);
// Create connection
Example: Establishing a connection and setting
the user ID and password in user and password parameters: String url = "jdbc:db2://myhost:5021/mydb";
// Set URL for data source
String user = "dbadm";
String password = "dbadm";
Connection con = DriverManager.getConnection(url, user, password);
// Create connection
Example: Establishing a connection and setting
the user ID and password in a java.util.Properties object: Properties properties = new Properties(); // Create Properties object
properties.put("user", "dbadm"); // Set user ID for connection
properties.put("password", "dbadm"); // Set password for connection
String url = "jdbc:db2://myhost:5021/mydb";
// Set URL for data source
Connection con = DriverManager.getConnection(url, properties);
// Create connection