dao层接口:
public interface IStudentDao { //根据学号修改学生 boolean updateStudentByStuNo(int stuno ,Student student) ; //根据学号删除学生 public boolean deleteStudentByStuNo(int stuno) ; //增加学生 public int addStudent(Student student) ; //查询此人是否存在 public boolean isExist(int stuno) ; //查询所有 public List<Student> queryAllStudent(); //根据学号查询学生 返回一个学生 public List<Student> queryStudentByStuNo(int stuno) ; //分页查询中的 查询总数据数 public int getTotalCount(); //分页查询中的 查询某一页的对象集合(当前页的数据) public List<Student> queryStudentByPage(int currentPage,int pageSize) ; }dao层实现类:
int count = DBUtil.executeUpdate(sql, params) ; if(count>0) { return true ; }else { return false ; } }catch (Exception e) { e.printStackTrace(); return false; } } //增加学生 public int addStudent(Student student) { String sql = "insert into student(stuno,stuname,stuage) values(?,?,?)" ; Object[] params = {student.getStuNo(),student.getStuName(),student.getStuAge()}; int count = DBUtil.executeUpdate(sql, params); if(count > 0) { return 1; }else { return 0; } } //查询此人是否存在 public boolean isExist(int stuno) { return queryStudentByStuNo(stuno) == null?false:true ; } //查询所有 public List<Student> queryAllStudent(){ PreparedStatement ps = null ; ResultSet rs = null; List<Student> students = new ArrayList<>() ; Student student = null ; try { String sql = "select * from student order by stuno desc" ; rs = DBUtil.executeQuery(sql, null); while(rs.next()) { int no = rs.getInt("stuno"); String name = rs.getString("stuname"); int age = rs.getInt("stuage"); student = new Student(no,name,age); students.add(student) ; } return students; } catch (SQLException e) { e.printStackTrace(); return null ; }catch (Exception e) { e.printStackTrace(); return null ; }finally { DBUtil.closeAll(rs, ps, DBUtil.connection); } } //根据学号查询学生 返回一个学生 public List<Student> queryStudentByStuNo(int stuno) { PreparedStatement ps = null; ResultSet rs = null ; List<Student> students = new ArrayList<>() ; Student student = null ; try { String sql = "select * from student where stuno = ?" ; Object[] params = {stuno} ; rs = DBUtil.executeQuery(sql, params); if(rs.next()) { int no = rs.getInt("stuNo"); String name = rs.getString("stuName"); int age = rs.getInt("stuAge"); student =new Student(no,name,age); students.add(student) ; } return students ; } catch (SQLException e) { e.printStackTrace(); return null ; }catch (Exception e) { e.printStackTrace(); return null ; }finally { DBUtil.closeAll(rs, ps, DBUtil.connection); } } //分页查询中的 查询总数据数 @Override public int getTotalCount() { String sql = "select count(1) from student" ; return DBUtil.getTotalCount(sql) ; } /* * 分页查询中的 查询当前页的对象结合 * currenPage : 当前页 * pageSize : 页面大小 (每页多少条数据) */ @Override public List<Student> queryStudentByPage(int currentPage, int pageSize) { //分页sql语句 String sql = "select s.* from \r\n" + " ( select rownum r,t.* from ( select * from student order by stuno asc ) t \r\n" + " where rownum<= ? " + " ) s where r>= ? \r\n" + " " ; //currentPage*pageSize:某一页的最后一条数据 //(currentPage-1)*pageSize+1 : 某一页的第一条数据 Object[] params = {currentPage*pageSize,(currentPage-1)*pageSize+1}; ResultSet rs = DBUtil.executeQuery(sql, params) ; List<Student> students = new ArrayList<>(); try { while (rs.next()) { int no = rs.getInt("stuno"); String name = rs.getString("stuname"); int age = rs.getInt("stuage"); Student student = new Student(no,name,age); students.add(student) ; } } catch (SQLException e) { e.printStackTrace(); } return students; } }Service接口:
public interface IStudentService { //增加学生 public int addStudent(Student student) ; //根据学号删除学生 public boolean deleteStudentByStuNo(int stuno) ; //根据学号修改学生 public boolean updateStudentByStuNo(int stuno , Student student) ; //根据学号查询学生 public List<Student> queryStudentByStuNo(int stuno); //查询全部学生 public List<Student> queryAllStudent(); //分页查询 中的查询总数居 public int getTotalCount(); //分页查询中的 查询当前页对象集合 (当前页的数据) public List<Student> queryStudentByPage(int currentPage,int pageSize); }Service实现类:
public class StudentServiceImpl implements IStudentService{ //调用dao层 IStudentDao studao = new StudentDaoImpl(); //增加学生 public int addStudent(Student student) { //增加之前判断是否已经存在 // 没有查找 studao.isExist( student.getStuNo() ) == true ; if( studao.isExist(student.getStuNo())) { return studao.addStudent(student); }else { System.out.println("此人已存在"); return 0 ; } } //根据学号删除学生 public boolean deleteStudentByStuNo(int stuno) { //判断是否存在 if(studao.isExist(stuno)) { return studao.deleteStudentByStuNo(stuno) ; }else { System.out.println("该学生不存在"); return false ; } } //根据学号修改学生 public boolean updateStudentByStuNo(int stuno , Student student) { //判断是否存在 if(studao.isExist(stuno)) { return studao.updateStudentByStuNo(stuno, student) ; }else { System.out.println("该学生不存在"); return false ; } } //根据学号查询学生 public List<Student> queryStudentByStuNo(int stuno) { return studao.queryStudentByStuNo(stuno) ; } //查询全部学生 public List<Student> queryAllStudent(){ return studao.queryAllStudent() ; } @Override public int getTotalCount() { return studao.getTotalCount(); } @Override public List<Student> queryStudentByPage(int currentPage, int pageSize) { return studao.queryStudentByPage(currentPage, pageSize); } }addControler:
public class addStudentServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //从前端获取数据 request.setCharacterEncoding("utf-8"); int no = Integer.parseInt( request.getParameter("stuNo") ); String name = request.getParameter("stuName"); int age = Integer.parseInt( request.getParameter("stuAge") ); Student student = new Student(no,name,age); IStudentService studentservice = new StudentServiceImpl(); int addStudent = studentservice.addStudent(student); response.setCharacterEncoding("utf-8"); response.setContentType("text/html; charset=UTF-8"); // PrintWriter out = response.getWriter(); if(addStudent>0) {//如果增加失败,给request放一条数据error request.setAttribute("error", "noadderror"); }else {//增加成功 request.setAttribute("error", "adderror"); } request.getRequestDispatcher("queryAllStudentServlet").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }DeleteController:
public class deleteServlet extends HttpServlet { //调用service层 IStudentService studentService = new StudentServiceImpl(); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //处理编码 request.setCharacterEncoding("utf-8"); //从前端获取需要删除的学生的学号 int stuno = Integer.parseInt(request.getParameter("stuno") ); studentService.deleteStudentByStuNo(stuno); response.sendRedirect("queryAllStudentServlet"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } **PagingController:**public class PagingServlet extends HttpServlet {
//调用Service层 IStudentService stuService = new StudentServiceImpl(); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //将分页所需的5个字段组装到pageinfo实体类中 PageInfo pageinfo = new PageInfo(); //查询数据总数 int count = stuService.getTotalCount(); pageinfo.setTotalCount(count);//组装总数据 //从前端获取当前页是第几页 String cPage = request.getParameter("currentPage") ; //当第一次执行该Servlet时,cPage是null 所以需进行判断 if(cPage == null ) { cPage = "1"; } int currentPage = Integer.parseInt(cPage) ; pageinfo.setCurrentPage(currentPage); //从前端获取自定义的 页面大小 String ps = request.getParameter("choice") ; System.out.println("前:"+ps); if(ps == null) { ps = "3";//当第一次执行该Servlet时,ps为空 给其默认值为3 } System.out.println("后:"+ps); int pageSize = Integer.parseInt(ps) ; pageinfo.setPageSize(pageSize); //当前页对象集合(当前页的数据) List<Student> students = stuService.queryStudentByPage(currentPage, pageSize); pageinfo.setStudents(students);//组装当前页对象集合 request.setAttribute("pageinfo", pageinfo);//将封装了5个字段的PageInfo转发到前端 request.getRequestDispatcher("Paging/paging.jsp").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}
QueryAllController://查询全部学生 public class queryAllStudentServlet extends HttpServlet { //调用Service层 IStudentService studentService = new StudentServiceImpl(); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List students = studentService.queryAllStudent(); //将结果集 放入request域中 request.setAttribute(“students”,students); //因为request域中有数据,因此需要通过请求转发的方式跳转(重定向response会丢request中的失数据) request.getRequestDispatcher(“Student/result.jsp”).forward(request, response); }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}
QueryOneByExampleController://查询单个学生 public class queryStudentByStuNo extends HttpServlet { //调用Service层 IStudentService studentService = new StudentServiceImpl(); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取前端需要查询的学生的学号 int stuno = Integer.parseInt( request.getParameter(“stuno”) ); List student = studentService.queryStudentByStuNo(stuno) ; //跳转到学生信心详情页 进而可以执行修改 request.setAttribute(“student”, student); request.getRequestDispatcher(“Student/updateinfo.jsp”).forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }
}
UpdateControllerController:public class updateServlet extends HttpServlet { //调用Service层 IStudentService studentService = new StudentServiceImpl(); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //处理编码 request.setCharacterEncoding(“utf-8”); //获取前端学生信息 int no = Integer.parseInt( request.getParameter(“stuno”) ); String name = request.getParameter(“stuname”) ; int age = Integer.parseInt( request.getParameter(“stuage”) ); Student student = new Student(no,name,age); studentService.updateStudentByStuNo(no, student) ; //修改之后 跳转到查询功能 继而查询出修改之后的内容 // request.getRequestDispatcher(“queryAllStudentServlet”).forward(request, response); request.getRequestDispatcher(“PagingServlet”).forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }
}
**Paging.jsp** <%@page import="java.util.List"%> <%@page import="org.cpl.entity.PageInfo"%> <%@page import="org.cpl.entity.Student"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> **<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>** <html> <head> <meta charset="UTF-8"> <title>分页显示</title> <script type="text/javascript" src="js/jquery-3.1.1.js"></script> <script type="text/javascript"> $(function(){ $("#op").change(function(){ var value = $(this).val(); //var value = $("#op option:selected").val(); $("#choice").val(value); alert(value); }); }); </script> </head> <body> <table border="1px"> <tr> <th>学号</th> <th>姓名</th> <th>年龄</th> <th>操作</th> </tr> <% //获取前后端数据 PageInfo pageinfo = (PageInfo) request.getAttribute("pageinfo") ; //遍历pageinfo for(Student student : pageinfo.getStudents()) { %> <tr> <td><a href="queryStudentByStuNo?stuno=<%=student.getStuNo() %>"><%=student.getStuNo() %></a></td> <td><%=student.getStuName() %></td> <td><%=student.getStuAge() %></td> <td><a href="deleteServlet?stuno=<%=student.getStuNo() %>">删除</a></td> </tr> <% } %> </table> <% if(pageinfo.getCurrentPage()<pageinfo.getTotalPage() && pageinfo.getCurrentPage()>1) { %> <a href="PagingServlet?currentPage=1">首页</a> <a href="PagingServlet?currentPage=<%=pageinfo.getCurrentPage()-1 %>">上一页</a> <a href="PagingServlet?currentPage=<%=pageinfo.getCurrentPage()+1 %>">下一页</a> <a href="PagingServlet?currentPage=<%=pageinfo.getTotalPage() %>">尾页</a> <% } if(pageinfo.getCurrentPage()==1){ %> <a href="PagingServlet?currentPage=<%=pageinfo.getCurrentPage()+1 %>">下一页</a> <a href="PagingServlet?currentPage=<%=pageinfo.getTotalPage() %>">尾页</a> <% } if(pageinfo.getCurrentPage()==pageinfo.getTotalPage()){ %> <a href="PagingServlet?currentPage=1">首页</a> <a href="PagingServlet?currentPage=<%=pageinfo.getCurrentPage()-1 %>">上一页</a> <% } %> <a href="Student/add.jsp">增加学生</a><br> 自定义显示行数:<select id="op" name="choice" > <option value="3">3</option> <option value="5">5</option> <option value="10">10</option> </select> <input type="hidden" name="choice" id="choice"> </body>