在AdminDaoImpl中添加方法findNowPageInfo()
package www.csdn.net.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import www.csdn.net.domain.Admin; import www.csdn.net.util.DBConn; public class AdminDaoImpl implements AdminDao { private Connection conn; private PreparedStatement pstmt; private ResultSet rs; // 每页显示的记录数 public static final int PAGESIZE = 3; public boolean delete(Admin entity) { // TODO Auto-generated method stub return false; } public boolean delete(int id) { // TODO Auto-generated method stub return false; } public List<Admin> findAll() { // 1、声明返回值变量 List<Admin> entities = new ArrayList<Admin>(); // 2、声明sql语句 String sql = "select id,name,pass,sex,role from admin"; // 3、获取连接对象 conn = DBConn.getConn(); try { // 4、根据sql语句获取预处理对象 pstmt = conn.prepareStatement(sql); // 5、为占位符赋值 // 6、执行查询 rs = pstmt.executeQuery(); // 7、判断rs.next(); if,while while (rs.next()) { // 实例化对象 Admin entity = new Admin(); // 为对象赋值 ----实际上就是把admin这个表中字段的值,赋值给Admin这个类对象的属性 entity.setId(rs.getInt("id")); // rs.getInt("id")数据库id字段的值 // 赋值给了Class Admin 的id属性 entity.setName(rs.getString("name")); // rs.getString("name")数据库Admin表 // name字段的值 赋值给了 Class // Admin 的 name属性 entity.setPass(rs.getString("pass")); entity.setSex(rs.getString("sex")); entity.setRole(rs.getInt("role")); entities.add(entity);// 添加到集合中 } // 8、释放资源 DBConn.realse(rs, pstmt); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return entities; } public Admin findById(int id) { // TODO Auto-generated method stub return null; } findNowPageInfo { // 1、声明返回值对象 List<Admin> entities = new ArrayList<Admin>(); // 2、声明sql语句 String sql = " select id,name,pass,sex,role from admin limit ?,? "; // 3、获取连接对象 conn = DBConn.getConn(); try { // 4、根据sql语句获取预处理对象 pstmt = conn.prepareStatement(sql); // 5、为占位符赋值 int index = 1; pstmt.setInt(index++, (nowpage - 1) * PAGESIZE); pstmt.setInt(index++, PAGESIZE); // 6、执行查询 rs = pstmt.executeQuery(); // 7、判断rs.next(); if,while while (rs.next()) { // 实例化对象 Admin entity = new Admin(); // 为对象赋值 ----实际上就是把admin这个表中字段的值,赋值给Admin这个类对象的属性 entity.setId(rs.getInt("id")); // rs.getInt("id")数据库id字段的值 // 赋值给了Class Admin 的id属性 entity.setName(rs.getString("name")); // rs.getString("name")数据库Admin表 // name字段的值 赋值给了 Class // Admin 的 name属性 entity.setPass(rs.getString("pass")); entity.setSex(rs.getString("sex")); entity.setRole(rs.getInt("role")); entities.add(entity);// 添加到集合中 } // 8、释放资源 DBConn.realse(rs, pstmt); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return entities; } public int getCountSize() { // TODO Auto-generated method stub return 0; } public boolean insert(Admin entity) { // TODO Auto-generated method stub return false; } public Admin login(String name, String pass) { // 1、声明返回值变量 Admin entity = null; // 2、声明sql语句 String sql = "select id,name,pass,sex,role from admin where name=? and pass=? "; // 3、获取连接对象 conn = DBConn.getConn(); try { // 4、根据sql语句获取预处理对象 pstmt = conn.prepareStatement(sql); // 5、为占位符赋值 int index = 1; pstmt.setString(index++, name); pstmt.setString(index++, pass); // 6、执行查询 rs = pstmt.executeQuery(); // 7、判断rs.next(); if,while if (rs.next()) { // 实例化对象 entity = new Admin(); // 为对象赋值 ----实际上就是把admin这个表中字段的值,赋值给Admin这个类对象的属性 entity.setId(rs.getInt("id")); // rs.getInt("id")数据库id字段的值 // 赋值给了Class Admin 的id属性 entity.setName(rs.getString("name")); // rs.getString("name")数据库Admin表 // name字段的值 赋值给了 Class // Admin 的 name属性 entity.setPass(rs.getString("pass")); entity.setSex(rs.getString("sex")); entity.setRole(rs.getInt("role")); } // 8、释放资源 DBConn.realse(rs, pstmt); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return entity; } }
测试
package www.csdn.net.junit; import java.util.List; import org.junit.Test; import www.csdn.net.dao.AdminDao; import www.csdn.net.dao.AdminDaoImpl; import www.csdn.net.domain.Admin; public class AdminDaoImolTest { //AdminDao接口,AdminDaoImpl是接口的实现类,所有一个接口实现类的对象可以转换成接口的对象(多态性)。 private AdminDao adminDao = new AdminDaoImpl(); @Test public void login(){ Admin entity = adminDao.login("Jack", "123"); System.out.println(entity.toString()); } @Test public void findAll() { List<Admin> list = adminDao.findAll(); System.out.println("查询内容是:"); for (Admin admin : list) { System.out.println("id:" + admin.getId()); System.out.println("name:" + admin.getName()); System.out.println("pw:" + admin.getPass()); System.out.println("sex:" + admin.getSex()); System.out.println("role:" + admin.getRole()); System.out.println(" "); } } @Test public void findNow(){ List<Admin> i = adminDao.findNowPageInfo(3); System.out.println(i); } }
select.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'select.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <h1>查询所有的管理员记录</h1> <table border="1px" cellpadding="1px" cellspacing="0"> <thead> <th>序号</th> <th>姓名</th> <th>性别</th> <th>密码</th> <th>角色</th> <th>操作</th> </thead> <tbody> <c:forEach var="entity" items="${entities}"> <tr> <td>${entity.id}</td> <td>${entity.name}</td> <td>${entity.sex}</td> <td>${entity.pass}</td> <td>${entity.role}</td> <td> <a href="${pageContext.request.contextPath}/findById.do?id=${entity.id}">查看详情</a>| <a href="${pageContext.request.contextPath}/deleteById.do?id=${entity.id}">删除</a> </td> </tr> </c:forEach> <tr align="center"> <td colspan="6"> <a href="${pageContext.request.contextPath}/select.do?nowpage=1"/>首页</a> <a href="${pageContext.request.contextPath}/select.do?nowpage=${nowpage-1}"/>上一页</a> <a href="${pageContext.request.contextPath}/select.do?nowpage=${nowpage+1}"/>下一页</a> <a href="${pageContext.request.contextPath}/select.do?nowpage=${countpage}"/>末页</a> </td> </tr> </tbody> </table> </body> </html>
AdminDao新建方法getCountPage()
package www.csdn.net.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import www.csdn.net.domain.Admin; import www.csdn.net.util.DBConn; public class AdminDaoImpl implements AdminDao { private Connection conn; private PreparedStatement pstmt; private ResultSet rs; // 每页显示的记录数 public static final int PAGESIZE = 3; public boolean delete(Admin entity) { // TODO Auto-generated method stub return false; } public boolean delete(int id) { // TODO Auto-generated method stub return false; } public List<Admin> findAll() { // 1、声明返回值变量 List<Admin> entities = new ArrayList<Admin>(); // 2、声明sql语句 String sql = "select id,name,pass,sex,role from admin"; // 3、获取连接对象 conn = DBConn.getConn(); try { // 4、根据sql语句获取预处理对象 pstmt = conn.prepareStatement(sql); // 5、为占位符赋值 // 6、执行查询 rs = pstmt.executeQuery(); // 7、判断rs.next(); if,while while (rs.next()) { // 实例化对象 Admin entity = new Admin(); // 为对象赋值 ----实际上就是把admin这个表中字段的值,赋值给Admin这个类对象的属性 entity.setId(rs.getInt("id")); // rs.getInt("id")数据库id字段的值 // 赋值给了Class Admin 的id属性 entity.setName(rs.getString("name")); // rs.getString("name")数据库Admin表 // name字段的值 赋值给了 Class // Admin 的 name属性 entity.setPass(rs.getString("pass")); entity.setSex(rs.getString("sex")); entity.setRole(rs.getInt("role")); entities.add(entity);// 添加到集合中 } // 8、释放资源 DBConn.realse(rs, pstmt); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return entities; } public Admin findById(int id) { // TODO Auto-generated method stub return null; } public List<Admin> findNowPageInfo(int nowpage) { // 1、声明返回值对象 List<Admin> entities = new ArrayList<Admin>(); // 2、声明sql语句 String sql = " select id,name,pass,sex,role from admin limit ?,? "; // 3、获取连接对象 conn = DBConn.getConn(); try { // 4、根据sql语句获取预处理对象 pstmt = conn.prepareStatement(sql); // 5、为占位符赋值 int index = 1; pstmt.setInt(index++, (nowpage - 1) * PAGESIZE); pstmt.setInt(index++, PAGESIZE); // 6、执行查询 rs = pstmt.executeQuery(); // 7、判断rs.next(); if,while while (rs.next()) { // 实例化对象 Admin entity = new Admin(); // 为对象赋值 ----实际上就是把admin这个表中字段的值,赋值给Admin这个类对象的属性 entity.setId(rs.getInt("id")); // rs.getInt("id")数据库id字段的值 // 赋值给了Class Admin 的id属性 entity.setName(rs.getString("name")); // rs.getString("name")数据库Admin表 // name字段的值 赋值给了 Class // Admin 的 name属性 entity.setPass(rs.getString("pass")); entity.setSex(rs.getString("sex")); entity.setRole(rs.getInt("role")); entities.add(entity);// 添加到集合中 } // 8、释放资源 DBConn.realse(rs, pstmt); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return entities; } public int getCountSize() { // 1、声明返回值变量 int countSize = 0; // 2、声明sql语句 String sql = " select count(*) as c from admin "; // 3、获取连接对象 conn = DBConn.getConn(); try { // 4、根据sql语句获取预处理对象 pstmt = conn.prepareStatement(sql); // 6、执行查询 rs = pstmt.executeQuery(); // 7、判断rs.next(); if,while if (rs.next()) { countSize = rs.getInt("c"); } // 8、释放资源 DBConn.realse(rs, pstmt); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return countSize; } public boolean insert(Admin entity) { // TODO Auto-generated method stub return false; } public Admin login(String name, String pass) { // 1、声明返回值变量 Admin entity = null; // 2、声明sql语句 String sql = "select id,name,pass,sex,role from admin where name=? and pass=? "; // 3、获取连接对象 conn = DBConn.getConn(); try { // 4、根据sql语句获取预处理对象 pstmt = conn.prepareStatement(sql); // 5、为占位符赋值 int index = 1; pstmt.setString(index++, name); pstmt.setString(index++, pass); // 6、执行查询 rs = pstmt.executeQuery(); // 7、判断rs.next(); if,while if (rs.next()) { // 实例化对象 entity = new Admin(); // 为对象赋值 ----实际上就是把admin这个表中字段的值,赋值给Admin这个类对象的属性 entity.setId(rs.getInt("id")); // rs.getInt("id")数据库id字段的值 // 赋值给了Class Admin 的id属性 entity.setName(rs.getString("name")); // rs.getString("name")数据库Admin表 // name字段的值 赋值给了 Class // Admin 的 name属性 entity.setPass(rs.getString("pass")); entity.setSex(rs.getString("sex")); entity.setRole(rs.getInt("role")); } // 8、释放资源 DBConn.realse(rs, pstmt); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return entity; } }
修改FindAllServlet
package www.csdn.net.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import www.csdn.net.dao.AdminDaoImpl; import www.csdn.net.domain.Admin; import www.csdn.net.service.AdminService; import www.csdn.net.service.AdminServiceImpl; public class FindAllServlet extends HttpServlet { private AdminService adminService = new AdminServiceImpl(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); String npage = request.getParameter("nowpage"); int countSize = adminService.getCountSize(); //总页数 int countPage=(countSize%AdminDaoImpl.PAGESIZE==0?countSize/AdminDaoImpl.PAGESIZE:countSize/AdminDaoImpl.PAGESIZE+1); int nowpage=0; //等于null if(npage==null){ nowpage=1; }else{ nowpage = Integer.valueOf(npage); if(nowpage<1){ //小于1的情况下 nowpage=1; } if(nowpage>=countPage){ //大于总页数的情况 nowpage=countPage; } } List<Admin> entities = adminService.findNowPageInfo(nowpage); request.setAttribute("entities", entities); request.setAttribute("nowpage", nowpage); request.setAttribute("countpage", countPage); request.getRequestDispatcher("./message/select.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }