ACCPS2机试答案.docx
- 文档编号:229651
- 上传时间:2022-10-07
- 格式:DOCX
- 页数:18
- 大小:37.61KB
ACCPS2机试答案.docx
《ACCPS2机试答案.docx》由会员分享,可在线阅读,更多相关《ACCPS2机试答案.docx(18页珍藏版)》请在冰豆网上搜索。
ACCP7.0S2机试营业网点查询
首先针对题目所示创建数据库BranchesMgr
USE[BranchesMgr]
GO
CREATETABLE[dbo].[Branches](
[id][int]IDENTITY(1,1)NOTNULL,
[bname][nvarchar](50)NOTNULL,
[cityAreaId][int]NOTNULL,
[address][nvarchar](100)NOTNULL,
[telephone][nvarchar](50)NOTNULL,
)
ALTERTABLE[dbo].[Branches]WITHCHECKADDFOREIGNKEY([cityAreaId])
REFERENCES[dbo].[CityArea]([id])
GO
CREATETABLE[dbo].[CityArea](
[id][int]IDENTITY(1,1)NOTNULL,
[cname][nvarchar](50)NOTNULL,
)
第二步:
向表中添加至少3条数据
表1:
Branches
表2:
CityArea
第三步创建Web项目BranchesMgr
第四步:
依次创建包名为com.branches.entity,com.branches.dao,com.Branches.biz,com.Branches.action
第五步:
创建实体类(在com.branches.entity中)
packagecom.branches.entity;
publicclassBranches{
privateintid;
privateStringname;
privateCitycity;
privateStringaddress;
privateStringtelephone;
publicBranches(){
super();
}
publicBranches(intid,Stringname,Citycity,Stringaddress,
Stringtelephone){
super();
this.id=id;
this.name=name;
this.city=city;
this.address=address;
this.telephone=telephone;
}
publicintgetId(){
returnid;
}
publicvoidsetId(intid){
this.id=id;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicCitygetCity(){
returncity;
}
publicvoidsetCity(Citycity){
this.city=city;
}
publicStringgetAddress(){
returnaddress;
}
publicvoidsetAddress(Stringaddress){
this.address=address;
}
publicStringgetTelephone(){
returntelephone;
}
publicvoidsetTelephone(Stringtelephone){
this.telephone=telephone;
}
packagecom.branches.entity;
publicclassCity{
privateintid;
privateStringname;
publicCity(){
super();
}
publicCity(intid,Stringname){
super();
this.id=id;
this.name=name;
}
publicintgetId(){
returnid;
}
publicvoidsetId(intid){
this.id=id;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
}
}
第六步:
添加jdbc驱动,创建dao基类用于连接数据库,并创建接口用于实现增删改等功能
packagecom.branches.dao;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
publicclassBaseDao{
protectedConnectionconn=null;
protectedResultSetrs=null;
protectedPreparedStatementps=null;
//获取数据库连接
publicvoidgetConnection()throwsException{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection(
"jdbc:
sqlserver:
//localhost:
1433;DataBaseName=CityDB",
"sa","123");
}
publicResultSetexecuteQuery(Stringsql,Object[]param)throwsException{
this.getConnection();
ps=conn.prepareStatement(sql);
if(param!
=null){
for(inti=0;i ps.setObject(i+1,param[i]); } } rs=ps.executeQuery(); returnrs; } publicintexecuteUpdate(Stringsql,Object[]param)throwsException{ this.getConnection(); ps=conn.prepareStatement(sql); if(param! =null){ for(inti=0;i ps.setObject(i+1,param[i]); } } introws=ps.executeUpdate(); this.closeReSource(); returnrows; } //释放资源 publicvoidcloseReSource()throwsException{ if(ps! =null)ps.close(); if(rs! =null)rs.close(); if(conn! =null)conn.close(); } } packagecom.branches.dao; importjava.util.List; importcom.branches.entity.Branches; publicinterfaceBranchesDao{ List BranchesgetBranche(intid)throwsException; booleanupdBranches(Branchesbranches)throwsException; booleandelBranches(intid)throwsException; } packagecom.branches.dao; importjava.util.List; importcom.branches.entity.City; publicinterfaceCityDao{ List } 第七步: 编写接口实现类 packagecom.branches.dao.impl; importjava.util.ArrayList; importjava.util.List; importcom.branches.dao.BaseDao; importcom.branches.dao.BranchesDao; importcom.branches.entity.Branches; importcom.branches.entity.City; publicclassBrandchesDaoImplextendsBaseDaoimplementsBranchesDao{ publicList Stringsql="selectc.idascid,c.nameascname,b.idasbid,b.nameasbname,b.address,b.telephonefromBranchesbinnerjoincityareaconb.cityAreaId=c.id"; super.executeQuery(sql,null); List while(rs.next()){ Branchesbra=newBranches(); bra.setId(rs.getInt("bid")); bra.setName(rs.getString("bname")); bra.setAddress(rs.getString("address")); bra.setTelephone(rs.getString("telephone")); bra.setCity(newCity(rs.getInt("cid"),rs.getString("cname"))); list.add(bra); } super.closeReSource(); returnli
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- ACCPS2 答案
![提示](https://static.bdocx.com/images/bang_tan.gif)