使用JSP+SERVLET+JDBC实现对数据库的增删改查(详细)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

使用JSP+SERVLET+JDBC实现对数据库的增删改查首先,打开sql*plus,输入用户名(我用的scott)密码(我设置的是tiger)。先建个表student,Createtablestudent(idnumber(30)notnullprimarykey,namevarchar(50),agenumber(30),gendervarchar(30),majorvarchar(50));1,打开myeclipse(我用的是myeclipse8.5)新建一个webproject2,在projectname中输入合法名字,比如normal3,新建的normal工程4,在src目录下建一个包,右击src选择new在选择package5,输入合法名字比如bean6,在bean目录下建一个class,右击bean选择new再选择class7,输入名字Page完整的Page.java代码如下packagebean;publicclassPage{privateinttotalPage;privateintcurrentPage;privateinttotalRecord;privateintcurrentRecord;privateintpageSize=8;//获得和设置当前页publicintgetCurrentPage(){returncurrentPage;}publicvoidsetCurrentPage(intcurrentRecord,intpageSize){if(currentRecord%pageSize==0){currentPage=currentRecord/pageSize;}else{currentPage=currentRecord/pageSize+1;}}//获得和设置当前记录publicintgetCurrentRecord(){returncurrentRecord;}publicvoidsetCurrentRecord(intcurrentRecord){this.currentRecord=currentRecord;}//获得和设置每页记录数量publicintgetPageSize(){returnpageSize;}publicvoidsetPageSize(intpageSize){this.pageSize=pageSize;}//获得和设置总页数publicintgetTotalPage(){returntotalPage;}publicvoidsetTotalPage(inttotalRecord,intpageSize){if(totalRecord%pageSize==0){totalPage=totalRecord/pageSize;}else{totalPage=totalRecord/pageSize+1;}}//获得和设置总记录publicintgetTotalRecord(){returntotalRecord;}publicvoidsetTotalRecord(inttotalRecord){this.totalRecord=totalRecord;}}8,用相同的方法建一个StudentInfo类完整的StudentInfo.java代码如下packagebean;publicclassStudentInfo{privateintid;//学号privateStringname;//姓名privateintage;//年龄privateStringgender;//性别privateStringmajor;//专业publicStudentInfo(){}publicStudentInfo(intid,Stringname,intage,Stringgender,Stringmajor){this.id=id;this.name=name;this.age=age;this.gender=gender;this.major=major;}publicintgetId(){returnid;}publicvoidsetId(intid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicintgetAge(){returnage;}publicvoidsetAge(intage){this.age=age;}publicStringgetGender(){returngender;}publicvoidsetGender(Stringgender){this.gender=gender;}publicStringgetMajor(){returnmajor;}publicvoidsetMajor(Stringmajor){this.major=major;}}9,在src目录下添加另一个包dbservlet在该包中建立一个AllServlet类完整的AllServlet.java代码如下packagedbservlet;importjava.io.IOException;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;importjava.util.ArrayList;importjava.util.List;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importbean.Page;importbean.StudentInfo;publicclassAllServletextendsHttpServlet{/****/privatestaticfinallongserialVersionUID=1L;//doPost方法publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{request.setCharacterEncoding(UTF-8);response.setCharacterEncoding(UTF-8);StringmethodName=request.getParameter(methodName);intmethod=Integer.parseInt(methodName);try{switch(method){case0:insert(request,response);case1:difpage(request,response);break;case2:delete(request,response);break;case3:update(request,response);break;case4:update1(request,response);break;case5:dispatch(request,response);break;}}catch(ClassNotFoundExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}catch(SQLExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}//doGet方法publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doPost(request,response);}//数据库连接方法publicConnectionconnect()throwsClassNotFoundException,SQLException{Connectionconn=null;Class.forName(oracle.jdbc.driver.OracleDriver);Stringurl=jdbc:oracle:thin:@localhost:1521:orcl;Stringuser=scott;Stringpassword=tiger;conn=DriverManager.getConnection(url,user,password);returnconn;}//关闭数据库资源publicvoidclose(Statementstat,Connectionconn)throwsSQLException{if(stat!=null){stat.close();}if(conn!=null){conn.close();}}//插入方法publicvoidinsert(HttpServletRequestrequest,HttpServletResponseresponse)throwsClassNotFoundException,SQLException{Connectionconn=null;Statementstat=null;Stringid=request.getParameter(id);Stringname=request.getParameter(name);Stringage=request.getParameter(age);Stringgender=request.getParameter(gender);Stringmajor=request.getParameter(major);conn=connect();stat=conn.createStatement();stat.execute(insertintostudent(id,name,age,gender,major)values(+id+,'+name+',+age+,'+gender+','+major+'));close(stat,conn);}//查询方法publicArrayListStudentInfoselect(Stringid,Stringname)throwsClassNotFoundException,SQLException{Connectionconn=null;Statementstat=null;ResultSetrs=null;conn=connect();stat=conn.createStatement();ArrayListStudentInforesult=newArrayListStudentInfo();if(id==&&name==){rs=stat.executeQuery(select*fromstudent);}if(id!=&&name==){rs=stat.executeQuery(select*fromstudentwhereid=+id+);}if(id==&&name!=){rs=stat.executeQuery(select*fromstudentwherename='+name+');}if(id!=&&name!=){rs=stat.executeQuery(select*fromstudentwhereid=+id+andname='+name+');}while(rs.next()){StudentInfost=newStuden

1 / 24
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功