ACCPS2机试答案Word格式.docx
- 文档编号:1449491
- 上传时间:2023-04-30
- 格式:DOCX
- 页数:18
- 大小:37.61KB
ACCPS2机试答案Word格式.docx
《ACCPS2机试答案Word格式.docx》由会员分享,可在线阅读,更多相关《ACCPS2机试答案Word格式.docx(18页珍藏版)》请在冰点文库上搜索。
privateintid;
privateStringname;
privateCitycity;
privateStringaddress;
privateStringtelephone;
publicBranches(){
super();
}
publicBranches(intid,Stringname,Citycity,Stringaddress,
Stringtelephone){
this.id=id;
this.name=name;
this.city=city;
this.address=address;
this.telephone=telephone;
publicintgetId(){
returnid;
publicvoidsetId(intid){
publicStringgetName(){
returnname;
publicvoidsetName(Stringname){
publicCitygetCity(){
returncity;
publicvoidsetCity(Citycity){
publicStringgetAddress(){
returnaddress;
publicvoidsetAddress(Stringaddress){
publicStringgetTelephone(){
returntelephone;
publicvoidsetTelephone(Stringtelephone){
packagecom.branches.entity;
publicclassCity{
publicCity(){
publicCity(intid,Stringname){
}
第六步:
添加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<
param.length;
i++){
ps.setObject(i+1,param[i]);
}
}
rs=ps.executeQuery();
returnrs;
publicintexecuteUpdate(Stringsql,Object[]param)throwsException{
introws=ps.executeUpdate();
this.closeReSource();
returnrows;
//释放资源
publicvoidcloseReSource()throwsException{
if(ps!
=null)ps.close();
if(rs!
=null)rs.close();
if(conn!
=null)conn.close();
importjava.util.List;
importcom.branches.entity.Branches;
publicinterfaceBranchesDao{
List<
Branches>
getBranches()throwsException;
BranchesgetBranche(intid)throwsException;
booleanupdBranches(Branchesbranches)throwsException;
booleandelBranches(intid)throwsException;
importcom.branches.entity.City;
publicinterfaceCityDao{
City>
getCitys()throwsException;
第七步:
编写接口实现类
packagecom.branches.dao.impl;
importjava.util.ArrayList;
importcom.branches.dao.BaseDao;
importcom.branches.dao.BranchesDao;
publicclassBrandchesDaoImplextendsBaseDaoimplementsBranchesDao{
publicList<
getBranches()throwsException{
Stringsql="
selectc.idascid,c.nameascname,b.idasbid,b.nameasbname,b.address,b.telephonefromBranchesbinnerjoincityareaconb.cityAreaId=c.id"
;
super.executeQuery(sql,null);
List<
list=newArrayList<
();
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();
returnlist;
publicbooleanupdBranches(Branchesbranches)throwsException{
updatebranchessetname=?
cityareaid=?
address=?
telephone=?
whereid=?
"
Object[]param={branches.getName(),branches.getCity().getId(),branches.getAddress(),branches.getTelephone(),branches.getId()};
returnsuper.executeUpdate(sql,param)>
0;
publicbooleandelBranches(intid)throwsException{
deletefrombrancheswhereid=?
Object[]param={id};
returnsuper.executeUpdate(sql,param)>
publicBranchesgetBranche(intid)throwsException{
selectc.idascid,c.nameascname,b.idasbid,b.nameasbname,b.address,b.telephonefromBranchesbinnerjoincityareaconb.cityAreaId=c.idwhereb.id=?
Object[]param={id};
super.executeQuery(sql,param);
Branchesbra=null;
bra=newBranches();
returnbra;
importcom.branches.dao.CityDao;
publicclassCityDaoImplextendsBaseDaoimplementsCityDao{
getCitys()throwsException{
select*fromCityArea"
list.add(newCity(rs.getInt("
id"
name"
第八步:
在业务层依次调用方法
packagecom.branches.biz;
importcom.branches.dao.impl.BrandchesDaoImpl;
publicclassBranchesManager{
BranchesDaodao=newBrandchesDaoImpl();
getBranches()throwsException{
returndao.getBranches();
publicbooleanmodifyBranches(Branchesbran)throwsException{
returndao.updBranches(bran);
publicbooleanremoveBranches(intid)throwsException{
returndao.delBranches(id);
publicBranchesgetBranche(intid)throwsException{
returndao.getBranche(id);
importcom.branches.dao.impl.CityDaoImpl;
publicclassCityManager{
CityDaodao=newCityDaoImpl();
getCitys()throwsException{
returndao.getCitys();
第九步:
创建servlet,实现题目所要求的功能
packagecom.branches.action;
importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importcom.branches.biz.BranchesManager;
publicclassBranchesActionextendsHttpServlet{
/**
*ThedoGetmethodoftheservlet.<
br>
*
*Thismethodiscalledwhenaformhasitstagvaluemethodequalstoget.
*
*@paramrequesttherequestsendbytheclienttotheserver
*@paramresponsetheresponsesendbytheservertotheclient
*@throwsServletExceptionifanerroroccurred
*@throwsIOExceptionifanerroroccurred
*/
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
request.setCharacterEncoding("
UTF-8"
response.setCharacterEncoding("
response.setContentType("
text/html"
try{
BranchesManagerbm=newBranchesManager();
request.setAttribute("
list"
bm.getBranches());
request.getRequestDispatcher("
index.jsp"
).forward(request,response);
}catch(Exceptione){
e.printStackTrace();
response.sendRedirect("
err.jsp"
*ThedoPostmethodoftheservlet.<
*Thismethodiscalledwhenaformhasitstagvaluemethodequalstopost.
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
PrintWriterout=response.getWriter();
out.println("
<
!
DOCTYPEHTMLPUBLIC\"
-//W3C//DTDHTML4.01Transitional//EN\"
>
HTML>
<
HEAD>
TITLE>
AServlet<
/TITLE>
/HEAD>
BODY>
out.print("
Thisis"
out.print(this.getClass());
usingthePOSTmethod"
/BODY>
/HTML>
out.flush();
out.close();
packagecom.b
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- ACCPS2 答案
![提示](https://static.bingdoc.com/images/bang_tan.gif)