Java Reference
In-Depth Information
ConnectionPool pool = new ConnectionPool();
// Set the JDBC Driver
pool.setDriver(“sun.jdbc.odbc.JdbcOdbcDriver”);
// Set the URL to the Datasource
pool.setURL(“jdbc:odbc:Movie Catalog”);
// Set the initial size of the Connection Pool
pool.setSize(4);
// Set the Username
pool.setUsername(“”);
// Set the Password
pool.setPassword(“”);
7
// Initialize the pool
pool.initializePool();
The init() method first creates an instance of the ConnectionPool . It then sets the accessors
appropriately. It sets the driver to the JDBC-ODBC Bridge, the data source URL to the Movie
Catalog, and the initial number of connections to 4. The next two accessors (username and
password) are set to empty strings. This is because the Microsoft Access database that we have
created has no login or password.
The last executable line of the init() method initializes the ConnectionPool . This is where
the ConnectionPool connections begin their life. An excerpt of the ConnectionPool's
initializePool() method is listed as follows:
// Load the Driver class file
Class.forName(driver);
// Create Connections based on the size member
for ( int x = 0; x < size; x++ ) {
System.err.println(“Opening JDBC Connection “ + x);
Connection con = createConnection();
if ( con != null ) {
// Create a PooledConnection to encapsulate the
// real JDBC Connection
PooledConnection pcon = new PooledConnection(con);
// Add the Connection the pool.
addConnection(pcon);
}
}
 
Search WWH ::




Custom Search