DB2 Version 9.7 for Linux, UNIX, and Windows

Security under the DB2 JDBC Type 2 Driver

The DB2® JDBC Type 2 Driver for Linux, UNIX and Windows (DB2 JDBC Type 2 Driver) supports user ID and password security.

You must set the user ID and the password, or set neither. If you do not set a user ID and password, the driver uses the user ID and password of the user who is currently logged on to the operating system.

To specify user ID and password security for a JDBC connection, use one of the following techniques.

For the DriverManager interface: you can specify the user ID and password directly in the DriverManager.getConnection invocation. For example:
import java.sql.*;        // JDBC base
…
String id = "db2adm";      // Set user ID
String pw = "db2adm";      // Set password
String url = "jdbc:db2:toronto";
                           // Set URL for the data source
Connection con = DriverManager.getConnection(url, id, pw); 
                           // Create connection
Alternatively, you can set the user ID and password by setting the user and password properties in a Properties object, and then invoking the form of the getConnection method that includes the Properties object as a parameter. For example:
import java.sql.*;                        // JDBC base
import COM.ibm.db2.jdbc.*;                // DB2 JDBC Type 2 driver 
                                          // implementation of JDBC
…
Properties properties = new java.util.Properties(); 
                                          // Create Properties object
properties.put("user", "db2adm");         // Set user ID for the connection
properties.put("password", "db2adm");     // Set password for the connection
String url = "jdbc:db2:toronto";
                                          // Set URL for the data source
Connection con = DriverManager.getConnection(url, properties); 
                                          // Create connection
For the DataSource interface: you can specify the user ID and password directly in the DataSource.getConnection invocation. For example:
import java.sql.*;                        // JDBC base
import COM.ibm.db2.jdbc.*;                // DB2 JDBC Type 2 driver 
                                          // implementation of JDBC
…
Context ctx=new InitialContext();         // Create context for JNDI
DataSource ds=(DataSource)ctx.lookup("jdbc/sampledb"); 
                                          // Get DataSource object
String id = "db2adm";                     // Set user ID
Sring pw = "db2adm";                      // Set password
Connection con = ds.getConnection(id, pw); 
                                          // Create connection
Alternatively, if you create and deploy the DataSource object, you can set the user ID and password by invoking the DataSource.setUser and DataSource.setPassword methods after you create the DataSource object. For example:
import java.sql.*;                        // JDBC base
import COM.ibm.db2.jdbc.*;                // DB2 JDBC Type 2 driver 
                                          // implementation of JDBC
…
DB2DataSource db2ds = new DB2DataSource();
                                            // Create DataSource object
db2ds.setDatabaseName("toronto");           // Set location
db2ds.setUser("db2adm");                    // Set user ID
db2ds.setPassword("db2adm");                // Set password