第83节:Java中的学生管理系统分页功能
程序员文章站
2022-06-21 11:13:04
第83节:Java中的学生管理系统分页功能 分页功能一般可以做成两种,一种是物理分页,另一种是逻辑分页。这两种功能是有各自的特点的,物理分页是查询的时候,对数据库进行访问,只是查一页数据就进行返回,其特点是对内存中数据量存储不大,只是缺点就是要对数据库不断的进行访问;而对逻辑分页来说,就有所不同,它 ......
第83节:java中的学生管理系统分页功能
分页功能一般可以做成两种,一种是物理分页,另一种是逻辑分页。这两种功能是有各自的特点的,物理分页是查询的时候,对数据库进行访问,只是查一页数据就进行返回,其特点是对内存中数据量存储不大,只是缺点就是要对数据库不断的进行访问;而对逻辑分页来说,就有所不同,它是一下子就把所有的数据全部查询出来,然后放入到内存中,访问速度快,缺点就是对内存空间不足,数据量过大。
select * from stu limit 5;
// offset 偏移前面的多少条,offset 1 跳过前面的一条 select * from stu limit 5 offset 5;
select * from stu limit 5 , 5;
select * from stu limit 5 , 2;
select * from stu limit 2 , 5;
写分页的dao模式
// index.jsp <h3><a href="studentlistpageservlet?currentpage=1">分页显示所有学生</a></h3>
// studentlistpageservlet //1. 获取需要显示的页码数 int currentpage =integer.parseint( request.getparameter("currentpage"));
// studentdao // 接口中定义的成员都是常量 // 一页显示多少条记录 int page_size = 5; // 分页dao,查询当页的学生数据 list<student> findstudentbypage(int currentpage) throws sqlexception;
// studentdaoimpl @override public list<student> findstudentbypage(int currentpage) throws sqlexception { // todo auto-generated method stub queryrunner runner = new queryrunner(jdbcutil02.getdatasource()); // 第一个问号,一页返回多少条记录,第二个问号,跳过前面的多少条记录 //5 0 --- 第一页 (1-1)*5 //5 5 --- 第二页 (2-1)*5 //5 10 --- 第三页 return runner.query("select * from stu limit ? offset ?", new beanlisthandler<student>(student.class),page_size , (currentpage-1)*page_size); }
业务逻辑
select count(*) from stu;
业务逻辑
package com.dashucoding.servlet; import java.io.ioexception; import java.sql.sqlexception; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import com.dashucoding.domain.pagebean; import com.dashucoding.service.studentservice; import com.dashucoding.service.impl.studentserviceimpl; /** * 这是用于分页显示学生列表的servlet */ public class studentlistpageservlet extends httpservlet { protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { try { //1. 获取需要显示的页码数 int currentpage =integer.parseint( request.getparameter("currentpage")); //2. 根据指定的页数,去获取该页的数据回来 //list<student> --- list.jsp studentservice service = new studentserviceimpl(); pagebean pagebean= service.findstudentbypage(currentpage); request.setattribute("pagebean", pagebean); //3. 跳转界面。 request.getrequestdispatcher("list_page.jsp").forward(request, response); } catch (sqlexception e) { e.printstacktrace(); } } protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // todo auto-generated method stub doget(request, response); } }
去业务逻辑去找
package com.dashucoding.service; import java.sql.sqlexception; import java.util.list; import com.dashucoding.domain.pagebean; import com.dashucoding.domain.student; /* * 这是学生的业务处理规范 * */ public interface studentservice { // 分页dao,查询当页的学生数据 // 分页的很多小逻辑业务 可以做到service里面 // 业务做到service里面,做的就是bean了,当前页,总页数,显示条数, 总记录数 // 返回的是一个bean了 // 返回的是pagebean里面的所有数据了 pagebean findstudentbypage(int currentpage) throws sqlexception; // 根据姓名或性别,查询 list<student> searchstudent(string sname, string sgender) throws sqlexception; /* * 查询所有学生 list<student> */ list<student> findall() throws sqlexception; void insert(student student) throws sqlexception; // sid根据id删除学生 void delete(int sid) throws sqlexception; // 根据id查询单个学生对象 student findstudentbyid(int sid) throws sqlexception; // 更新学生信息 void update(student student) throws sqlexception; }
package com.dashucoding.service.impl; import java.sql.sqlexception; import java.util.list; import com.dashucoding.dao.studentdao; import com.dashucoding.dao.impl.studentdaoimpl; import com.dashucoding.domain.pagebean; import com.dashucoding.domain.student; import com.dashucoding.service.studentservice; /* * 这是学生业务实现 * */ public class studentserviceimpl implements studentservice { @override public list<student> findall() throws sqlexception { studentdao dao = new studentdaoimpl(); return dao.findall(); } @override public void insert(student student) throws sqlexception { // todo auto-generated method stub studentdao dao = new studentdaoimpl(); dao.insert(student); } @override public void delete(int sid) throws sqlexception { // todo auto-generated method stub studentdao dao = new studentdaoimpl(); dao.delete(sid); } @override public student findstudentbyid(int sid) throws sqlexception { // todo auto-generated method stub studentdao dao = new studentdaoimpl(); return dao.findstudentbyid(sid); } @override public void update(student student) throws sqlexception { // todo auto-generated method stub studentdao dao = new studentdaoimpl(); dao.update(student); } @override public list<student> searchstudent(string sname, string sgender) throws sqlexception { // todo auto-generated method stub studentdao dao = new studentdaoimpl(); return dao.searchstudent(sname, sgender); } @override public pagebean findstudentbypage(int currentpage) throws sqlexception { // todo auto-generated method stub // 封装分页的该页的数据 pagebean<student> pagebean = new pagebean<student>(); int pagesize = studentdao.page_size; // 设置当前页 pagebean.setcurrentpage(currentpage); // 每条记录 pagebean.setpagesize(pagesize); studentdao dao = new studentdaoimpl(); list<student> list = dao.findstudentbypage(currentpage); pagebean.setlist(list); // 总记录数,总页数 int count = dao.findcount(); pagebean.settotalsize(count); // 总页数 pagebean.settotalpage(count % pagesize == 0 ? count / pagesize : (count / pagesize) + 1); return null; } }
// 封装的数据 package com.dashucoding.domain; import java.util.list; // 一个用于封装了分页的数据 // 有: 当前学生集合数据,总的记录数,总的页数,当前页,每页的显示记录数 public class pagebean<t> { private int currentpage; // 当前页 private int totalpage;// 总页数 private int pagesize;// 每页的记录数,每页要显示多少记录 private int totalsize; // 总的记录数 private list<t> list; // 当前页的学生集合 public int getcurrentpage() { return currentpage; } public void setcurrentpage(int currentpage) { this.currentpage = currentpage; } public int gettotalpage() { return totalpage; } public void settotalpage(int totalpage) { this.totalpage = totalpage; } public int getpagesize() { return pagesize; } public void setpagesize(int pagesize) { this.pagesize = pagesize; } public int gettotalsize() { return totalsize; } public void settotalsize(int totalsize) { this.totalsize = totalsize; } public list<t> getlist() { return list; } public void setlist(list<t> list) { this.list = list; } }
最后就靠显示页面逻辑
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>学生列表页面</title> <script type="text/javascript"> function dodelete(sid) { /* 如果这里弹出的对话框,用户点击的是确定,就马上去请求servlet。 如何知道用户点击的是确定。 如何在js的方法中请求servlet。 */ var flag = confirm("是否确定删除?"); if(flag){ //表明点了确定。 访问servlet。 在当前标签页上打开 超链接, //window.location.href="deleteservlet?sid="+sid; location.href="deleteservlet?sid="+sid; } } </script> </head> <body> <form action="searchstudentservlet" method="post"> <table border="1" width="700"> <tr > <td colspan="8"> 按姓名查询:<input type="text" name="sname"/> 按性别查询:<select name="sgender"> <option value="">--请选择-- <option value="男">男 <option value="女">女 </select> <input type="submit" value="查询"> <a href="add.jsp">添加</a> </td> </tr> <tr align="center"> <td>编号</td> <td>姓名</td> <td>性别</td> <td>电话</td> <td>生日</td> <td>爱好</td> <td>简介</td> <td>操作</td> </tr> <c:foreach items="${pagebean.list }" var="stu"> <tr align="center"> <td>${stu.sid }</td> <td>${stu.sname }</td> <td>${stu.gender }</td> <td>${stu.phone }</td> <td>${stu.birthday }</td> <td>${stu.hobby }</td> <td>${stu.info }</td> <td><a href="editservlet?sid=${stu.sid }">更新</a> <a href="#" onclick="dodelete(${stu.sid})">删除</a></td> </tr> </c:foreach> <tr> <td colspan="8"> 第 ${pagebean.currentpage } / ${pagebean.totalpage } 每页显示${pagebean.pagesize }条 总的记录数${pagebean.totalsize } <c:if test="${pagebean.currentpage !=1 }"> <a href="studentlistpageservlet?currentpage=1">首页</a> | <a href="studentlistpageservlet?currentpage=${pagebean.currentpage-1 }">上一页</a> </c:if> <c:foreach begin="1" end="${pagebean.totalpage }" var="i"> <c:if test="${pagebean.currentpage == i }"> ${i } </c:if> <c:if test="${pagebean.currentpage != i }"> <a href="studentlistpageservlet?currentpage=${i }">${i }</a> </c:if> </c:foreach> <c:if test="${pagebean.currentpage !=pagebean.totalpage }"> <a href="studentlistpageservlet?currentpage=${pagebean.currentpage+1 }">下一页</a> | <a href="studentlistpageservlet?currentpage=${pagebean.totalpage }">尾页</a> </c:if> </td> </tr> </table> </form> </body> </html>
我的源码
package com.dashucoding.dao; import java.sql.sqlexception; import java.util.list; import com.dashucoding.domain.student; /* * 这是针对学生表的数据访问 * * */ public interface studentdao { // 接口中定义的成员都是常量 // 一页显示多少条记录 int page_size = 5; // 分页dao,查询当页的学生数据 list<student> findstudentbypage(int currentpage) throws sqlexception; // 根据姓名或性别,查询 list<student> searchstudent(string sname, string sgender) throws sqlexception; /* * 查询所有学生 list<student> */ list<student> findall() throws sqlexception; void insert(student student) throws sqlexception; // sid根据id删除学生 void delete(int sid) throws sqlexception; // 根据id查询单个学生对象 student findstudentbyid(int sid) throws sqlexception; // 更新学生信息 void update(student student) throws sqlexception; // 查询总的学生记录数 int findcount() throws sqlexception; }
package com.dashucoding.dao.impl; import java.sql.connection; import java.sql.sqlexception; import java.util.arraylist; import java.util.list; import org.apache.commons.dbutils.queryrunner; import org.apache.commons.dbutils.handlers.beanhandler; import org.apache.commons.dbutils.handlers.beanlisthandler; import org.apache.commons.dbutils.handlers.scalarhandler; import com.dashucoding.dao.studentdao; import com.dashucoding.domain.student; import com.dashucoding.util.jdbcutil02; import com.dashucoding.util.textutils; /* *这是studentdao的实现,针对前面定义的规范,做出具体的实现 * */ public class studentdaoimpl implements studentdao { /* * 查询所有学生 */ @override public list<student> findall() throws sqlexception { queryrunner runner = new queryrunner(jdbcutil02.getdatasource()); return runner.query("select * from stu", new beanlisthandler<student>(student.class)); } @override public void insert(student student) throws sqlexception { // todo auto-generated method stub queryrunner runner = new queryrunner(jdbcutil02.getdatasource()); runner.update("insert into stu values(null, ?,?,?,?,?,?)", student.getsname(), student.getgender(), student.getphone(), student.getbirthday(), student.gethobby(), student.getinfo() ); } @override public void delete(int sid) throws sqlexception { // todo auto-generated method stub queryrunner runner = new queryrunner(jdbcutil02.getdatasource()); runner.update("delete from stu where sid=?", sid); } @override public student findstudentbyid(int sid) throws sqlexception { // todo auto-generated method stub queryrunner runner = new queryrunner(jdbcutil02.getdatasource()); return runner.query("select * from stu where sid = ?", new beanhandler<student>(student.class), sid); } @override public void update(student student) throws sqlexception { // todo auto-generated method stub queryrunner runner = new queryrunner(jdbcutil02.getdatasource()); runner.update("update stu set sname=?, gender=?, phone=?, birthday=?, hobby=?, info=? where sid=?", student.getsname(), student.getgender(), student.getphone(), student.getbirthday(), student.gethobby(), student.getinfo(), student.getsid()); } // 模糊查询 @override public list<student> searchstudent(string sname, string sgender) throws sqlexception { // todo auto-generated method stub /*system.out.println(sname + sgender);*/ queryrunner runner = new queryrunner(jdbcutil02.getdatasource()); /* * string sql = "select * from stu where sname=? or sgender=?"; * select * from stu where sname like ?; * select * from stu where gender = ?; * select * from stu where sname like ? and gender = ?; * 如果两个都没有就查询所有 * sql = "select * from stu" * if(姓名){ * sql = sql + "where sname like ?"; * } * if(性别){ * sql = sql + "where gender = ?"; * } * * string sql = "select * from stu where 1=1"; * if(姓名){ * sql = sql + " and sname like ? "; * } * if(性别){ * sql = sql + " and gender = ? "; * } * */ string sql = "select * from stu where 1=1"; list<string> list = new arraylist<string>(); if(!textutils.isempty(sname)) { sql = sql + " and sname like ? "; list.add("%"+sname+"%"); } if(!textutils.isempty(sgender)) { sql = sql + " and gender = ? "; list.add(sgender); } /*list.toarray()*/ return runner.query(sql, new beanlisthandler<student>(student.class),list.toarray()); } @override public list<student> findstudentbypage(int currentpage) throws sqlexception { // todo auto-generated method stub queryrunner runner = new queryrunner(jdbcutil02.getdatasource()); // 第一个问号,一页返回多少条记录,第二个问号,跳过前面的多少条记录 //5 0 --- 第一页 (1-1)*5 //5 5 --- 第二页 (2-1)*5 //5 10 --- 第三页 return runner.query("select * from stu limit ? offset ?", new beanlisthandler<student>(student.class),page_size , (currentpage-1)*page_size); } // 查询总的记录数 @override public int findcount() throws sqlexception { // todo auto-generated method stub queryrunner runner = new queryrunner(jdbcutil02.getdatasource()); // 用于处理平均值,总的个数 long result = (long) runner.query("select count(*) from stu", new scalarhandler()); return result.intvalue(); } }
package com.dashucoding.domain; import java.util.list; // 一个用于封装了分页的数据 // 有: 当前学生集合数据,总的记录数,总的页数,当前页,每页的显示记录数 public class pagebean<t> { private int currentpage; // 当前页 private int totalpage;// 总页数 private int pagesize;// 每页的记录数,每页要显示多少记录 private int totalsize; // 总的记录数 private list<t> list; // 当前页的学生集合 public int getcurrentpage() { return currentpage; } public void setcurrentpage(int currentpage) { this.currentpage = currentpage; } public int gettotalpage() { return totalpage; } public void settotalpage(int totalpage) { this.totalpage = totalpage; } public int getpagesize() { return pagesize; } public void setpagesize(int pagesize) { this.pagesize = pagesize; } public int gettotalsize() { return totalsize; } public void settotalsize(int totalsize) { this.totalsize = totalsize; } public list<t> getlist() { return list; } public void setlist(list<t> list) { this.list = list; } }
package com.dashucoding.domain; import java.util.date; /* * 这是学生封装的对象bean * * */ public class student { private int sid; private string sname; private string gender; private string phone; private string hobby; private string info; private date birthday; public student() { super(); // todo auto-generated constructor stub } public student(int sid, string sname, string gender, string phone, string hobby, string info, date birthday) { super(); this.sid = sid; this.sname = sname; this.gender = gender; this.phone = phone; this.hobby = hobby; this.info = info; this.birthday = birthday; } public student(string sname, string gender, string phone, string hobby, string info, date birthday) { super(); this.sname = sname; this.gender = gender; this.phone = phone; this.hobby = hobby; this.info = info; this.birthday = birthday; } public int getsid() { return sid; } public void setsid(int sid) { this.sid = sid; } public string getsname() { return sname; } public void setsname(string sname) { this.sname = sname; } public string getgender() { return gender; } public void setgender(string gender) { this.gender = gender; } public string getphone() { return phone; } public void setphone(string phone) { this.phone = phone; } public string gethobby() { return hobby; } public void sethobby(string hobby) { this.hobby = hobby; } public string getinfo() { return info; } public void setinfo(string info) { this.info = info; } public date getbirthday() { return birthday; } public void setbirthday(date birthday) { this.birthday = birthday; } @override public string tostring() { return "student [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", phone=" + phone + ", hobby=" + hobby + ", info=" + info + ", birthday=" + birthday + "]"; } }
package com.dashucoding.service; import java.sql.sqlexception; import java.util.list; import com.dashucoding.domain.pagebean; import com.dashucoding.domain.student; /* * 这是学生的业务处理规范 * */ public interface studentservice { // 分页dao,查询当页的学生数据 // 分页的很多小逻辑业务 可以做到service里面 // 业务做到service里面,做的就是bean了,当前页,总页数,显示条数, 总记录数 // 返回的是一个bean了 // 返回的是pagebean里面的所有数据了 pagebean findstudentbypage(int currentpage) throws sqlexception; // 根据姓名或性别,查询 list<student> searchstudent(string sname, string sgender) throws sqlexception; /* * 查询所有学生 list<student> */ list<student> findall() throws sqlexception; void insert(student student) throws sqlexception; // sid根据id删除学生 void delete(int sid) throws sqlexception; // 根据id查询单个学生对象 student findstudentbyid(int sid) throws sqlexception; // 更新学生信息 void update(student student) throws sqlexception; }
package com.dashucoding.service.impl; import java.sql.sqlexception; import java.util.list; import com.dashucoding.dao.studentdao; import com.dashucoding.dao.impl.studentdaoimpl; import com.dashucoding.domain.pagebean; import com.dashucoding.domain.student; import com.dashucoding.service.studentservice; /* * 这是学生业务实现 * */ public class studentserviceimpl implements studentservice { @override public list<student> findall() throws sqlexception { studentdao dao = new studentdaoimpl(); return dao.findall(); } @override public void insert(student student) throws sqlexception { // todo auto-generated method stub studentdao dao = new studentdaoimpl(); dao.insert(student); } @override public void delete(int sid) throws sqlexception { // todo auto-generated method stub studentdao dao = new studentdaoimpl(); dao.delete(sid); } @override public student findstudentbyid(int sid) throws sqlexception { // todo auto-generated method stub studentdao dao = new studentdaoimpl(); return dao.findstudentbyid(sid); } @override public void update(student student) throws sqlexception { // todo auto-generated method stub studentdao dao = new studentdaoimpl(); dao.update(student); } @override public list<student> searchstudent(string sname, string sgender) throws sqlexception { // todo auto-generated method stub studentdao dao = new studentdaoimpl(); return dao.searchstudent(sname, sgender); } @override public pagebean findstudentbypage(int currentpage) throws sqlexception { // todo auto-generated method stub // 封装分页的该页的数据 pagebean<student> pagebean = new pagebean<student>(); int pagesize = studentdao.page_size; // 设置当前页 pagebean.setcurrentpage(currentpage); // 每条记录 pagebean.setpagesize(pagesize); studentdao dao = new studentdaoimpl(); list<student> list = dao.findstudentbypage(currentpage); pagebean.setlist(list); // 总记录数,总页数 int count = dao.findcount(); pagebean.settotalsize(count); // 总页数 pagebean.settotalpage(count % pagesize == 0 ? count / pagesize : (count / pagesize) + 1); return null; } }
package com.dashucoding.servlet; import java.io.ioexception; import java.text.simpledateformat; import java.util.arrays; import java.util.date; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import com.dashucoding.domain.student; import com.dashucoding.service.studentservice; import com.dashucoding.service.impl.studentserviceimpl; /** * 用于处理学生的添加请求 */ public class addservlet extends httpservlet { protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { request.setcharacterencoding("utf-8"); try { // 1. 获取客户端提交上来的数据 string sname = request.getparameter("sname"); string gender = request.getparameter("gender"); string phone = request.getparameter("phone"); string birthday = request.getparameter("birthday"); string info = request.getparameter("info"); // string hobby = request.getparameter("hobby");//hobby : 游泳,写字, 足球。 string[] h = request.getparametervalues("hobby"); string hobby = arrays.tostring(h); hobby = hobby.substring(1, hobby.length() - 1); // 2. 添加到数据库 // string -- date date date = new simpledateformat("yyyy-mm-dd").parse(birthday); student student = new student(sname, gender, phone, hobby, info, date); studentservice service = new studentserviceimpl(); service.insert(student); // 3. 跳转到列表页 request.getrequestdispatcher("studentlistservlet").forward(request, response); } catch (exception e) { e.printstacktrace(); } } protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { doget(request, response); } }
package com.dashucoding.servlet; import java.io.ioexception; import java.sql.sqlexception; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import com.dashucoding.service.studentservice; import com.dashucoding.service.impl.studentserviceimpl; /** * 用于处理删除学生 */ public class deleteservlet extends httpservlet { protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { try { int sid = integer.parseint(request.getparameter("sid")); // system.out.println("sid="+sid); // 执行删除 studentservice service = new studentserviceimpl(); service.delete(sid); // 跳转到列表页 request.getrequestdispatcher("studentlistservlet").forward(request, response); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } } protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { doget(request, response); } }
package com.dashucoding.servlet; import java.io.ioexception; import java.sql.sqlexception; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import com.dashucoding.domain.student; import com.dashucoding.service.studentservice; import com.dashucoding.service.impl.studentserviceimpl; /** * 处理单个学生的更新,查询学生的信息,跳转到更新的页面 */ public class editservlet extends httpservlet { protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { try { // 接收id int sid = integer.parseint(request.getparameter("sid")); // 查询学生数据 studentservice service = new studentserviceimpl(); student stu = service.findstudentbyid(sid); // 显示数据 // 存储数据 request.setattribute("stu", stu); // 跳转 request.getrequestdispatcher("edit.jsp").forward(request, response); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } } /** * @see httpservlet#dopost(httpservletrequest request, httpservletresponse * response) */ protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // todo auto-generated method stub doget(request, response); } }
package com.dashucoding.servlet; import java.io.ioexception; import java.sql.sqlexception; import java.util.list; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import com.dashucoding.domain.student; import com.dashucoding.service.studentservice; import com.dashucoding.service.impl.studentserviceimpl; /** * servlet implementation class searchstudentservlet */ public class searchstudentservlet extends httpservlet { protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { request.setcharacterencoding("utf-8"); try { // 取到了要查询的关键数据 string sname = request.getparameter("sname"); string sgender = request.getparameter("sgender"); // 找service查询 studentservice service = new studentserviceimpl(); list<student> list = service.searchstudent(sname, sgender); /*for(student student : list) { system.out.println("stu=" + student); }*/ request.setattribute("list", list); // 跳转界面 request.getrequestdispatcher("list.jsp").forward(request, response); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } } protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // todo auto-generated method stub doget(request, response); } }
package com.dashucoding.servlet; import java.io.ioexception; import java.sql.sqlexception; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import com.dashucoding.domain.pagebean; import com.dashucoding.service.studentservice; import com.dashucoding.service.impl.studentserviceimpl; /** * 这是用于分页显示学生列表的servlet */ public class studentlistpageservlet extends httpservlet { protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { try { //1. 获取需要显示的页码数 int currentpage =integer.parseint( request.getparameter("currentpage")); //2. 根据指定的页数,去获取该页的数据回来 //list<student> --- list.jsp studentservice service = new studentserviceimpl(); pagebean pagebean= service.findstudentbypage(currentpage); request.setattribute("pagebean", pagebean); //3. 跳转界面。 request.getrequestdispatcher("list_page.jsp").forward(request, response); } catch (sqlexception e) { e.printstacktrace(); } } protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // todo auto-generated method stub doget(request, response); } }
package com.dashucoding.servlet; import java.io.ioexception; import java.sql.sqlexception; import java.util.list; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import com.dashucoding.dao.studentdao; import com.dashucoding.dao.impl.studentdaoimpl; import com.dashucoding.domain.student; import com.dashucoding.service.studentservice; import com.dashucoding.service.impl.studentserviceimpl; public class studentlistservlet extends httpservlet { protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { try { // 查询所有的学生 studentservice service = new studentserviceimpl(); list<student> list = service.findall(); // 把数据存储到作用域中 request.setattribute("list", list); // 跳转页面 request.getrequestdispatcher("list.jsp").forward(request,response); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } } protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { doget(request, response); } }
package com.dashucoding.servlet; import java.io.ioexception; import java.text.simpledateformat; import java.util.arrays; import java.util.date; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import com.dashucoding.domain.student; import com.dashucoding.service.studentservice; import com.dashucoding.service.impl.studentserviceimpl; /** * servlet implementation class updateservlet */ public class updateservlet extends httpservlet { protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { request.setcharacterencoding("utf-8"); try { // 1. 获取客户端提交上来的数据 int sid = integer.parseint(request.getparameter("sid")); string sname = request.getparameter("sname"); string gender = request.getparameter("gender"); string phone = request.getparameter("phone"); string birthday = request.getparameter("birthday"); string info = request.getparameter("info"); // string hobby = request.getparameter("hobby"); string[] h = request.getparametervalues("hobby"); string hobby = arrays.tostring(h); hobby = hobby.substring(1, hobby.length() - 1); // 2. 添加到数据库 date date = new simpledateformat("yyyy-mm-dd").parse(birthday); student student = new student(sid, sname, gender, phone, hobby, info, date); // 2. 更新数据库数据 studentservice service = new studentserviceimpl(); service.update(student); // 3. 跳转界面 request.getrequestdispatcher("studentlistservlet").forward(request, response); } catch (exception e) { e.printstacktrace(); } } protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // todo auto-generated method stub doget(request, response); } }
package com.dashucoding.util; import java.io.fileinputstream; import java.io.inputstream; import java.sql.connection; import java.sql.drivermanager; import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; import java.util.properties; import javax.sql.datasource; import com.mchange.v2.c3p0.combopooleddatasource; public class jdbcutil02 { static combopooleddatasource datasource = null; static { datasource = new combopooleddatasource(); } public static datasource getdatasource() { return datasource; } /** * 获取连接对象 * @return * @throws sqlexception */ public static connection getconn() throws sqlexception{ return datasource.getconnection(); } /** * 释放资源 * @param conn * @param st * @param rs */ public static void release(connection conn , statement st , resultset rs){ closers(rs); closest(st); closeconn(conn); } public static void release(connection conn , statement st){ closest(st); closeconn(conn); } private static void closers(resultset rs){ try { if(rs != null){ rs.close(); } } catch (sqlexception e) { e.printstacktrace(); }finally{ rs = null; } } private static void closest(statement st){ try { if(st != null){ st.close(); } } catch (sqlexception e) { e.printstacktrace(); }finally{ st = null; } } private static void closeconn(connection conn){ try { if(conn != null){ conn.close(); } } catch (sqlexception e) { e.printstacktrace(); }finally{ conn = null; } } }
package com.dashucoding.util; public class textutils { /** * 判断某一个字符串是否为空。 * * @param s * @return */ public static boolean isempty(charsequence s) { return s == null || s.length() == 0; } }
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <!doctype html> <html> <head> <meta charset="utf-8"> <title>添加学生页面</title> </head> <body> <form method="post" action="addservlet"> <table border="1" width="600"> <tr> <td>姓名</td> <td><input type="text" name="sname"></td> </tr> <tr> <td>性别</td> <td><input type="radio" name="gender" value="男">男 <input type="radio" name="gender" value="女">女</td> </tr> <tr> <td>电话</td> <td><input type="text" name="phone"></td> </tr> <tr> <td>生日</td> <td><input type="text" name="birthday"></td> </tr> <tr> <td>爱好</td> <td><input type="checkbox" name="hobby" value="游泳">游泳 <input type="checkbox" name="hobby" value="篮球">篮球 <input type="checkbox" name="hobby" value="足球">足球 <input type="checkbox" name="hobby" value="看书">看书 <input type="checkbox" name="hobby" value="写字">写字</td> </tr> <tr> <td>简介</td> <td><textarea name="info" rows="3" cols="20"></textarea></td> </tr> <tr> <td colspan="2"><input type="submit" value="添加"></td> </tr> </table> </form> </body> </html>
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>更新学生页面</title> </head> <body> <h3>更新学生页面</h3> <form method="post" action="updateservlet"> <input type="hidden" name="sid" value="${stu.sid }"> <table border="1" width="600"> <tr> <td>姓名</td> <td><input type="text" name="sname" value="${stu.sname }"></td> </tr> <tr> <td>性别</td> <td> <input type="radio" name="gender" value="男" <c:if test="${stu.gender == '男'}">checked</c:if>>男 <input type="radio" name="gender" value="女" <c:if test="${stu.gender == '女'}">checked</c:if>>女 </td> </tr> <tr> <td>电话</td> <td><input type="text" name="phone" value="${stu.phone }"></td> </tr> <tr> <td>生日</td> <td><input type="text" name="birthday" value="${stu.birthday }"></td> </tr> <tr> <td>爱好</td> <td> <input type="checkbox" name="hobby" value="游泳" <c:if test="${fn:contains(stu.hobby,'游泳') }">checked</c:if>>游泳 <input type="checkbox" name="hobby" value="篮球" <c:if test="${fn:contains(stu.hobby,'篮球') }">checked</c:if>>篮球 <input type="checkbox" name="hobby" value="足球" <c:if test="${fn:contains(stu.hobby,'足球') }">checked</c:if>>足球 <input type="checkbox" name="hobby" value="看书" <c:if test="${fn:contains(stu.hobby,'看书') }">checked</c:if>>看书 <input type="checkbox" name="hobby" value="写字" <c:if test="${fn:contains(stu.hobby,'写字') }">checked</c:if>>写字 </td> </tr> <tr> <td>简介</td> <td><textarea name="info" rows="3" cols="20">${stu.info }</textarea></td> </tr> <tr> <td colspan="2"> <input type="submit" value="更新"> </td> </tr> </table> </form> </body> </html>
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <!doctype html> <html> <head> <meta charset="utf-8"> <title>首页</title> </head> <body> <h3><a href="studentlistservlet">显示所有学生列表</a></h3><br> <h3><a href="studentlistpageservlet?currentpage=1">分页显示所有学生</a></h3> </body> </html>
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>学生列表页面</title> <script type="text/javascript"> function dodelete(sid) { // 弹出对话框,点击确定,请求servlet var flag = confirm("是否确定删除?"); if(flag){ //访问servlet //window.location.href="deleteservlet?sid="+sid; location.href="deleteservlet?sid="+sid; } } </script> </head> <body> <form action="searchstudentservlet" method="post"> <table border="1" width="700"> <tr > <td colspan="8"> 按姓名查询:<input type="text" name="sname"/> 按性别查询:<select name="sgender"> <option value="">--请选择-- <option value="男">男 <option value="女">女 </select> <input type="submit" value="查询"> <a href="add.jsp">添加</a> </td> </tr> <tr align="center"> <td>编号</td> <td>姓名</td> <td>性别</td> <td>电话</td> <td>生日</td> <td>爱好</td> <td>简介</td> <td>操作</td> </tr> <c:foreach items="${list }" var="stu"> <tr align="center"> <td>${stu.sid }</td> <td>${stu.sname }</td> <td>${stu.gender }</td> <td>${stu.phone }</td> <td>${stu.birthday }</td> <td>${stu.hobby }</td> <td>${stu.info }</td> <td><a href="editservlet?sid=${stu.sid }">更新</a> <a href="#" onclick="dodelete(${stu.sid})">删除</a></td> </tr> </c:foreach> </table> </form> </body> </html>
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>学生列表页面</title> <script type="text/javascript"> function dodelete(sid) { // 弹出对话框,点击确定,请求servlet var flag = confirm("是否确定删除?"); if(flag){ //访问servlet //window.location.href="deleteservlet?sid="+sid; location.href="deleteservlet?sid="+sid; } } </script> </head> <body> <form action="searchstudentservlet" method="post"> <table border="1" width="700"> <tr> <td colspan="8">按姓名查询:<input type="text" name="sname" /> 按性别查询:<select name="sgender"> <option value="">--请选择-- <option value="男">男 <option value="女">女 </select> <input type="submit" value="查询"> <a href="add.jsp">添加</a> </td> </tr> <tr align="center"> <td>编号</td> <td>姓名</td> <td>性别</td> <td>电话</td> <td>生日</td> <td>爱好</td> <td>简介</td> <td>操作</td> </tr> <c:foreach items="${pagebean.list }" var="stu"> <tr align="center"> <td>${stu.sid }</td> <td>${stu.sname }</td> <td>${stu.gender }</td> <td>${stu.phone }</td> <td>${stu.birthday }</td> <td>${stu.hobby }</td> <td>${stu.info }</td> <td><a href="editservlet?sid=${stu.sid }">更新</a> <a href="#" onclick="dodelete(${stu.sid})">删除</a></td> </tr> </c:foreach> <tr> <td colspan="8">第 ${pagebean.currentpage } / ${pagebean.totalpage } 每页显示${pagebean.pagesize }条 总的记录数${pagebean.totalsize } <c:if test="${pagebean.currentpage !=1 }"> <a href="studentlistpageservlet?currentpage=1">首页</a> | <a href="studentlistpageservlet?currentpage=${pagebean.currentpage-1 }">上一页</a> </c:if> <c:foreach begin="1" end="${pagebean.totalpage }" var="i"> <c:if test="${pagebean.currentpage == i }"> ${i } </c:if> <c:if test="${pagebean.currentpage != i }"> <a href="studentlistpageservlet?currentpage=${i }">${i }</a> </c:if> </c:foreach> <c:if test="${pagebean.currentpage !=pagebean.totalpage }"> <a href="studentlistpageservlet?currentpage=${pagebean.currentpage+1 }">下一页</a> | <a href="studentlistpageservlet?currentpage=${pagebean.totalpage }">尾页</a> </c:if> </td> </tr> </table> </form> </body> </html>
package com.dashucoding.service.impl; import java.sql.sqlexception; import java.util.list; import com.dashucoding.dao.studentdao; import com.dashucoding.dao.impl.studentdaoimpl; import com.dashucoding.domain.pagebean; import com.dashucoding.domain.student; import com.dashucoding.service.studentservice; /* * 这是学生业务实现 * */ public class studentserviceimpl implements studentservice { @override public list<student> findall() throws sqlexception { studentdao dao = new studentdaoimpl(); return dao.findall(); } @override public void insert(student student) throws sqlexception { // todo auto-generated method stub studentdao dao = new studentdaoimpl(); dao.insert(student); } @override public void delete(int sid) throws sqlexception { // todo auto-generated method stub studentdao dao = new studentdaoimpl(); dao.delete(sid); } @override public student findstudentbyid(int sid) throws sqlexception { // todo auto-generated method stub studentdao dao = new studentdaoimpl(); return dao.findstudentbyid(sid); } @override public void update(student student) throws sqlexception { // todo auto-generated method stub studentdao dao = new studentdaoimpl(); dao.update(student); } @override public list<student> searchstudent(string sname, string sgender) throws sqlexception { // todo auto-generated method stub studentdao dao = new studentdaoimpl(); return dao.searchstudent(sname, sgender); } @override public pagebean findstudentbypage(int currentpage) throws sqlexception { // 封装分页的该页数据 pagebean<student> pagebean = new pagebean<student>(); int pagesize = studentdao.page_size; pagebean.setcurrentpage(currentpage); // 设置当前页 pagebean.setpagesize(pagesize); // 设置每页显示多少记录 studentdao dao = new studentdaoimpl(); list<student> list = dao.findstudentbypage(currentpage); pagebean.setlist(list); // 设置这一页的学生数据 // 总的记录数, 总的页数。 int count = dao.findcount(); pagebean.settotalsize(count); // 设置总的记录数 pagebean.settotalpage(count % pagesize == 0 ? count / pagesize : (count / pagesize) + 1); // 总页数 return pagebean; } }
结言
好了,欢迎在留言区留言,与大家分享你的经验和心得。
感谢你学习今天的内容,如果你觉得这篇文章对你有帮助的话,也欢迎把它分享给更多的朋友,感谢。
达叔小生:往后余生,唯独有你
you and me, we are family !
90后帅气小伙,良好的开发习惯;独立思考的能力;主动并且善于沟通
简书博客: 达叔小生
结语
- 下面我将继续对 其他知识 深入讲解 ,有兴趣可以继续关注
- 小礼物走一走 or 点赞
上一篇: Mysql普通索引与唯一索引的选择详析