简陋的学生管理系统
1.数据库
create database student; use student; create table student( id int primary key auto_increment, name varchar(15), gender varchar(2), phone varchar(14), birthday Date, hobby varchar(30), info varchar(200) ); insert into student values (null,'Jack','男','17709083478','2001-1-2','足球,篮球','虽然我是英文名但我是一个中国人');
2.辅助工具类
1 package com.yxfyg.util; 2 3 import java.sql.Connection; 4 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 import com.mchange.v2.c3p0.ComboPooledDataSource; 9 10 public class JDBCUtil { 11 12 private static ComboPooledDataSource dataSource = null; 13 14 static { 15 dataSource = new ComboPooledDataSource(); 16 } 17 18 public static Connection getConnection() { 19 Connection conn = null; 20 try { 21 conn = dataSource.getConnection(); 22 } catch (SQLException e) { 23 e.printStackTrace(); 24 } 25 return conn; 26 } 27 28 public static ComboPooledDataSource getDataSource() { 29 return dataSource; 30 } 31 32 public static void release(ResultSet rs,Statement st,Connection conn) { 33 closeRs(rs); 34 closeSt(st); 35 closeConn(conn); 36 } 37 38 public static void release(Statement st,Connection conn) { 39 closeSt(st); 40 closeConn(conn); 41 } 42 43 private static void closeRs(ResultSet rs) { 44 try { 45 if(rs != null) { 46 rs.close(); 47 } 48 }catch(SQLException e) { 49 e.printStackTrace(); 50 }finally { 51 rs = null; 52 } 53 } 54 55 private static void closeSt(Statement st) { 56 try { 57 if(st != null) { 58 st.close(); 59 } 60 }catch(SQLException e) { 61 e.printStackTrace(); 62 }finally { 63 st = null; 64 } 65 } 66 67 private static void closeConn(Connection conn) { 68 try { 69 if(conn != null) { 70 conn.close(); 71 } 72 }catch(SQLException e) { 73 e.printStackTrace(); 74 }finally { 75 conn = null; 76 } 77 } 78 }
1 package com.yxfyg.util; 2 3 public class TextUtil { 4 5 public static boolean isEmpty(String str) { 6 return str == null || str.length() == 0; 7 } 8 }
1 package com.yxfyg.bean; 2 3 import java.util.Date; 4 5 6 /** 7 * 学生类 8 */ 9 public class Student { 10 11 private int id; 12 private String name; 13 private String gender; 14 private String phone; 15 private Date birthday; 16 private String hobby; 17 private String info; 18 19 //保留无参构造方法的原因:DBUtils中的对象BeanHandler<T>或者BeanListHandler<T>需要使用 20 public Student() {} 21 22 public Student(String name, String gender, String phone, Date birthday, String hobby, String info) { 23 super(); 24 this.name = name; 25 this.gender = gender; 26 this.phone = phone; 27 this.birthday = birthday; 28 this.hobby = hobby; 29 this.info = info; 30 } 31 32 public Student(int id, String name, String gender, String phone, Date birthday, String hobby, String info) { 33 super(); 34 this.id = id; 35 this.name = name; 36 this.gender = gender; 37 this.phone = phone; 38 this.birthday = birthday; 39 this.hobby = hobby; 40 this.info = info; 41 } 42 43 public int getId() { 44 return id; 45 } 46 public void setId(int id) { 47 this.id = id; 48 } 49 public String getName() { 50 return name; 51 } 52 public void setName(String name) { 53 this.name = name; 54 } 55 public String getGender() { 56 return gender; 57 } 58 public void setGender(String gender) { 59 this.gender = gender; 60 } 61 public String getPhone() { 62 return phone; 63 } 64 public void setPhone(String phone) { 65 this.phone = phone; 66 } 67 public Date getBirthday() { 68 return birthday; 69 } 70 public void setBirthday(Date birthday) { 71 this.birthday = birthday; 72 } 73 public String getHobby() { 74 return hobby; 75 } 76 public void setHobby(String hobby) { 77 this.hobby = hobby; 78 } 79 public String getInfo() { 80 return info; 81 } 82 public void setInfo(String info) { 83 this.info = info; 84 } 85 86 }
1 package com.yxfyg.bean; 2 3 import java.util.List; 4 5 /** 6 * 对分页的页面上所要显示的数据进行封装 7 */ 8 public class Page<T> { 9 10 private int pageSize; 11 private int currentPage; 12 private int totalPage; 13 private int totalItems; 14 private List<T> list; 15 16 public Page(int pageSize, int currentPage, int totalPage, int totalItems, List<T> list) { 17 super(); 18 this.pageSize = pageSize; 19 this.currentPage = currentPage; 20 this.totalPage = totalPage; 21 this.totalItems = totalItems; 22 this.list = list; 23 } 24 25 public int getPageSize() { 26 return pageSize; 27 } 28 public void setPageSize(int pageSize) { 29 this.pageSize = pageSize; 30 } 31 public int getCurrentPage() { 32 return currentPage; 33 } 34 public void setCurrentPage(int currentPage) { 35 this.currentPage = currentPage; 36 } 37 public int getTotalPage() { 38 return totalPage; 39 } 40 public void setTotalPage(int totalPage) { 41 this.totalPage = totalPage; 42 } 43 public int getTotalItems() { 44 return totalItems; 45 } 46 public void setTotalItems(int totalItems) { 47 this.totalItems = totalItems; 48 } 49 public List<T> getList() { 50 return list; 51 } 52 public void setList(List<T> list) { 53 this.list = list; 54 } 55 }
3.View层(JSP)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>首页</title> 6 </head> 7 <body> 8 <h3><a href = "StuListServlet">学生总列表</a></h3><br/> 9 <h3><a href = "StuListPageServlet?currentPage=1">学生分页列表</a></h3> 10 </body> 11 </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <script type="text/javascript"> 8 function doDelete(id){ 9 var flag = confirm("确认删除?"); 10 if(flag){ 11 window.location.href = "DelStuServlet?id=" + id; 12 } 13 } 14 </script> 15 <meta charset="UTF-8"> 16 <title>学生列表</title> 17 </head> 18 <body> 19 <h2>学生列表</h2> 20 <form action="SearchStuServlet" method="post"> 21 <table border="1px" width="900px"> 22 <tr> 23 <td colspan="8">按姓名搜索: <input type="text" name="name" /> 24 选择性别: <select name="gender"> 25 <option value="">--请选择-- 26 <option value="男">男 27 <option value="女">女 28 </select> <input type="submit" value="搜索"> 29 <a href="addStu.html">添加</a> 30 </td> 31 </tr> 32 <tr align="center"> 33 <td>编号</td> 34 <td>姓名</td> 35 <td>性别</td> 36 <td>手机号</td> 37 <td>生日</td> 38 <td>爱好</td> 39 <td>介绍</td> 40 <td>操作</td> 41 </tr> 42 <c:forEach items="${ sessionScope.stuList }" var="stu"> 43 <tr align="center"> 44 <td>${ stu.id }</td> 45 <td>${ stu.name }</td> 46 <td>${ stu.gender }</td> 47 <td>${ stu.phone }</td> 48 <td>${ stu.birthday }</td> 49 <td>${ stu.hobby }</td> 50 <td>${ stu.info }</td> 51 <td><a href="EditStuServlet?id=${ stu.id }">更新</a> <a href="#" 52 onclick="doDelete(${ stu.id })">删除</a></td> 53 </tr> 54 </c:forEach> 55 </table> 56 </form> 57 </body> 58 </html>
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>添加学生</title> 6 </head> 7 <body> 8 <h3>添加学生</h3> 9 <form action="AddStuServlet" method="post"> 10 <table border="1px" width="600px"> 11 <tr> 12 <td>姓名</td> 13 <td><input type="text" name="name"/></td> 14 </tr> 15 <tr> 16 <td>性别</td> 17 <td> 18 <input type="radio" name="gender" value="男" checked="checked"/>男 19 <input type="radio" name="gender" value="女"/>女 20 </td> 21 </tr> 22 <tr> 23 <td>电话</td> 24 <td><input type="text" name="phone"/></td> 25 </tr> 26 <tr> 27 <td>生日</td> 28 <td><input type="text" name="birthday"/></td> 29 </tr> 30 <tr> 31 <td>爱好</td> 32 <td> 33 <input type="checkbox" name="hobby" value="足球"/>足球 34 <input type="checkbox" name="hobby" value="篮球"/>篮球 35 <input type="checkbox" name="hobby" value="乒乓球"/>乒乓球 36 <input type="checkbox" name="hobby" value="羽毛球"/>羽毛球 37 <input type="checkbox" name="hobby" value="排球"/>排球 38 </td> 39 </tr> 40 <tr> 41 <td>简介</td> 42 <td> 43 <textarea rows="3" cols="16" name="info"></textarea> 44 </td> 45 </tr> 46 <tr> 47 <td colspan="2"><input type="submit" value="添加"/></td> 48 </tr> 49 </table> 50 </form> 51 </body> 52 </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 4 <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 5 <!DOCTYPE html> 6 <html> 7 <head> 8 <meta charset="UTF-8"> 9 <title>更新学生</title> 10 </head> 11 <body> 12 <h3>更新学生</h3> 13 <form action="UpdateStuServlet" method="post"> 14 <input type="hidden" name="id" value="${ sessionScope.stu.id }"/> 15 <table border="1px" width="600px"> 16 <tr> 17 <td>姓名</td> 18 <td><input type="text" name="name" value="${ sessionScope.stu.name }"/></td> 19 </tr> 20 <tr> 21 <td>性别</td> 22 <td> 23 <input type="radio" name="gender" value="男" <c:if test="${ sessionScope.stu.gender == '男' }">checked</c:if> />男 24 <input type="radio" name="gender" value="女" <c:if test="${ sessionScope.stu.gender == '女' }">checked</c:if>/>女 25 </td> 26 </tr> 27 <tr> 28 <td>电话</td> 29 <td><input type="text" name="phone" value="${ sessionScope.stu.phone }"/></td> 30 </tr> 31 <tr> 32 <td>生日</td> 33 <td><input type="text" name="birthday" value="${ sessionScope.stu.birthday }"/></td> 34 </tr> 35 <tr> 36 <td>爱好</td> 37 <td> 38 <input type="checkbox" name="hobby" value="足球" <c:if test="${ fn:contains(sessionScope.stu.hobby,'足球') }">checked</c:if>/>足球 39 <input type="checkbox" name="hobby" value="篮球" <c:if test="${ fn:contains(sessionScope.stu.hobby,'篮球') }">checked</c:if>/>篮球 40 <input type="checkbox" name="hobby" value="乒乓球" <c:if test="${ fn:contains(sessionScope.stu.hobby,'乒乓球') }">checked</c:if>/>乒乓球 41 <input type="checkbox" name="hobby" value="羽毛球" <c:if test="${ fn:contains(sessionScope.stu.hobby,'羽毛球') }">checked</c:if>/>羽毛球 42 <input type="checkbox" name="hobby" value="排球" <c:if test="${ fn:contains(sessionScope.stu.hobby,'排球') }">checked</c:if>/>排球 43 </td> 44 </tr> 45 <tr> 46 <td>简介</td> 47 <td> 48 <textarea rows="3" cols="16" name="info" >${ sessionScope.stu.info }</textarea> 49 </td> 50 </tr> 51 <tr> 52 <td colspan="2"><input type="submit" value="更新"/></td> 53 </tr> 54 </table> 55 </form> 56 </body> 57 </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <script type="text/javascript"> 8 function doDelete(id){ 9 var flag = confirm("确认删除?"); 10 if(flag){ 11 window.location.href = "DelStuServlet?id=" + id; 12 } 13 } 14 </script> 15 <meta charset="UTF-8"> 16 <title>学生列表</title> 17 </head> 18 <body> 19 <h2>学生列表</h2> 20 <form action="SearchStuServlet" method="post"> 21 <table border="1px" width="900px"> 22 <tr> 23 <td colspan="8">按姓名搜索: <input type="text" name="name" /> 24 选择性别: <select name="gender"> 25 <option value="">--请选择-- 26 <option value="男">男 27 <option value="女">女 28 </select> <input type="submit" value="搜索"> 29 <a href="addStu.html">添加</a> 30 </td> 31 </tr> 32 <tr align="center"> 33 <td>编号</td> 34 <td>姓名</td> 35 <td>性别</td> 36 <td>手机号</td> 37 <td>生日</td> 38 <td>爱好</td> 39 <td>介绍</td> 40 <td>操作</td> 41 </tr> 42 <c:forEach items="${ sessionScope.page.list }" var="stu"> 43 <tr align="center"> 44 <td>${ stu.id }</td> 45 <td>${ stu.name }</td> 46 <td>${ stu.gender }</td> 47 <td>${ stu.phone }</td> 48 <td>${ stu.birthday }</td> 49 <td>${ stu.hobby }</td> 50 <td>${ stu.info }</td> 51 <td><a href="EditStuServlet?id=${ stu.id }">更新</a> <a href="#" 52 onclick="doDelete(${ stu.id })">删除</a></td> 53 </tr> 54 </c:forEach> 55 <tr> 56 <td colspan="8"> 57 第${ sessionScope.page.currentPage } / ${ sessionScope.page.totalPage } 58 59 60 61 每页显示${ sessionScope.page.pageSize }条 62 63 64 65 总记录数${ sessionScope.page.totalItems } 66 67 68 69 <c:if test="${ sessionScope.page.currentPage != 1 }"> 70 <a href = "StuListPageServlet?currentPage=1">[首页]</a> 71 | 72 <a href = "StuListPageServlet?currentPage=${ sessionScope.page.currentPage - 1 }">上一页</a> 73 </c:if> 74 75 76 77 <c:forEach begin="1" end="${ sessionScope.page.totalPage }" var="i"> 78 <c:if test="${ sessionScope.page.currentPage == i }"> 79 ${ i } 80 </c:if> 81 <c:if test="${ sessionScope.page.currentPage != i }"> 82 <a href = "StuListPageServlet?currentPage=${ i }" >${ i }</a> 83 </c:if> 84 </c:forEach> 85 86 <c:if test="${ sessionScope.page.currentPage != sessionScope.page.totalPage }"> 87 <a href = "StuListPageServlet?currentPage=${ sessionScope.page.currentPage + 1 }">下一页</a> 88 | 89 <a href = "StuListPageServlet?currentPage=${ sessionScope.page.totalPage }">[尾页]</a> 90 </c:if> 91 92 93 </td> 94 </tr> 95 </table> 96 </form> 97 </body> 98 </html>
4.Controller层(Servlet)
1 package com.yxfyg.servlet; 2 3 import java.io.IOException; 4 import java.sql.SQLException; 5 import java.util.List; 6 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 import javax.servlet.http.HttpSession; 12 13 import com.yxfyg.bean.Student; 14 import com.yxfyg.service.impl.StudentServiceImpl; 15 16 public class StuListServlet extends HttpServlet{ 17 18 @Override 19 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 20 21 try { 22 List<Student> list = new StudentServiceImpl().findAll(); 23 HttpSession session = req.getSession(); 24 session.setAttribute("stuList", list); 25 resp.sendRedirect("stuList.jsp"); 26 } catch (SQLException e) { 27 e.printStackTrace(); 28 } 29 } 30 31 @Override 32 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 33 doGet(req, resp); 34 } 35 }
1 package com.yxfyg.servlet; 2 3 import java.io.IOException; 4 import java.text.ParseException; 5 import java.text.SimpleDateFormat; 6 import java.util.Arrays; 7 import java.util.Date; 8 9 import javax.servlet.ServletException; 10 import javax.servlet.http.HttpServlet; 11 import javax.servlet.http.HttpServletRequest; 12 import javax.servlet.http.HttpServletResponse; 13 14 import com.yxfyg.bean.Student; 15 import com.yxfyg.service.StudentService; 16 import com.yxfyg.service.impl.StudentServiceImpl; 17 18 public class AddStuServlet extends HttpServlet{ 19 20 @Override 21 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 22 23 try { 24 req.setCharacterEncoding("UTF-8"); 25 String name = req.getParameter("name"); 26 String gender = req.getParameter("gender"); 27 String phone = req.getParameter("phone"); 28 String birth = req.getParameter("birthday"); 29 String[] hobbyArray = req.getParameterValues("hobby"); 30 String info = req.getParameter("info"); 31 32 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 33 Date birthday = format.parse(birth); 34 35 String hobby = Arrays.toString(hobbyArray); 36 hobby = hobby.substring(1,hobby.length()-1); 37 38 Student student = new Student(name,gender,phone,birthday,hobby,info); 39 40 StudentService service = new StudentServiceImpl(); 41 service.insert(student); 42 43 resp.sendRedirect("StuListServlet"); 44 } catch (Exception e) { 45 e.printStackTrace(); 46 } 47 } 48 49 @Override 50 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 51 doGet(req, resp); 52 } 53 }
1 package com.yxfyg.servlet; 2 3 import java.io.IOException; 4 import java.sql.SQLException; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 import com.yxfyg.service.StudentService; 12 import com.yxfyg.service.impl.StudentServiceImpl; 13 14 public class DelStuServlet extends HttpServlet{ 15 16 @Override 17 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 18 try { 19 int id = Integer.parseInt(req.getParameter("id")); 20 StudentService service = new StudentServiceImpl(); 21 service.delete(id); 22 resp.sendRedirect("StuListServlet"); 23 } catch (NumberFormatException e) { 24 e.printStackTrace(); 25 } catch (SQLException e) { 26 e.printStackTrace(); 27 } 28 } 29 30 @Override 31 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 32 doGet(req, resp); 33 } 34 35 }
1 package com.yxfyg.servlet; 2 3 import java.io.IOException; 4 import java.sql.SQLException; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 import javax.servlet.http.HttpSession; 11 12 import com.yxfyg.bean.Student; 13 import com.yxfyg.service.StudentService; 14 import com.yxfyg.service.impl.StudentServiceImpl; 15 16 public class EditStuServlet extends HttpServlet{ 17 18 @Override 19 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 20 try { 21 int id = Integer.parseInt(req.getParameter("id")); 22 StudentService service = new StudentServiceImpl(); 23 Student stu = service.findStuById(id); 24 HttpSession session = req.getSession(); 25 session.setAttribute("stu", stu); 26 resp.sendRedirect("editStu.jsp"); 27 } catch (NumberFormatException e) { 28 e.printStackTrace(); 29 } catch (SQLException e) { 30 e.printStackTrace(); 31 } 32 } 33 34 @Override 35 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 36 doGet(req, resp); 37 } 38 }
1 package com.yxfyg.servlet; 2 3 import java.io.IOException; 4 import java.text.SimpleDateFormat; 5 import java.util.Arrays; 6 import java.util.Date; 7 8 import javax.servlet.ServletException; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 13 import com.yxfyg.bean.Student; 14 import com.yxfyg.service.StudentService; 15 import com.yxfyg.service.impl.StudentServiceImpl; 16 17 public class UpdateStuServlet extends HttpServlet{ 18 19 @Override 20 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 21 try { 22 req.setCharacterEncoding("UTF-8"); 23 int id = Integer.parseInt(req.getParameter("id")); 24 String name = req.getParameter("name"); 25 String gender = req.getParameter("gender"); 26 String phone = req.getParameter("phone"); 27 String birth = req.getParameter("birthday"); 28 String[] hobbyArray = req.getParameterValues("hobby"); 29 String info = req.getParameter("info"); 30 31 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 32 Date birthday = format.parse(birth); 33 34 String hobby = Arrays.toString(hobbyArray); 35 hobby = hobby.substring(1,hobby.length()-1); 36 37 Student student = new Student(id,name,gender,phone,birthday,hobby,info); 38 39 StudentService service = new StudentServiceImpl(); 40 service.update(student); 41 42 resp.sendRedirect("StuListServlet"); 43 } catch (Exception e) { 44 e.printStackTrace(); 45 } 46 } 47 48 @Override 49 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 50 doGet(req, resp); 51 } 52 }
1 package com.yxfyg.servlet; 2 3 import java.io.IOException; 4 import java.sql.SQLException; 5 import java.util.List; 6 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 import javax.servlet.http.HttpSession; 12 13 import com.yxfyg.bean.Student; 14 import com.yxfyg.service.StudentService; 15 import com.yxfyg.service.impl.StudentServiceImpl; 16 17 public class SearchStuServlet extends HttpServlet{ 18 19 @Override 20 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 21 try { 22 req.setCharacterEncoding("UTF-8"); 23 String name = req.getParameter("name"); 24 String gender = req.getParameter("gender"); 25 26 StudentService service = new StudentServiceImpl(); 27 List<Student> stuList = service.search(name,gender); 28 HttpSession session = req.getSession(); 29 session.setAttribute("stuList", stuList); 30 resp.sendRedirect("stuList.jsp"); 31 } catch (SQLException e) { 32 e.printStackTrace(); 33 } 34 } 35 36 @Override 37 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 38 doGet(req, resp); 39 } 40 41 }
1 package com.yxfyg.servlet; 2 3 import java.io.IOException; 4 import java.sql.SQLException; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 import javax.servlet.http.HttpSession; 11 12 import com.yxfyg.bean.Page; 13 import com.yxfyg.bean.Student; 14 import com.yxfyg.service.StudentService; 15 import com.yxfyg.service.impl.StudentServiceImpl; 16 17 public class StuListPageServlet extends HttpServlet{ 18 19 @Override 20 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 21 try { 22 int currentPage = Integer.parseInt(req.getParameter("currentPage")); 23 StudentService service = new StudentServiceImpl(); 24 Page<Student> page = service.findPage(currentPage); 25 HttpSession session = req.getSession(); 26 session.setAttribute("page", page); 27 resp.sendRedirect("stuListPage.jsp"); 28 } catch (NumberFormatException e) { 29 e.printStackTrace(); 30 } catch (SQLException e) { 31 e.printStackTrace(); 32 } 33 34 35 } 36 37 @Override 38 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 39 doPost(req, resp); 40 } 41 }
5.Model层(业务逻辑层+Dao层)
1 package com.yxfyg.service; 2 3 import java.sql.SQLException; 4 import java.util.List; 5 6 import com.yxfyg.bean.Page; 7 import com.yxfyg.bean.Student; 8 9 /** 10 * 业务逻辑层 11 */ 12 public interface StudentService { 13 14 List<Student> findAll() throws SQLException; 15 16 boolean insert(Student student) throws SQLException; 17 18 boolean delete(int id) throws SQLException; 19 20 Student findStuById(int id) throws SQLException; 21 22 boolean update(Student student) throws SQLException; 23 24 List<Student> search(String name , String gender) throws SQLException; 25 26 Page<Student> findPage(int currentPage) throws SQLException; 27 }
1 package com.yxfyg.service.impl; 2 3 import java.sql.SQLException; 4 import java.util.List; 5 6 import com.yxfyg.bean.Page; 7 import com.yxfyg.bean.Student; 8 import com.yxfyg.dao.StudentDao; 9 import com.yxfyg.dao.impl.StudentDaoImpl; 10 import com.yxfyg.service.StudentService; 11 12 public class StudentServiceImpl implements StudentService{ 13 14 private StudentDao dao = new StudentDaoImpl(); 15 16 @Override 17 public List<Student> findAll() throws SQLException { 18 return dao.findAll(); 19 } 20 21 @Override 22 public boolean insert(Student student) throws SQLException { 23 return dao.insert(student); 24 } 25 26 public boolean delete(int id) throws SQLException { 27 return dao.delete(id); 28 } 29 30 @Override 31 public Student findStuById(int id) throws SQLException { 32 return dao.findStuById(id); 33 } 34 35 @Override 36 public boolean update(Student student) throws SQLException { 37 return dao.update(student); 38 } 39 40 @Override 41 public List<Student> search(String name, String gender) throws SQLException { 42 return dao.search(name, gender); 43 } 44 45 @Override 46 public Page<Student> findPage(int currentPage) throws SQLException { 47 int pageSize = StudentDao.PAGE_SIZE; 48 int totalItems = dao.getTotalItems(); 49 int totalPage = totalItems % pageSize == 0 ? totalItems / pageSize : totalItems / pageSize + 1; 50 List<Student> stuList = dao.findStuByPage(currentPage); 51 Page<Student> page = new Page<Student>(pageSize, currentPage, totalPage, totalItems, stuList); 52 return page; 53 } 54 }
1 package com.yxfyg.dao; 2 3 import java.sql.SQLException; 4 import java.util.List; 5 6 import com.yxfyg.bean.Student; 7 8 9 /** 10 * DAO层 11 */ 12 public interface StudentDao { 13 14 int PAGE_SIZE = 3; 15 16 List<Student> findAll() throws SQLException; 17 18 boolean insert(Student student) throws SQLException; 19 20 boolean delete(int id) throws SQLException; 21 22 Student findStuById(int id) throws SQLException; 23 24 boolean update(Student student) throws SQLException; 25 26 List<Student> search(String name , String gender) throws SQLException; 27 28 List<Student> findStuByPage(int currentPage) throws SQLException; 29 30 int getTotalItems() throws SQLException; 31 32 }
1 package com.yxfyg.dao.impl; 2 3 import java.sql.SQLException; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import javax.sql.DataSource; 8 9 import org.apache.commons.dbutils.QueryRunner; 10 import org.apache.commons.dbutils.handlers.BeanHandler; 11 import org.apache.commons.dbutils.handlers.BeanListHandler; 12 import org.apache.commons.dbutils.handlers.ScalarHandler; 13 14 import com.yxfyg.bean.Student; 15 import com.yxfyg.dao.StudentDao; 16 import com.yxfyg.util.JDBCUtil; 17 import com.yxfyg.util.TextUtil; 18 19 import javafx.beans.binding.ListBinding; 20 21 public class StudentDaoImpl implements StudentDao{ 22 23 private DataSource dataSource = JDBCUtil.getDataSource(); 24 QueryRunner runner = new QueryRunner(dataSource); 25 26 @Override 27 public List<Student> findAll() throws SQLException { 28 List<Student> list = new ArrayList<Student>(); 29 list = runner.query("select * from student", new BeanListHandler<Student>(Student.class)); 30 return list; 31 } 32 33 @Override 34 public boolean insert(Student student) throws SQLException { 35 int count = runner.update( 36 "insert into student values(null,?,?,?,?,?,?)", 37 student.getName(),student.getGender(),student.getPhone(), 38 student.getBirthday(),student.getHobby(),student.getInfo()); 39 if(count != 0) { 40 return true; 41 } 42 return false; 43 } 44 45 @Override 46 public boolean delete(int id) throws SQLException { 47 int count = runner.update("delete from student where id = ?",id); 48 if(count != 0) { 49 return true; 50 } 51 return false; 52 } 53 54 @Override 55 public Student findStuById(int id) throws SQLException { 56 Student student = runner.query("select * from student where id = ?", new BeanHandler<Student>(Student.class) , id); 57 return student; 58 } 59 60 @Override 61 public boolean update(Student student) throws SQLException { 62 int count = runner.update( 63 "update student set name = ? , gender = ? , phone = ? ," 64 + "birthday = ? , hobby = ? , info = ? where id = ?", 65 student.getName(),student.getGender(),student.getPhone(), 66 student.getBirthday(),student.getHobby(),student.getInfo(),student.getId()); 67 if(count != 0) { 68 return true; 69 } 70 return false; 71 } 72 73 @Override 74 public List<Student> search(String name, String gender) throws SQLException { 75 String sql = "select * from student where 1 = 1 "; 76 List<String> list = new ArrayList<String>(); 77 78 if(!TextUtil.isEmpty(name)) { 79 sql = sql + "and name like ?"; 80 list.add("%" + name + "%"); 81 } 82 83 if(!TextUtil.isEmpty(gender)) { 84 sql = sql + "and gender = ?"; 85 list.add(gender); 86 } 87 //参数不仅可以是可变参数,也可以是数组 88 List<Student> stuList = runner.query(sql, new BeanListHandler<Student>(Student.class), list.toArray()); 89 return stuList; 90 } 91 92 @Override 93 public List<Student> findStuByPage(int currentPage) throws SQLException { 94 List<Student> list = new ArrayList<Student>(); 95 //offset,偏移 例:limit 5 offset 2 从第三条开始取,取连续的5条数据 96 list = runner.query("select * from student limit ? offset ?", 97 new BeanListHandler<Student>(Student.class), 98 StudentDao.PAGE_SIZE, 99 (currentPage - 1) * StudentDao.PAGE_SIZE); 100 return list; 101 } 102 103 @Override 104 public int getTotalItems() throws SQLException { 105 //ScalarHandler()常用于取单一的数,返回Object,实际类型为Long类型 106 Long count = (Long)runner.query("select count(*) from student", new ScalarHandler()); 107 int totalItems = count.intValue(); 108 return totalItems; 109 } 110 111 }
浙公网安备 33010602011771号