Practical Data Access


In this chapter, we survey some leading data-access technologies and choose one for use in our sample app. The data access technologies discussed in this chapter can be used anywhere in a J2EE app. Unlike entity beans, they are not tied to use in an EJB container. This has significant advantages for testing and architectural flexibility. However, we may still make good use of session EJB CMT, when we implement data access within the EJB container. This chapter focuses on accessing relational databases, as we've previously noted that most J2EE apps work with them. We consider SQL-based technologies and O/R mapping technologies other than entity beans. We will see the potential importance of Java Data Objects, a new API for persisting Java objects, which may standardize the use of O/R mapping in Java. We focus on JDBC which best fulfils the data access requirements of the sample app. We look at the JDBC API in detail, showing how to avoid common JDBC errors and discussing some subtle points that are often neglected. As the JDBC API is relatively low-level, and using it is error-prone and requires an unacceptable volume of code, it's important for app code to work with higher-level APIs built on it. We'll look at the design, implementation, and usage of a JDBC abstraction framework that greatly simplifies the use of JDBC. This framework is used in the sample app, and can be used in any app using JDBC. As we discussed in , it's desirable to separate data access from business logic. We can achieve this separation by concealing the details of data access behind an abstraction layer such as an O/R mapping framework or the Data-Access Objects (DAO) pattern.

We conclude by looking at the DAO pattern in action, as we implement some of the core data-access code of the sample app using the DAO pattern and the JDBC abstraction framework discussed in this chapter.