Java Server Pages 2nd Edition phần 8 pps

2 185 0
Java Server Pages 2nd Edition phần 8 pps

Đang tải... (xem toàn văn)

Thông tin tài liệu

Chapter 23. Database Access Strategies 426 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 http://java.sun.com/products/jdbc/ or read a book about JDBC, such as George Reese's Database Programming with JDBC and Java (O'Reilly). 23.1 JDBC Basics The JDBC API is a set of classes and interfaces that allows a Java application to send SQL statements to a database in a vendor-independent way. The API consists mostly of interfaces that define the methods you use in your program. Database engine vendors and third parties provide implementations of these interfaces for a specific database engine; such an implementation is called a JDBC driver. This allows you to develop your program in a database-independent way and connect to a specific database engine by plugging in the appropriate JDBC driver at deployment time. There are JDBC drivers for most database engines on the market, both commercial and open source. If you can't get one from your vendor, check out Sun's list of third-party drivers at http://industry.java.sun.com/products/jdbc/drivers. Figure 23-1 shows how the main interfaces and classes are related. . 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. JDBC documentation online at http:/ /java. sun.com/products/jdbc/ or read a book about JDBC, such as George Reese's Database Programming with JDBC and Java (O'Reilly). 23.1 JDBC Basics. 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.

Ngày đăng: 13/08/2014, 21:21

Tài liệu cùng người dùng

Tài liệu liên quan