學(xué)生面試題servlet生命周期和原理_第1頁
學(xué)生面試題servlet生命周期和原理_第2頁
學(xué)生面試題servlet生命周期和原理_第3頁
學(xué)生面試題servlet生命周期和原理_第4頁
學(xué)生面試題servlet生命周期和原理_第5頁
已閱讀5頁,還剩66頁未讀, 繼續(xù)免費(fèi)閱讀

付費(fèi)下載

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

?Servlet被裝載后,ServletServletServletinit()方法Servlet的整個(gè)生命周期內(nèi),init()方法只被調(diào)用一次。Servlet接收和響應(yīng)客戶請(qǐng)求的過程,首先客戶發(fā)送一個(gè)請(qǐng)求,doGet,doPost等這些方法,然后再進(jìn)入對(duì)應(yīng)的方法中調(diào)用邏ServletGenericServlet中是沒有doGet,doPost等等這些方法的,HttpServleterror信息,所以,我們每次定義一個(gè)ServletdoGetdoPost等這些方法。ServletServlet的接口,Servlet接口中定義了五個(gè)方法,Servlet的生命周期,分別是上文提到的init(),service(),destroy()方法。GenericServlet是一個(gè)通用的,不特定于任何協(xié)議的Servlet,ServletHttpServletGenericServlet,因此HttpServletServlet接口。所以我們定義ServletHttpServletServletGenericServletHttpServlet是特定于HTTPHttpServletservice()方法,并將請(qǐng)求ServletRequest,ServletResponseHttpRequestHttpResponse。?publicvoidservice(ServletRequestpublicvoidservice(ServletRequestreq,ServletResponseres){HttpRequestrequest;HttpResponseresponse;{req=(HttpRequest)request;res=(HttpResponse)response;}catch(ClassCastException}catch(ClassCastException{thrownewServletException("non-HTTPrequest}}doXXXHttpServletdoXXX方法都是返回錯(cuò)誤信息,?protectedvoiddoGet(HttpServletRequestprotectedvoiddoGet(HttpServletRequestres,HttpServletResponse{Stringprotocol=req.getProtocol();Stringmsg={}{}}Servlet中override這些方法!ServletResponseServletservice方法。service方法從ServletRequestServletResponse對(duì)象向客?privateHashtable<StringprivateHashtable<StringString[]>=newHashtable<StringString-->String[]的鍵值映射。當(dāng)WEBServletServlet容器重新裝載Servlet新實(shí)例時(shí),ServletServletdestroy()destroy()方法中可以釋放掉Servlet所占用的資源。1,默認(rèn)情況下,當(dāng)WEB客戶第一次請(qǐng)求某個(gè)Servlet的時(shí)候,WEB容器將創(chuàng)建這Servlet的實(shí)例。web.xmlServlet只有<serlvet>元素,沒有<servlet-Servletinit()方法,完成一些全局性的初始化工作。1Servlet容器啟動(dòng)的時(shí)候,所有的Web二、Hibernate生命周期圖舉例 1:瞬時(shí)/臨時(shí)(Transient)-由newHibernateSession關(guān)聯(lián)的對(duì)象被認(rèn)定為瞬時(shí)的。瞬時(shí)對(duì)象不會(huì)被持久化到數(shù)據(jù)庫中,也不會(huì)被賦予持久化標(biāo)識(shí)(identifier)2:持久(Persistent)-持久的實(shí)例在數(shù)據(jù)庫中有對(duì)應(yīng)的記錄,并擁有一個(gè)持久化標(biāo)識(shí)。持久SessionHibernate會(huì)檢測(cè)到處于持久狀態(tài)的對(duì)象的任何改動(dòng),在當(dāng)前操作單元執(zhí)行完畢時(shí)將對(duì)象數(shù)據(jù)與數(shù)據(jù)庫同步。開發(fā)者不需要手動(dòng)執(zhí)行UPDATE。3:脫管/游離(Detached)-Session被關(guān)閉后,對(duì)象就變?yōu)槊摴艿?。?duì)脫管對(duì)象的依然有效,對(duì)象可繼續(xù)被修改。脫管對(duì)象如果重新關(guān)聯(lián)到某個(gè)新的Session上,三、ibaties生命周期圖舉 iBATIS工作原理淺談到iBATIS工作原理問題尤其要注意的是,有了iBATIS,你就再不需要直接編寫JDBC代碼或ADO.NET代碼了。JDBC和ADO.NET的確非常強(qiáng)大,但使用起來總不免覺得太過繁瑣。代碼2-2給出了一個(gè)使用JDBC的例子。代碼2-2使用JDBC的代碼示JDBCAPI其中的一些代碼挪到某個(gè)實(shí)用方法(utilitymethod)中,最明顯的就是那些關(guān)閉資源(如PreparedStatementResultSet)的代碼。代碼2-3iBATIS顯然比JDBC要精簡(jiǎn)得根本無須比較,iBATIS代碼明顯更加簡(jiǎn)潔,更容易閱讀,因此也更容易。本章稍后的“SQL Employeeemp=(Employee)sqlMap.queryForObject("getEmployee",newSQLJava對(duì)象作為結(jié)果。SQLJavaXMLiBATIS負(fù)責(zé)管理幕后的所有資源,其運(yùn)行的實(shí)際效果與我們之前所見的代碼2-2所示的JDBC的例這就引起一個(gè)問題,iBATIS2.1.iBATIS工作原理之于小型、簡(jiǎn)單系輯層非常簡(jiǎn)單,有時(shí)對(duì)一些簡(jiǎn)單的只涉及增查改刪(CRUD:Create,Read,Update,操作的應(yīng)用來說甚至根本就不存在業(yè)務(wù)邏輯。iBATIS3個(gè)原第一,iBATIS要任何額外的基礎(chǔ)設(shè)施(infrastructure)。iBATIS也沒有任何第依賴。iBATIS的最簡(jiǎn)安裝只需要2JAR375KBSQL,iBATIS第二,iBATIS們接下來想說的就是,iBATIS2.2iBATISiBATISiBATIS有著大量的優(yōu)點(diǎn)。iBATISiBATIS個(gè)很重要的原因就是針對(duì)這樣的數(shù)據(jù)庫。因此,iBATIS其實(shí)iBATIS適用于大型系統(tǒng)中的第一個(gè)原因我們已經(jīng)了,不過這個(gè)原因的確很重要,所以我們還是想再強(qiáng)調(diào)一下:iBATIS設(shè)。不論你的應(yīng)用中這兩個(gè)模型之間是多么不匹配,iBATISiBATIS照技術(shù)橫向劃分,iBATIS第二點(diǎn),iBATISiBATIS理器(rowhandler)使得它能夠批處理超大型記錄集,一次一條記錄。iBATISiBATIS最后一點(diǎn),iBATIS晚上執(zhí)行批處理工作。iBATIS最高效的方式執(zhí)行。iBATISiBATIS2.3節(jié)做這件事情。并且為了公平起見,在2.4iBATIS四、Struts的工作原理以及1.0和2.0的區(qū) struts2的執(zhí)行流程、工作原:Struts2框架本身大致可以分為3個(gè)部分:控制器FilterDispatcher、ActionStruts2框架本身大致可以分為3個(gè)部分:控制器FilterDispatcher、業(yè)務(wù)控制器Action和用戶實(shí)現(xiàn)的企業(yè)業(yè)務(wù)邏輯組件??刂破鱂ilterDispatcher是Struts2框架的基礎(chǔ),包含了Action和業(yè)務(wù)邏輯組件是需要用戶來自己實(shí)現(xiàn)的。用戶在開發(fā)Action和業(yè)務(wù)邏輯組件的同時(shí),還需要編寫相關(guān)的配置文件,供控制器FilterDispatcherStruts2Struts1要簡(jiǎn)單,與WebWork框架基本Struts2是WebWork的升級(jí)版本。基本簡(jiǎn)要流程如下:3struts.xmlActionIoC5、Actionstruts.xmlresult,并跳轉(zhuǎn)到相應(yīng)頁StrutsStruts?Struts的工作流程struts-config.xml文件中配置信息,把它們存放到各種配置對(duì)象當(dāng)ActionServlet接收到一個(gè)客戶請(qǐng)求時(shí),將執(zhí)行如程.ActionForm對(duì)象中-(4)ActionFormvalidate()方法返回nullActionMessageActuibErrors對(duì)象,就表示表單驗(yàn)證成功-(5)ActionServlet根據(jù)ActionMap所包含的映射信息決定將請(qǐng)求轉(zhuǎn)發(fā)給哪個(gè)Action,如果相應(yīng)的Action實(shí)例不存在,就先創(chuàng)建這個(gè)實(shí)例,然后調(diào)用Actionexecute()方法;ActionForwardJSP組件JSP、Servlet、JavaBean技術(shù)的出現(xiàn)給我們構(gòu)建強(qiáng)大的企業(yè)應(yīng)用系統(tǒng)提供了可能。但用這些規(guī)則,這就是框架,Struts便應(yīng)運(yùn)而生。StrutsvalidateStrutsMVC模式:webActionServler。用戶提交表單時(shí),一個(gè)配ActionForm對(duì)象被創(chuàng)建,并被填入表單相應(yīng)的數(shù)據(jù),ActionServlerStruts-config.xmlActionForm的Validate()ActionAction不存在,ActionServlet會(huì)先創(chuàng)建這個(gè)對(duì)象,然后調(diào)用Actionexecute()方法。Execute()ActionForm對(duì)象中獲取ActionForward對(duì)象,ActionServlet再把客戶請(qǐng)求轉(zhuǎn)發(fā)給ActionForward對(duì)象指定的jsp組件,ActionForward對(duì)象指定的jsp生成動(dòng)態(tài)的網(wǎng)頁,返回給Action類:Struts1Action類繼承一個(gè)抽象基類。Struts1的一個(gè)普遍問題是使用抽象類編程而不Struts2ActionAction接口,也可實(shí)現(xiàn)其他接口,使可選和定制的服務(wù)成為可能。Struts2ActionSupport基類去實(shí)現(xiàn)常用的接口。Action接口不是必須的,任何有executePOJOStruts2Action對(duì)象。線程模式Struts1ActionAction的一個(gè)實(shí)例來處理所有的請(qǐng)求。單例策略限制了Struts1Action能作的事,并且要在開發(fā)時(shí)特別。Action資源Servlet依賴Struts1ActionServletAPI因?yàn)楫?dāng)一個(gè)ActionHttpServletRequestHttpServletResponse被傳遞給executeStruts2ActionAction脫離容器單獨(dú)被測(cè)試。如果需要,Struts2Action仍然可以初始的request和response。但是,其他的元素減少或者消除了直接訪HttpServetRequest和HttpServletResponse的必要性??蓽y(cè)性測(cè)試Struts1Action的一個(gè)主要問題是execute方法了servletAPI(這使得測(cè)試要依賴于容器)。一個(gè)第擴(kuò)展--StrutsTestCase--提供了一套Struts1的模擬對(duì)象(來進(jìn)Struts2Action可以通過初始化、設(shè)置屬性、調(diào)用方法來測(cè)試,“依賴注入”支持也使測(cè)捕獲輸入Struts1ActionFormActionForm必須繼承一個(gè)基類。因?yàn)槠銳avaBeanActionFormBean(DynaBeans)ActionForm的選擇,但是,開發(fā)者可能是在重新描述(創(chuàng)建)JavaBean(javabean)。Struts2Action屬性作為輸入屬性,消除了對(duì)第二個(gè)輸入對(duì)象的需求。輸入屬性可能是有自己(子)屬性的rich對(duì)象類型。Action屬性能夠通過web頁面上的taglibs。Struts2ActionForm模式。rich對(duì)象類型,包括業(yè)務(wù)對(duì)象,能夠用作輸入/輸出對(duì)象。這種ModelDriven特性簡(jiǎn)化了taglib對(duì)POJO輸入對(duì)象的。Struts1JSTLJSTLELEL有基本對(duì)象圖遍歷,但是對(duì)集合和索Struts2JSTL,但是也支持一個(gè)更強(qiáng)大和靈活的表達(dá)式語言--"ObjectGraphNotationLanguage"(OGNL). Struts2使用"ValueStack"技術(shù),使taglib能夠 象綁定起來。ValueStack策略允許通過一系列名稱相同但類型不同的屬性重用頁面Struts1ActionForm屬性通常都是String類型。 mons-Beanutils進(jìn)行類Struts2OGNL進(jìn)行類型轉(zhuǎn)換。提供基本和常用對(duì)象的轉(zhuǎn)換器。Struts1ActionFormvalidate方法中手動(dòng)校驗(yàn),或者通過CommonsStruts2validateXWork校驗(yàn)框架來進(jìn)行校驗(yàn)。XWork校驗(yàn)框架使用為屬chain校驗(yàn)子屬性Struts1RequestProcessors(生命周期),但是模塊中的所有Action必須共享相同的生命周期。Struts2支持通過器堆棧(InterceptorStacks)為每一個(gè)Action創(chuàng)建不同的生命周Action一起使用五、三大框架的搭 1、首先寫它的配置beans-<beanid="dataSource"<property<property<property<property<beanid="sqlMap"<property<property<ref<beanid="dbDao"<property <ref <beanid="loginDao"<property <ref <beanid="loginService"<property<ref<bean<property<ref②struts.xml(總<?xmlversion="1.0"encoding="GBK"<!DOCTYPEstruts"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"""><includefile="struts-default.xml"<packagename="sample1"extends="struts-<includefile="com/qr/xm/qx/login/login.xml"http://這里可以放多個(gè)Login.xml(分支 <?xmlversion="1.0"encoding="GBK"<!DOCTYPEstruts"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"""><packagename="login"extends="sample1"<actionname="login"<resultname="addone"<resultname="queryone"<result<?xmlversion="1.0"encoding="UTF-<!DOCTYPEsqlMapConfigPUBLIC" SQLMapConfig2.0//EN" /dtd/sql-map-config-<!--從properties里JDBC的路"/>--useStatementNamespaces="true"/><transactionManager<dataSource<propertyname="JDBC.Driver"<propertyname="JDBC.ConnectionURL"<propertyname="JDBC.Username"<propertyname="JDBC.Password"Login.xml(分支 <?xmlversion="1.0"encoding="UTF-8"<!DOCTYPEsqlMapPUBLIC"-////DTDSQLMap2.0//EN""map-<sqlMap<!--Usetypealiasestoavoidtythefullclassnameeverytime.--><typeAliasalias="login"<!--Resultmapsdescribethemapbetweenthecolumnsreturnedfromaquery,andtheclassproperties.Aresultmapisn'tnecessaryifthecolumns(oraliases)matchtothepropertiesexactly.--<resultMapid="LoginResult"<resultproperty="login_id"<resultproperty="password"<!--SelectwithnoparametersusingtheresultmapforAccountclass.--><selectid="selectAllLogin"select*from<selectid="selectLoginById"login_id,passwordfromuserwherelogin_id=<!--Insertexample,usingtheAccountparameterclass--><insertid="insertLogin"parameterClass="login">insertintouser()values#login_id#,)<!--Updateexample,usingtheAccountparameterclass--><updateid="updaogin"parameterClass="login"updateuserset<!--Deleteexample,usinganintegerastheparameterclass--><deleteid="deleparameterClass="String"deletefromuserwherelogin_id=publicinterfaceRoleServicepublicListpublicvoidadd(RoleEntitypublicvoiddelete(StringpublicMapqueryone(Stringpublicvoidupdate(RoleEntitypublicclassRoleServiceImpleRoleServiceprivateRoleDaopublicListquery()//TODOAuto-generatedmethodreturn}publicvoidadd(RoleEntityroleEntity)//TODOAuto-generatedmethodstub}publicvoiddelete(Stringid)//TODOAuto-generatedmethodstub}publicMapqueryone(Stringid)TODOAuto-generatedmethodstubreturnroleDao.queryone(id);}publicvoidupdate(RoleEntityroleEntity)//TODOAuto-generatedmethodstub}publicRoleDaogetRoleDao()return}publicvoidsetRoleDao(RoleDaoroleDao)this.roleDao=}publicList<RoleEntity>query();publicvoidadd(RoleEntityroleEntity);publicvoiddelete(Stringid);publicvoidupdate(RoleEntitypublicMapqueryone(StringpublicclassRoleDaoImpleextendsimplementsRoleDaopublicListquery()//TODOAuto-generatedmethodstubreturn}publicvoidadd(RoleEntityroleEntity)//TODOAuto-generatedmethodsuper.add("Role.insertRole",}publicvoiddelete(Stringsuper.delete("Role.deleteRoleById",}publicvoidupdate(RoleEntityroleEntity)//TODOAuto-generatedmethodsuper.update("Role.updateRole",}publicMapqueryone(Stringid)//TODOAuto-generatedmethodreturnsuper.queryObjectList("Role.selectRoleById",}}publicclassRoleActionextends*privatestaticfinallongserialVersionUID=privateRoleServiceroleService;privateList<RoleEntity>listrole;privateRoleEntityroleEntity;privateString[]cid;privateMapprivateString//給這些私有GETSET方publicStringquery(){return}publicStringreturn}publicStringreturn}publicStringdelete(){for(inti0;icid.lengthi++)}return}publicStringqueryone(){return}publicStringupdate(){returnthis.query();Vo:實(shí)體publicclassRoleEntity{privateStringrole_id;privateStringname;privateStringdescription;privateString//給這些私有加GETSET方}publicinterfaceDbDao*@param@parampublicvoidadd(Stringstr,Object@param@parampublicvoidupdate(Stringstr,Object@param@parampublicvoiddelete(Stringstr,Object@parampublicvoidadd(String@parampublicvoidupdate(String@param}}catch(SQLExceptione)publicvoiddelete(String@parampublicList<Object>queryForList(String@param@param@parampublicList<Object>queryPageList(Stringstr,Objectobj,Pagepage);@parampublicObjectqueryObjectList(Stringstr,String@parampublicintqueryCount(String}publicclassDbDaoImpleSqlMapDaoSupportimplementsDbDaopublicvoidadd(Stringstr,Objectobj)//TODOAuto-generatedmethodtrysuper.getSqlMap().insert(str,//TODOAuto-generatedcatchblock}}publicvoidadd(Stringstr)//TODOAuto-generatedmethodtrysuper.getSqlMap}catch(SQLExceptione)//TODOAuto-generatedcatchblock}}publicvoiddelete(Stringstr,Objectobj)//TODOAuto-generatedmethodtrysuper.getSqlMap().delete(str,}catch(SQLExceptione){}}publicvoiddelete(Stringstr)//TODOAuto-generatedmethodtrysuper.getSqlMap}catch(SQLExceptione)//TODOAuto-generatedcatchblock}}publicintqueryCount(Stringstr)//TODOAuto-generatedmethodtry//TODOAuto-generatedcatchblock}return}publicList<Object>queryForList(Stringstr)//TODOAuto-generatedmethodtry}catch(SQLExceptione)//TODOAuto-generatedcatchblock}return}publicObjectqueryObjectList(Stringstr,Stringid){//TODOAuto-generatedmethodtrysuper.getSqlMap().queryForObject(str,id);}catch(SQLExceptione)//TODOAuto-generatedcatchblock}return}publicList<Object>queryPageList(Stringstr,Objectobj,Pagepage){//TODOAuto-generatedmethodreturn}publicvoidupdate(Stringstr,Objectobj)//TODOAuto-generatedmethodtry ().update(str,}catch(SQLExceptione)//TODOAuto-generatedcatchblock}}publicvoidupdate(Stringstr)//TODOAuto-generatedmethodtry }catch(SQLExceptione)//TODOAuto-generatedcatchblock}}<?xmlversion="1.0"encoding="UTF-<web-app <filter-name>struts2</filter-<filter-</filter-<filter-map<filter-name>struts2</filter-<url-pattern>/*</url-</filter-map</web-2、SSH框publicinterfaceDbDaopublicList<Object>find(Stringpublicvoidsave(Objectobj);publicvoidupdate(Objectobj);publicvoiddelete(ObjectpublicObjectqueryone(Classcls,SerializablepublicList<Object>querypageList(HQuery}publicclassDbDaoImpleextendsimplementsDbDaopublicvoiddelete(Objectobj)//TODOAuto-generatedmethodsuper.getHibernateTem}publicList<Object>find(Stringhql)//TODOAuto-generatedmethod}publicObjectqueryone(Classcls,Serializableid){}

//TODOAuto-generatedmethodreturnsuper.getHibernateTempublicvoidsave(Objectobj)//TODOAuto-generatedmethodtrysuper.getHibernateTem}catch(Exceptionex){}}publicvoidupdate(Objectobj)//TODOAuto-generatedmethodtrysuper.getHibernateTem}catch(Exceptionex){}}publicList<Object>querypageList(finalHQueryhquery){//TODOAuto-generatedmethodHibernateCallback(){

publicObjectthrowsSQLException//TODOAuto-generatedmethodstubStringBufferstr=newQueryquery=List<Params>list=for(inti=0;i<list.size();i++){Paramsparams=list.get(i);inttype=switch(type)casejava.sql.Types.VARCHAR:query.setString(i,(String)casejava.sql.Types.INTEGER:query.setInteger(i,(Integer)casejava.sql.Types.DOUBLE:query.setDouble(i,(Double)query.setString(i,}}return}}publicinterfaceBaseServicepublicListquery(Conditionpublicvoidadd(Objectpublicvoidupdate(ObjectpublicObjectqueryone(Classcls,Serializablepublicvoiddelete(Classcls,Serializable[]}public Serviceimplements{privateDbDaopublicListquery(Conditioncondition)//TODOAuto-generatedmethod}publicDbDaogetDbDao()return}publicvoidsetDbDao(DbDaodbDao)this.dbDao=}publicvoidadd(Objectobj)//TODOAuto-generatedmethodstub}publicvoidupdate(Object{}publicObjectqueryone(Classcls,Serializable{returndbDao.queryone(cls,}publicvoiddelete(Classcls,Serializable[]{ //TODOAuto-generatedmethodstubListlist=newArrayList();for(inti=0;i<ids.length;i++) Objectobj=this.queryone(cls,ids[i]); };//dbDao.delete(cls,}publicinterfaceStudentServiceextends{}publicclassStudentServiceImpleServiceStudentService}publicclassStudentConditionextendsConditionprivateStringprivateStringpublicStudentCondition()super.hql="fromcom.qr.xm.student.vo.StudentEntityss";}publicStringgetId()return}publicvoidsetId(Stringid)this.id=}publicStringgetName()return}publicvoidsetName(Stringname)=}publicvoidparams1()//TODOAuto-generatedmethod}}publicclassStudentActionextendsActionSupport*privatestaticfinallongserialVersionUID=publicList<StudentEntity>privateStudentServiceprivateStudentConditionstudentCondition=privateStudentEntitystudentEntity=privateString[]ids;privateStringsid;privateStringprivateMappublicStringquery(){studentList=return"query";}publicString{return}publicStringadd(){return}publicStringqueryone() studentEntity=(StudentEntity)StudentEntity.class,sid);return}publicStringupdate(){return"update";}publicString }

returnPrivateStringid;PrivateStringname;//加getset方配置文件設(shè) 與ssi框架中的一樣<?xmlversion="1.0"encoding="UTF-beans-<beanid="propertyConfigurer"perty<property<bean destroy-<property<property<property<property<beanid="sessionFactory"<property<ref<propertyname="map <property<beanid="dbDao"<property<ref<beanid="studentService"<property<ref<beanid="studentAction"<property<ref<!----<beanid="sy_gy_sybgService"<property<ref<beanid="yqAction"<property<ref <?xmlversion="1.0"<!DOCTYPEhibernate-"-PUBLIC"-//Hibernate/HibernateMapDTD"-<hibernate-map<!--創(chuàng)建實(shí)體對(duì)應(yīng)數(shù)據(jù)庫中的表。name是實(shí)體的類路是數(shù)據(jù)庫中的表名。<classname="com.qr.xm.student.vo.StudentEntity"<!--實(shí)體對(duì)應(yīng)表中的主鍵,name是實(shí)體中的是表中的字段。<idname="id"<generator<!--name是實(shí)體中的屬性,column是表中的字段。<!--如果屬性名稱和表中的一樣,column可以省略。<propertyname="name"</hibernate-map<?xmlversion="1.0"encoding="UTF-<web-app <filter-name>struts2</filter-<filter-</filter-<filter-map<filter-name>struts2</filter-<url-pattern>/*</url-</filter-map</web-3、springMVC框publicinterfaceDbDao根據(jù)sql查詢列表數(shù)據(jù)(查詢一條),不支持預(yù)編譯的@param@throws@throwspublicMap<String,Object>executeQueryForMap(Stringsql)throwsClassNotFoundException,根據(jù)sql查詢列表數(shù)據(jù)(查詢一條),支持預(yù)編譯的方@param@param@param@throws@throwspublicMap<String,Object>executeQueryForMap(Stringsql,int[]types,Object[]values)throwsClassNotFoundException,SQLException;根據(jù)sql查詢列表數(shù)據(jù)(查詢多條),不支持預(yù)編譯的@param@throws@throwspublicList<Map<String,Object>>executeQueryForList(Stringsql)throwsClassNotFoundException,SQLException;根據(jù)sql查詢列表數(shù)據(jù)(查詢多條),支持預(yù)編譯的方@param@param@param@throws@throwspublicList<Map<String,Object>>executeQueryForList(Stringsql,int[]types,Object[]values)throwsClassNotFoundException,SQLException;執(zhí)行增、刪、改、等的操作,不支持預(yù)編譯的方@param@throws@throwspublicintexecuteUpdate(Stringsql)ClassNotFoundException,SQLException預(yù)編譯sql操作 支持insert,update,語@param@param@param@throws@throws@throws@throwspublicintexecuteUpdate(Stringsql,int[]types,Object[]values)throwsClassNotFoundException,SQLException,FileNotFoundException,IOException;預(yù)編譯sql操作 支持select語@param@param@param@throws@throwspublicintexecuteQueryForCount(StringsqlthrowsClassNotFoundException,SQLException預(yù)編譯sql操作 支持select語@param@param@param@throws@throwspublicintexecuteQueryForCount(Stringsql,int[]types,Object[]values)throwsClassNotFoundException,SQLException;}publicclassDbDaoImpleimplementsDbDaoprivatejavax.sql.DataSourcepublicjavax.sql.DataSourcegetDataSource()return}publicvoidsetDataSource(javax.sql.DataSourcedataSource){this.dataSource=}建立@throws@throwspublicConnectiongetConnection()SQLExceptionreturn}根據(jù)sql查詢列表數(shù)據(jù)(查詢一條),不支持預(yù)編譯的方@param@throws@throwspublicMap<String,Object>executeQueryForMap(Stringsql)throwsClassNotFoundException,SQLException{Connectionconnect=this.getConnection();Statementstmt=connect.createStatement();ResultSetrs=stmt.executeQuery(sql);List<Map<String,Object>>list=if(!list.isEmpty())return}this.releaseConnection(rs,stmt,關(guān)閉連return}根據(jù)sql查詢列表數(shù)據(jù)(查詢一條),支持預(yù)編譯的方@param@param@param@throws@throwspublicMap<String,Object>executeQueryForMap(Stringsql,int[]types,Object[]values)ClassNotFoundException,SQLException{Connectionconnect=this.getConnection();PreparedStatementpst=if(types!=null)for(inti=0;i<types.length;i++)switch(types[i])caseTypes.VARCHAR:pst.setString(i+1,caseTypes.INTEGER:pst.setInt(i+1,}}}ResultSetrs=pst.executeQuery();List<Map<String,Object>>list=if(!list.isEmpty())return}this.releaseConnection(rs,pst,return}根據(jù)sql查詢列表數(shù)據(jù),不支持預(yù)編譯的方@param@throws@throwspublicList<Map<String,Object>>executeQueryForList(Stringsql)throwsClassNotFoundException,SQLException{Connectionconnect=this.getConnection();Statementstmt=connect.createStatement();ResultSetrs=stmt.executeQuery(sql);List<Map<String,Object>>list=this.releaseConnection(rs,stmt,關(guān)閉連return}執(zhí)行增、刪、改、等的操作,不支持預(yù)編譯的方@param@throws@throwspublicintexecuteUpdate(Stringsql)SQLExceptionConnectionconnect=this.getConnection();Statementstmt=connect.createStatement();intcount=stmt.executeUpdate(sql);this.releaseConnection(stmt,connect);//關(guān)連return}根據(jù)sql查詢列表數(shù)據(jù),支持預(yù)編譯的方@param@param@param@throws@throwspublicList<Map<String,Object>>executeQueryForList(Stringsql,int[]types,Object[]values)SQLExceptionConnectionconnect=this.getConnection();PreparedStatementpst=if(types!=null)for(inti=0;i<types.length;i++)switch(types[i])caseTypes.VARCHAR:pst.setString(i+1,caseTypes.INTEGER:pst.setInt(i+1,}}}ResultSetrs=pst.executeQuery();List<Map<String,Object>>list=this.releaseConnection(rs,pst,return}預(yù)編譯sql操作持insertupdatedelete句@param@param@param@throws@throws@throwspublicintexecuteUpdate(Stringsql,types,Object[]throwsClassNotFoundException,SQLException,IOException{Connectionconnect=this.getConnection();PreparedStatementpst=if(types!=null)for(inti=0;i<types.length;i++)switch(types[i])caseTypes.VARCHAR:pst.setString(i+1,caseTypes.INTEGER:pst.setInt(i+1,caseTypes.BLOB:InputStreamin=newFileInputStream((File)pst.setBinaryStream(i+1,in,}}}intcount=pst.executeUpdate();this.releaseConnection(pst,connect);returncount;}查詢一個(gè)整數(shù),例如記錄總數(shù)(不支持預(yù)編譯@param@param@param@throws@throwspublicintexecuteQueryForCount(StringthrowsClassNotFoundException,SQLException{Connectionconnect=this.getConnection();Statementstmt=connect.createStatement();ResultSetrs=stmt.executeQuery(sql);if(rs.next())return}this.releaseConnection(rs,stmt,return}查詢一個(gè)整數(shù),例如記錄總數(shù)(支持預(yù)編譯@param@param@param@throws@throwspublicintexecuteQueryForCount(Stringint[]types,Object[]throwsClassNotFoundException,SQLException{Connectionconnect=this.getConnection();PreparedStatementpst=if(types!=null)for(inti=0;i<types.length;i++)switch(types[i])caseTypes.VARCHAR:pst.setString(i+1,caseTypes.INTEGER:pst.setInt(i+1,}}}ResultSetrs=if(rs.next())return}this.releaseConnection(rs,pst,return}將ResultSet中的結(jié)果包裝成list中裝Map@author李世@time2011-05-@param@throwsprivateList<Map<String,Object>>rsToList(ResultSetrs)throwsSQLException{List<Map<String,Object>>row=newArrayList<Map<String,while(rs.next())Map<String,Object>col=HashMap<String,for(inti=1;i<=rs.getMetaData().getColumnCount();i++){System.out.println(rs.getMetaData().getColumnType(i));(rs.getMetaData().getColumnType(i))casecasecaseTypes.BLOB:InputStreamin=col.put(rs.getMetaData().getColumnName(i),}}}return}privatevoidreleaseConnection(Connectionconnect)throwsSQLException{tryif(connect!=&&!connect.isClosed())}}catch(SQLExceptionse)System.out.println("Closetheconnectionencountererror!\n"+thrownewSQLException("關(guān)閉連接異常}}privatevoidreleaseConnection(Statementstmt,Connectionconnect)throwsSQLExceptiontryif(stmt!=null){}if(connect!=&&!connect.isClosed())}}catch(SQLExceptionse)System.out.println("Closetheconnectionencountererror!\n"+thrownewSQLException("關(guān)閉連接異常}}privatevoidreleaseConnection(PreparedStatementpst,Connectionconnect)throwsSQLExceptiontryif(pst!=null){}if(connect!=&&!connect.isClosed())}}catch(SQLExceptionse)System.out.println("Closetheconnectionencountererror!\n"+thrownewSQLException("關(guān)閉連接異常}}privatevoidreleaseConnection(ResultSetrs,Statementstmt,Connectionconnect)throwsSQLExceptiontryif(rs!=null)}if(stmt!=null){}if(connect!=&&!connect.isClosed())}}catch(SQLExceptionse)System.out.println("Closetheconnectionencountererror!\n"+thrownewSQLException("關(guān)閉連接異常}}privatevoidreleaseConnection(ResultSetrs,PreparedStatementpst,Connectionconnect)throwsSQLExceptiontryif(rs!=null)}if(pst!=null){}if(connect!=&&!connect.isClosed())}}catch(SQLExceptionse)System.out.println("Closetheconnectionencountererror!\n"+thrownewSQLException("關(guān)閉連接異常}}}publicinterfaceStudentDaopublicList<StudentEntity>studentList()publicvoidadd(StudentEntitythrowspublicvoidupdate(StudentEntitythrowspublicStudentEntityqueryone(Stringid)publicvoidaddone()throwspublicvoiddelete(Stringid)}publicclassStudentDaoImpleimplements{privateSqlMapsqlMappublicvoidaddone()throwsSQLException//TODOAuto-generatedmethod}publicvoidadd(StudentEntitythrowsSQLException//TODOAuto-generatedmethodthrownew}publicvoiddelete(Stringid)SQLException//TODOAuto-generatedmethod}publicStudentEntityqueryone(Stringid)SQLExceptionTODOAuto-generatedmethodstubreturn(StudentEntity)yId",id);}publicList<StudentEntity>studentList()SQLException//TODOAuto-generatedmethod}publicvoidupdate(StudentEntitythrowsSQLException//TODOAuto-generatedmethod}public getSqlMap()return }publicvoidsetSqlMap(SqlMapsqlMap){this.sqlMap=sqlMap}}publicinterfaceStudentServicepublicList<StudentEntity>studentList()SQLException,publicvoidadd(StudentEntitythrowsSQLException,publicvoidupdate(StudentEntitythrowsSQLException,publicvoiddelete(Stringid)SQLException,publicStudentEntityqueryone(Stringid)SQLException,publicvoidaddone()throwsSQLException,}publicclassStudentServiceImpleStudentService{privateStudentDaopublicvoidadd(StudentEntitythrowsException//TODOAuto-generatedmethodstub}publicvoidaddone()throwsException//TODOAuto-generatedmethod}publicvoiddelete(Stringid)throwsException//TODOAuto-generatedmethodstub}publicStudentEntityqueryone(Stringid)Exception//TODOAuto-generatedmethodreturn}publicList<StudentEntity>studentList()Exception//TODOAuto-generatedmethodreturn}publicvoidupdate(StudentEntitythrowsException//TODOAuto-generatedmethodstub}}publicclassStudentAction{privateStudentServicestudentService;privateList<StudentEntity>studentList;privateStudentEntitystudentEntitypublicModelAndViewquery(){ModelAndViewmode=try{}catch(ArithmeticExceptione)//TODOAuto-generatedcatchblock}catch(Exception{}return}@RequestMappublicModelAndView{ModelAndViewmode=return}@RequestMappublicStringadd(HttpServletRequestrequest,HttpServletResponseresp){ModelAndViewmode=try{Stringname=request.getParameter("name");}catch(Exceptione)//TODOAuto-generatedcatchblock}return} publicModelAndViewupdate(HttpServletRequestrequest,HttpServletResponsereponse){ tryrequest.setCharacterEncoding("utf-Stringid=request.getParameter("id");}catch(SQLExceptione)//TODOAuto-generatedcatchblock}catch(Exceptione)//TODOAuto-generatedcatchblock}return}@RequestMappublicModelAndViewqueryone(Stri

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論