apache > db
Apache DB Project
 
Font size:      

Accessing the Network Server using a DataSource

Accessing the Network Server using a DataSource

The DB2 Universal Driver DataSources com.ibm.db2.jcc.DB2SimpleDataSource and com.ibm.db2.jcc.DB2ConnectionPoolDataSource are supported with Network Server. See the DB2 Universal Driver Documentation for details.

Example

The following example uses com.ibm.db2.jcc.DB2SimpleDataSource to access the Network Server:

public static javax.sql.DataSource getDS(String database, String user, String
password) throws SQLException
{
com.ibm.db2.jcc.DB2SimpleDataSource ds = new com.ibm.db2.jcc.DB2SimpleDataSource();
 
// DatabaseName can include Derby URL Attributes
ds.setDatabaseName(database);
 
if (user != null)
   ds.setUser(user);
if (password != null)
   ds.setPassword(password);
 
// The host on which network server is running
ds.setServerName("localhost");
 
// port on which Network Server is listening
ds.setPortNumber(1527);
 
// Driver type must be 4 to access Network Server
ds.setDriverType(4);
 
return ds;
}
 

The program then can connect:

javax.sql.DataSource ds = getDS("mydb;create=true", null, null);
// Note:  user and password are required on connection
Connection conn = ds.getConnection("usr2", "pass2");

Previous Page
Next Page
Table of Contents
Index