ad_qqread_mid_big">
六.范例代码
1.实现数据访问对象模式
范例9-4时表示customer信息的持久性对象的dao范例代码。当findcustomer()被调用时,cloudscapecustomerdao创建一个customer值对象。
范例9-6是使用dao的范例代码。
2.实现数据访问对象的工厂策略
1)使用工厂方法模式
2)使用抽象工厂模式
范例代码9-2是cloudscapedaofactory的范例代码。
范例代码9-3中的customerdao接口为customer持久性对象定义了dao方法,这些接口是被所有具体dao实现来实现的,比如cloudscapecustomerdao、oraclecustomerdao、已经sybasecustomerdao。account和orederdao接口也与此类似。
example 9.1 abstract daofactory class
example 9.2 concrete daofactory implementation for cloudscape
example 9.3 base dao interface for customer
example 9.4 cloudscape dao implementation for customer
example 9.5 customer value object
example 9.6 using a dao and dao factory ?client code
>>>更多专题请看java对象专题
六.范例代码
1.实现数据访问对象模式
范例9-4时表示customer信息的持久性对象的dao范例代码。当findcustomer()被调用时,cloudscapecustomerdao创建一个customer值对象。
范例9-6是使用dao的范例代码。
2.实现数据访问对象的工厂策略
1)使用工厂方法模式
2)使用抽象工厂模式
范例代码9-2是cloudscapedaofactory的范例代码。
范例代码9-3中的customerdao接口为customer持久性对象定义了dao方法,这些接口是被所有具体dao实现来实现的,比如cloudscapecustomerdao、oraclecustomerdao、已经sybasecustomerdao。account和orederdao接口也与此类似。
example 9.1 abstract daofactory class
| // abstract class dao factory public abstract class daofactory { // list of dao types supported by the factory public static final int cloudscape = 1; public static final int oracle = 2; public static final int sybase = 3; ... // there will be a method for each dao that can be // created. the concrete factories will have to // implement these methods. public abstract customerdao getcustomerdao(); public abstract accountdao getaccountdao(); public abstract orderdao getorderdao(); ... public static daofactory getdaofactory( int whichfactory) { switch (whichfactory) { case cloudscape: return new cloudscapedaofactory(); case oracle : return new oracledaofactory(); case sybase : return new sybasedaofactory(); ... default : return null; } } } |
example 9.2 concrete daofactory implementation for cloudscape
| // cloudscape concrete dao factory implementation import java.sql.*; public class cloudscapedaofactory extends daofactory { public static final string driver="com.cloudscape.core.rmijdbcdriver"; public static final string dburl="jdbc:cloudscape:rmi://localhost:1099/corej2eedb"; // method to create cloudscape connections public static connection createconnection() { // use driver and dburl to create a connection // recommend connection pool implementation/usage } public customerdao getcustomerdao() { // cloudscapecustomerdao implements customerdao return new cloudscapecustomerdao(); } public accountdao getaccountdao() { // cloudscapeaccountdao implements accountdao return new cloudscapeaccountdao(); } public orderdao getorderdao() { // cloudscapeorderdao implements orderdao return new cloudscapeorderdao(); } ... } |
example 9.3 base dao interface for customer
| // interface that all customerdaos must support public interface customerdao { public int insertcustomer(...); public boolean deletecustomer(...); public customer findcustomer(...); public boolean updatecustomer(...); public rowset selectcustomersrs(...); public collection selectcustomersvo(...); ... } |
example 9.4 cloudscape dao implementation for customer
| // cloudscapecustomerdao implementation of the // customerdao interface. this class can contain all // cloudscape specific code and sql statements. // the client is thus shielded from knowing // these implementation details. import java.sql.*; public class cloudscapecustomerdao implements customerdao { public cloudscapecustomerdao() { // initialization } // the following methods can use // cloudscapedaofactory.createconnection() // to get a connection as required public int insertcustomer(...) { // implement insert customer here. // return newly created customer number // or a -1 on error } public boolean deletecustomer(...) { // implement delete customer here // return true on success, false on failure } public customer findcustomer(...) { // implement find a customer here using supplied // argument values as search criteria // return a value object if found, // return null on error or if not found } public boolean updatecustomer(...) { // implement update record here using data // from the customerdata value object // return true on success, false on failure or // error } public rowset selectcustomersrs(...) { // implement search customers here using the // supplied criteria. // return a rowset. } public collection selectcustomersvo(...) { // implement search customers here using the // supplied criteria. // alternatively, implement to return a collection // of value objects. } ... } |
example 9.5 customer value object
| public class customer implements java.io.serializable { // member variables int customernumber; string name; string streetaddress; string city; ... // getter and setter methods... ... } |
example 9.6 using a dao and dao factory ?client code
| ... // create the required dao factory daofactory cloudscapefactory = daofactory.getdaofactory(daofactory.daocloudscape); // create a dao customerdao custdao = cloudscapefactory.getcustomerdao(); // create a new customer int newcustno = custdao.insertcustomer(...); // find a customer object. get the value object. customer cust = custdao.findcustomer(...); // modify the values in the value object. cust.setaddress(...); cust.setemail(...); // update the customer object using the dao custdao.updatecustomer(cust); // delete a customer object custdao.deletecustomer(...); // select all customers in the same city customer criteria=new customer(); criteria.setcity("广州"); collection customerslist = custdao.selectcustomersvo(criteria); // returns customerslist - collection of customer // value objects. iterate through this collection to // get values. ... |
| 上一页 1 2 3 4 |
| 相关内容:数据库 设计 解决方案 服务器 存储 |
| 【收藏此页】【大 中 小】【打印】【关闭】 | |
| 上一篇:java布局管理器使用方法探讨 下一篇:使用cmp2和xml处理动态数值对象 10万个软件免费高速下载 | |
| ||||||||||||||
|
|
-
关于我们
公司介绍 最新动态 联系我们 -
产品与服务
域名注册 jsp空间 php空间 -
常见问题
空间操作手册 网站备案相关 退款相关问题 -
技术支持
技术 QQ :178966803 联系电话:13616026886 联系邮箱:fjjsp@vip.163.com
扫描关注微信公众号
闽公网安备 35060202000074号