Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439

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:


Part 1: Creating a Data Access Object bean

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.

  1. An Interface with common "CRUD" access methods such as create, read, update, delete and find. You also add getters and setters for each column in the database table and any other "business" methods.
    For example, a method called List findAll() would be useful to dump all the records
  2.  An implementation class, this implements the above interface, and has database specific methods inside it. As a user of this class we hide as much implementation information from the developer. This way, we can transparently replace the DAO with different implementations with minimal impact on our code.

 

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.


© 2008 University of Technology, Sydney. All Rights Reserved.
Redistribution without permission prohibited.

o