第八章 Java模式設計之數據訪問對象模式(3 / 3)

publicbooleanupdateCustomer(...);

publicRowSetselectCustomersRS(...);

publicCollectionselectCustomersVO(...);

...

}

Example9.4CloudscapeDAOImplementationforCustomer

//CloudscapeCustomerDAOimplementationofthe

//CustomerDAOinterface.Thisclasscancontainall

//CloudscapespecificcodeandSQLstatements.

//Theclientisthusshieldedfromknowing

//theseimplementationdetails.

importjava.sql.*;

publicclassCloudscapeCustomerDAOimplements

CustomerDAO{

publicCloudscapeCustomerDAO(){

//initialization

}

//Thefollowingmethodscanuse

//CloudscapeDAOFactory.createConnection()

//togetaconnectionasrequired

publicintinsertCustomer(...){

//Implementinsertcustomerhere.

//Returnnewlycreatedcustomernumber

//ora-1onerror

}

publicbooleandeleteCustomer(...){

//Implementdeletecustomerhere

//Returntrueonsuccess,falseonfailure

}

publicCustomerfindCustomer(...){

//Implementfindacustomerhereusingsupplied

//argumentvaluesassearchcriteria

//Returnavalueobjectiffound,

//returnnullonerrororifnotfound

}

publicbooleanupdateCustomer(...){

//implementupdaterecordhereusingdata

//fromthecustomerDatavalueobject

//Returntrueonsuccess,falseonfailureor

//error

}

publicRowSetselectCustomersRS(...){

//implementsearchcustomershereusingthe

//suppliedcriteria.

//ReturnaRowSet.

}

publicCollectionselectCustomersVO(...){

//implementsearchcustomershereusingthe

//suppliedcriteria.

//Alternatively,implementtoreturnaCollection

//ofvalueobjects.

}

...

}

Example9.5CustomervalueObject

publicclassCustomerimplementsjava.io.Serializable{

//membervariables

intCustomerNumber;

Stringname;

StringstreetAddress;

Stringcity;

...

//getterandsettermethods...

...

}

Example9.6UsingaDAOandDAOFactory?ClientCode

...

//createtherequiredDAOFactory

DAOFactorycloudscapeFactory=

DAOFactory.getDAOFactory(DAOFactory.DAOCLOUDSCAPE);

//CreateaDAO

CustomerDAOcustDAO=cloudscapeFactory.getCustomerDAO();

//createanewcustomer

intnewCustNo=custDAO.insertCustomer(...);

//Findacustomerobject.Getthevalueobject.

Customercust=custDAO.findCustomer(...);

//modifythevaluesinthevalueobject.

cust.setAddress(...);

cust.setEmail(...);

//updatethecustomerobjectusingtheDAO

custDAO.updateCustomer(cust);

//deleteacustomerobject

custDAO.deleteCustomer(...);

//selectallcustomersinthesamecity

Customercriteria=newCustomer();

criteria.setCity("廣州");

CollectioncustomersList=

custDAO.selectCustomersVO(criteria);

//returnscustomersList-collectionofCustomer

//valueobjects.iteratethroughthiscollectionto

//getvalues.