JSP Tag Libraries

Скачать в pdf «JSP Tag Libraries»


// Construct the database source try {


Context ctxt = new InitialContext();


// Get the JDBC factory from the JNDI registry.


DataSource mDs = (DataSource)


ctxt.lookup(«java:comp/env/jdbc/somedatabase») ;


} catch(NamingException ne) {


// Handle errors


}


Instead of using the JDBC DriverManager, we are using the DataSource object (which is standard practice in J2EE applications). DataSource is a standard extension of JDBC2.0 that facilitates the application server’s control over allocation of database connections. To obtain a JDBC connection out of a DataSource all you need to do is call one of its getConnection() methods. When you are done with the JDBC connection, close it. Some DataSource variants also provide built-in database connection pooling (using the class PooledConnection). In such a case, calling close() on the database connection will not really close the connection, but will inform the application server that the user is finished with the connection and that it can be reused.

12.3.6 Wrap it up


We now know that the J2EE environment provides a standardized means of defining and referencing resources and we have seen several examples of how this is accomplished. The next few sections will demonstrate the integration of tags with these J2EE services, starting with a J2EE capable version of the previously defined database tags, and finishing with the newly encountered EJBs.

12.4 J2EE database connection tags


Working with databases from within J2EE is somewhat different from the simple database access demonstrated in chapter
11. Most notably, the allocation of the database connection changes considerably:


■    With standard JDBC, we used the DriverManager class to attain new connections. The input to DriverManager was complex and included several configuration items such as user name, password, driver class, and a URL specifying the exact location to which we want to connect.

Скачать в pdf «JSP Tag Libraries»