使用JSPSERVLETJDBC实现对数据库的增删改查Word文档下载推荐.docx
《使用JSPSERVLETJDBC实现对数据库的增删改查Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《使用JSPSERVLETJDBC实现对数据库的增删改查Word文档下载推荐.docx(29页珍藏版)》请在冰豆网上搜索。
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;
//XX
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..Servlet;
importjavax.servlet..ServletRequest;
importjavax.servlet..ServletResponse;
importbean.Page;
importbean.StudentInfo;
publicclassAllServletextendsServlet{
/**
*
*/
privatestaticfinallongserialVersionUID=1L;
//doPost方法
publicvoiddoPost(ServletRequestrequest,ServletResponseresponse)
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(ServletRequestrequest,ServletResponseresponse)
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(ServletRequestrequest,ServletResponseresponse)throwsClassNotFoundException,SQLException{