Chapter 23. Database Access Strategies

In this final chapter, we take a closer look at the strategies for using a database in a web application that I’ve mentioned in the previous chapters.

In case you’re new to Java database access, we start with a brief overview of the most important JDBC classes and interfaces. Next, we focus in on the JDBC Connection class and how pooling Connection objects helps to solve a number of common problems. We look at two ways to implement connection-pooling capabilities: the JDBC 2.0 way and by letting a JDBC 1.0 connection pool simulate a JDBC 2.0 pool.

A connection pool can be made available to the rest of the application -- servlets as well as the JSTL database access actions -- in a number of ways. In this chapter we discuss the approach used in Chapter 18 (using an application event listener) in more detail, as well as an approach that’s more flexible but that only works in web containers that support the Java Naming and Directory Interface (JNDI).

No matter if you use a servlet or a custom action to access the database, there are a number of JDBC details that must be handled. To help with this grunt work, we look at a generic database access bean that simplifies life and makes the result of a query easy to use. The last section contains an example of an application-specific custom action using this bean.

If you need to learn more about JDBC programming than what’s covered here, I recommend that you look at the JDBC documentation online at

Get JavaServer Pages, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.