This lab combines the various techniques from the previous labs to use a common technique for design pattern - the Data Access Object pattern. In this pattern, we develop a Java Bean which extends the RowSet interface to hide the implementation
Level of Difficulty: 2-3 (moderately easy to hard )
Estimated time: 30 minutes
Pre-requisites:
The concept of a Data Access Object (or DAO) is not new to Java. The objective of good layered application design is to encapsulate information and methods for a class such that the invoker does not need to know the internal implementation of that class.
For example, we could implement a DAO such that it initially consists of hard coded test data (where the information may be stored in, say, a static array. We later modify this DAO to access a database. Note that we don't expose at any point implementation specific information (such as the driver name, the database name, the table name or even the column names!) to the user.
Later, the DAO could even be updated to use, say, Web Services or some other external storage mechanisms, and our client code does not need to change. So we have now written a class whose interface is independent of the underlying persistence mechanism.
Instead of creating an OracleBean, we create 2 files.
In our case, we can update the Person object from earlier labs by adding ad
dress details (such as address, email, extn and birthday).
We then create a PersonDAO class which has the following methods.
void createPerson( Person)Person readPerson(String name) // since primarykey is name. Could also pass a Person object with name as key// this effectively does select * from addressbook where name = 'name'Person updatePerson(Person) // this would do update addressbook set .. where name = Person.name// note that we might have to put special processing for changing namevoid deletePerson(Person)// this would do delete from addressbook where name = Person.name // alternatively, could pass the name since this is the primary keyCollection<Person> findAll() // does a select * from addressbookCollection<Person> findByName() // does a select where name=name - note that this is the same as readPerson()
This PersonDAO will use the OracleBean to query and update the database.
We then change the JSP to use this PersonDAO bean. We then call PersonDAO.findAll() to get the collection.
The best thing about this is that you can use Expression Language eg: ${person.name} and also use the EL automatic understanding of Collection classes.
Try creating PersonDAO and Person and changing this JSP.
o