Connecting to a data source using the DriverManager interface with the IBM Data Server Driver for JDBC and SQLJ
A JDBC application can establish a connection to a data source using the JDBC DriverManager interface, which is part of the java.sql package.
Procedure
The steps for establishing a connection are:
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