使用JSPSERVLETJDBC实现对数据库的增删改查精Word文档格式.docx
- 文档编号:22629341
- 上传时间:2023-02-04
- 格式:DOCX
- 页数:25
- 大小:172.75KB
使用JSPSERVLETJDBC实现对数据库的增删改查精Word文档格式.docx
《使用JSPSERVLETJDBC实现对数据库的增删改查精Word文档格式.docx》由会员分享,可在线阅读,更多相关《使用JSPSERVLETJDBC实现对数据库的增删改查精Word文档格式.docx(25页珍藏版)》请在冰豆网上搜索。
//获得和设置当前记录
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;
case3:
update(request,response;
case4:
update1(request,response;
case5:
dispatch(request,response;
}catch(ClassNotFoundExceptione{
//TODOAuto-generatedcatchblock
e.printStackTrace(;
}catch(SQLExceptione{
}
//doGet方法
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse
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,ConnectionconnthrowsSQLException{
if(stat!
=null{
stat.close(;
if(conn!
conn.close(;
//插入方法
publicvoidinsert(HttpServletRequestrequest,HttpServletResponseresponsethrowsClassNotFoundException,SQLException{
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,majorvalues("
+id+"
'
"
+name+"
'
"
+age+"
+gender+"
+major+"
close(stat,conn;
//查询方法
publicArrayListselect(Stringid,StringnamethrowsClassNotFoundException,SQLException{
ResultSetrs=null;
ArrayListresult=newArrayList(;
if(id=="
&
name=="
rs=stat.executeQuery("
select*fromstudent"
if(id!
="
select*fromstudentwhereid="
name!
select*fromstudentwherename='
andname='
while(rs.next(
StudentInfost=newStudentInfo(;
st.setId(rs.getInt("
st.setName(rs.getString("
st.setAge(rs.getInt("
st.setGender(rs.getString("
st.setMajor(rs.getString("
result.add(st;
if(rs!
rs.close(;
returnresult;
//条件查询跳转
publicvoiddispatch(HttpServletRequestrequest,HttpServletResponseresponsethrowsClassNotFoundException,SQLException,ServletException,IOException{
Stringid5=request.getParameter("
Stringname5=request.getParameter("
if(select(id5,name5.isEmpty({
request.getRequestDispatcher("
selectnothing.jsp"
.forward(request,response;
request.setAttribute("
result"
select(id5,name5;
idnameselect.jsp"
//设置分页相关参数方法
publicPagesetpage(HttpServletRequestrequest,HttpServletResponseresponsethrowsClassNotFoundException,SQLException{
Stringcrd=request.getParameter("
currentRecord"
//Stringid=request.getParameter("
//Stringname=request.getParameter("
ArrayListresult=select("
Pagepager=newPage(;
pager.setTotalRecord(result.size(;
pager.setTotalPage(result.size(,pager.getPageSize(;
if(crd!
=null
intcurrentRecord=Integer.parseInt(crd;
pager.setCurrentRecord(currentRecord;
pager.setCurrentPage(currentRecord,pager.getPageSize(;
returnpager;
//获得分页显示的子集
publicvoiddifpage(HttpServletRequestrequest,HttpServletResponseresponsethrowsServletException,IOException,ClassNotFoundException,SQLException{
//Stringid=request.getParameter("
pager=setpage(request,response;
ListsubResult=null;
intcurrentRecord=pager.getCurrentRecord(;
if(currentRecord==0{
if(pager.getTotalRecord(<
8{
subResult=(Listresult.subList(0,pager.getTotalRecord(;
subResult=(Listresult.subList(0,pager.getPageSize(;
elseif(pager.getCurrentRecord(+pager.getPageSize(
subResult=(Listresult.subList(pager.getCurrentRecord(,pager.getCurrentRecord(+pager.getPageSize(;
else
subResult=(Listresult.subList(pager.getCurrentRecord(,result.size(;
pager"
pager;
subResult"
subResult;
layout.jsp"
//信息删除方法
publicvoiddelete(HttpServletRequestrequest,HttpServletResponseresponsethrowsClassNotFoundException,SQLException,ServletException,IOException{
Stringid2=request.getParameter("
deletefromstudentwhereid="
+id2+"
delete.jsp"
//信息修改方法
publicvoidupdate1(HttpServletRequestrequest,HttpServletResponseresponsethrowsClassNotFoundException,SQLException,ServletException,IOException{
Stringid4=request.getParameter("
select(id4,"
update1.jsp"
publicvoidupdate(HttpServletRequestrequest,HttpServletResponseresponsethrowsClassNotFoundException,SQLException,ServletException,IOException{
Stringid3=request.getParameter("
Stringname3=request.getParameter("
Stringage3=request.getParameter("
Stringgender3=request.getParameter("
Stringmajor3=request.getParameter("
updatestudentsetid="
+id3+"
name='
+name3+"
age="
+age3+"
gender='
+gender3+"
major='
+major3+"
whereid="
select(id3,"
update.jsp"
10,在webRoot目录下添加以下.jsp文件
10.1putin.jsp
<
%@pagelanguage="
java"
import="
java.util.*"
pageEncoding="
%>
%
Stringpath=request.getContextPath(;
StringbasePath=request.getScheme(+"
:
//"
+request.getServerName(+"
+request.getServerPort(+path+"
/"
学生信息输入
学号:
学号必须为数字"
>
姓名:
姓名不能为空"
年龄:
年龄必须为数字"
性别:
男"
男
女"
女
专业:
专业不能为空"
提交"
/>
name=<
%="
%>
查看已输入信息
10.2layout.jsp
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 使用 JSPSERVLETJDBC 实现 数据库 增删 改查精