JSP SERVLET JDBC实现对数据库的增删改查课案Word文档格式.docx
《JSP SERVLET JDBC实现对数据库的增删改查课案Word文档格式.docx》由会员分享,可在线阅读,更多相关《JSP SERVLET JDBC实现对数据库的增删改查课案Word文档格式.docx(31页珍藏版)》请在冰豆网上搜索。
![JSP SERVLET JDBC实现对数据库的增删改查课案Word文档格式.docx](https://file1.bdocx.com/fileroot1/2022-10/13/19884e75-6410-4206-9384-988c89620d54/19884e75-6410-4206-9384-988c89620d541.gif)
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;
totalPage=totalRecord/pageSize+1;
//获得和设置总记录
publicintgetTotalRecord(){
returntotalRecord;
publicvoidsetTotalRecord(inttotalRecord){
this.totalRecord=totalRecord;
}
8,用相同的方法建一个StudentInfo类
完整的StudentInfo.java代码如下
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){
publicStringgetName(){
returnname;
publicvoidsetName(Stringname){
publicintgetAge(){
returnage;
publicvoidsetAge(intage){
publicStringgetGender(){
returngender;
publicvoidsetGender(Stringgender){
publicStringgetMajor(){
returnmajor;
publicvoidsetMajor(Stringmajor){
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("
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);
case5:
dispatch(request,response);
}catch(ClassNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(SQLExceptione){
}
//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!
conn.close();
//插入方法
publicvoidinsert(HttpServletRequestrequest,HttpServletResponseresponse)throwsClassNotFou