Java——分页 Servlet + Jsp+Jdbc 有点瑕疵
1.创建数据库,插入多条数据
2.java连接DB
3.Person类:
package com.phome.po; public class Person { private int id; private int age; private String name; private String sex; public int getId() { return id; } public void setId(int id) { this.id = id; } //...Setter() And Getter(); }
4.Dao操作类:
public class PersonDao { /** * ADD * @param person * @return * @throws ClassNotFoundException * @throws SQLException */ public boolean add(Person person) throws ClassNotFoundException, SQLException { String sql = "Insert into Persons(name,age,sex) values(?,?,?)"; Connection conn = null; PreparedStatement ps = null; boolean success = false; try { conn = DbUtil.getConnection(); ps = conn.prepareStatement(sql); //设置参数 ps.setString(1, person.getName()); ps.setInt(2, person.getAge()); ps.setString(3, person.getSex()); //执行sql语句 success = (ps.executeUpdate() > 0); } catch (SQLException e) { System.out.println("【PersonDao -> add()发生异常】\n【异常信息】" + e.getErrorCode()); throw e; } finally { DbUtil.close(ps); DbUtil.close(conn); } return success; } /** * Page List * @param pageIndex * @param pageSize * @param condition * @return * @throws ClassNotFoundException * @throws SQLException */ public List<Person> findByPaging(int pageIndex,int pageSize,String condition) throws ClassNotFoundException, SQLException{ String sql = "Select * From Persons"; if(condition != null){ sql += " Where " + condition; } sql += " limit " + ((pageIndex - 1) * pageSize) + "," + pageSize ; Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; List<Person> persons = new ArrayList<Person>(); try { conn = DbUtil.getConnection(); ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while (rs.next()) { persons.add(rs2Person(rs)); } } catch (SQLException e) { System.out.println("【PersonDao -> findByPaging()发生异常】" + "\n【异常信息】" + e.getMessage()); throw e; } finally { DbUtil.close(rs); DbUtil.close(ps); DbUtil.close(conn); } return persons; } /** * 得到总数 * @param condition * @return * @throws ClassNotFoundException * @throws SQLException */ public int getCount(String condition) throws ClassNotFoundException, SQLException{ String sql = "Select count(*) From Persons"; if(condition != null){ sql += " Where " + condition; } Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; int count = 0; try { conn = DbUtil.getConnection(); ps = conn.prepareStatement(sql); rs = ps.executeQuery(); if(rs.next()){ count = rs.getInt(1); } } catch (SQLException e) { System.out.println("【PersonDao ->getCount()发生异常】" + "\n【异常信息】" + e.getMessage()); throw e; } finally { DbUtil.close(rs); DbUtil.close(ps); DbUtil.close(conn); } return count; } /** * 查询Person的结果集 * @param rs * @return * @throws SQLException */ private Person rs2Person(ResultSet rs) throws SQLException { Person person = null; try { person = new Person(); person.setId(rs.getInt("Id")); person.setName(rs.getString("Name")); person.setAge(rs.getInt("Age")); person.setSex(rs.getString("Sex")); } catch (SQLException e) { System.out.println("【PersonDao -> rs2Person()发生异常】" + "\n【异常信息】" + e.getMessage()); throw e; } return person; } }
5.Servlet类:
public class ShowPageServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { int pageIndex = 1; int count = 0; int pageSize = 10; PersonDao dao = new PersonDao(); List<Person> list= new ArrayList<Person>(); if(req.getParameter("pageIndex") != null){ pageIndex = Integer.parseInt(req.getParameter("pageIndex"));; } try { count = dao.getCount(null); list = dao.findByPaging(pageIndex, pageSize, null); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } int pageCount = count/10; req.setAttribute("pageCount", pageCount);//共 页 req.setAttribute("list", list); req.setAttribute("pageIndex", pageIndex);//第 页 req.setAttribute("count", count); req.getRequestDispatcher("showpage.jsp").forward(req, resp); } }
6.xml文件配置
<servlet> <servlet-name>page</servlet-name> <servlet-class>com.phome.servlet.ShowPageServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>page</servlet-name> <url-pattern>/page</url-pattern> </servlet-mapping>
7.Jsp
Index.jsp
<form action="${pageContext.request.contextPath}/page" method="post" > <input type="submit" value="分页显示" /> </form>
showpage.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>分页管理页面</title> <style> *{ font-size:12px; } table{ width:100%; height:100%; border-collapse:collapse; border:1px solid #78C0D4; } td,th{ border:1px solid #78C0D4; } .first-line{ height:20px; background-color:#A5D5E2; } </style> </head> <body> <div style="width:600px;height:400px;margin-left:300px;"> <table align="center" width="300px"> <tr class="first-line"> <th> Id </th> <th> 姓名 </th> <th> 年龄 </th> <th> 性别 </th> </tr> <c:forEach var="person" items="${list}" varStatus="s"> <tr <c:if test="${s.index mod 2 != 0}">style="background-color:#eee"</c:if> > <td> ${person.id} </td> <td> ${person.name} </td> <td> ${person.age} </td> <td> ${person.sex} </td> </tr> </c:forEach> </table> </div> <div style="width:600px;height:400px;margin-left:500px;"> <a href="page?pageIndex=1">首页</a> <a href="page?pageIndex=${pageIndex<=1 ? 1:pageIndex-1 }">上页</a> <a style="color:red;">第${pageIndex }页</a> / <a style="color:red;">共${pageCount+1 }页</a> <a href="page?pageIndex=${pageIndex>=pageCount+1 ? pageCount+1:pageIndex+1 }">下页</a> <a href="page?pageIndex=${pageCount+1 }">尾页</a> <form action="${pageContext.request.contextPath}/page"> 跳转至<input type="text" name="pageIndex" style="width:25px;"/>页 <input type="submit" value="跳转" /> </form> </div> </body> </html>
效果图:
逃避不一定躲得过,面对不一定最难过