Connection conn = null;
Context context = null;
try {
Class.forName(driver);
// create parameter list to access naming system
Hashtable parms = new Hashtable();
parms.put(Context.INITIAL_CONTEXT_FACTORY, CNInitialContextFactory.class.getName());
// access naming system
context = new InitialContext(parms);
// get DataSource factory object from naming system
dataSource = (DataSource) context.lookup(dataSourceName);
conn = dataSource.getConnection(userID, password);
} catch (Throwable t) {
System.err.println("Get connection failed.");
t.printStackTrace();
} finally {
try {
if (context != null) {
context.close();
}
} catch(Exception e) {
System.err.println("Context close error.");
e.printStackTrace();
}
}