分页器,根据过来的参数计算当着页应当从哪一条记录开始显示,并且显示到哪。
PageUtils [pageSize=5, currIndex=1, totalCount=166, totalPage=34, startPosition=0]
PageUtils [pageSize=5, currIndex=5, totalCount=166, totalPage=34, startPosition=20]
PageUtils [pageSize=5, currIndex=34, totalCount=166, totalPage=34, startPosition=165]
PageUtils [pageSize=5, currIndex=1, totalCount=166, totalPage=34, startPosition=0]
PageUtils [pageSize=5, currIndex=9, totalCount=166, totalPage=34, startPosition=40]
PageUtils [pageSize=10, currIndex=9, totalCount=166, totalPage=17, startPosition=80]
package src;public class TestPageUtils {public static void main(String[] args) {// TODO Auto-generated method stub//假设一页显示5条,现在有166条int pageSize = 5;int totalCount = 166;PageUtils pageUtils= new PageUtils(pageSize, totalCount);System.out.println(pageUtils);pageUtils.setCurrIndex(5);System.out.println(pageUtils);pageUtils.setCurrIndex(35);System.out.println(pageUtils);pageUtils.setCurrIndex(-1);System.out.println(pageUtils);pageUtils.setCurrIndex(9); System.out.println(pageUtils);pageUtils.setCurrIndex(9);pageUtils.setPageSize(10);System.out.println(pageUtils);}}
package src;
/*** * @lwd* 分页工具类**/
public class PageUtils {//相关成员变量// 页大小,每页显示多少条记录private int pageSize;//当前页private int currIndex;//总记录数private int totalCount;//总页数private int totalPage;//开始的记录开始的位置 private int startPosition;//一页显示多少条和一共有多少条记录public PageUtils(int pageSize, int totalCount) { this.pageSize = pageSize;this.totalCount = totalCount;currIndex = 1;//默认显示第一页startPosition = 0;//默认从第一条记录开始显示int pages=totalCount/pageSize;totalPage=totalCount%pageSize==0?pages:pages+1;}public int getPageSize() {return pageSize;}public void setPageSize(int pageSize) {this.pageSize = pageSize;int pages=totalCount/pageSize;totalPage=totalCount%this.pageSize==0?pages:pages+1; startPosition=(currIndex-1)*pageSize;}public int getCurrIndex() {return currIndex;}public void setCurrIndex(int currIndex) {if(currIndex<1)currIndex=1;else if(currIndex>totalPage)currIndex=totalPage; this.currIndex = currIndex;startPosition=(currIndex-1)*pageSize;}public int getTotalPage() {return totalPage;}public int getStartPosition() {return startPosition;}@Overridepublic String toString() {return "PageUtils [pageSize=" + pageSize + ", currIndex=" + currIndex + ", totalCount=" + totalCount+ ", totalPage=" + totalPage + ", startPosition=" + startPosition + "]";}}
JSP中调用。
<%@page import="src.PageUtils"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%> <%@ page import="
java.util.Date,
java.io.IOException,
java.io.PrintWriter,
java.sql.*
" %><%@ page import="src.User,src.Message"%><%Date date= new Date();SimpleDateFormat df= new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");%> <%!//留言表共有多少条记录int getTotalCount() throws Exception{Class.forName("com.mysql.cj.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://mysql.sqlpub.com:3306/huangjin","laocooon","fc12f7a5215e8e0a"); String sql = "SELECT COUNT(*) FROM message"; PreparedStatement pstmt = con.prepareStatement(sql); //5.执行sql语句,并且接收结果 ResultSet rs = pstmt.executeQuery();rs.next();int res = rs.getInt(1);rs.close();pstmt.close();con.close(); return res;}%><%//获取pageString pages=request.getParameter("pages");if(pages==null)pages = "1"; int currIndex = Integer.parseInt(pages); PageUtils pageUtils = new PageUtils(10,getTotalCount());//需要提供总记录数pageUtils.setCurrIndex(currIndex);out.println(pageUtils);%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link href="./css/1102.css" rel="stylesheet" type="text/css" />
<title>访客留言板</title></head>
<body>
<p>访客留言板</p>
<p><%="使用的浏览器是:<span style='font-size:16px;'>"+request.getHeader("User-Agent")+"</span>" %></p>
<p><%="使用的ip是:"+request.getRemoteAddr() %></p>
<p><%="现在时间是:"+ df.format(date) %></p>
<p><a href=./index.jsp?#form>我要留言</a></p>
<p> <% //读取message表中的所有记录//1.导入jar包//2.注册驱动//Class.forName("com.mysql.jdbc.Driver");Class.forName("com.mysql.cj.jdbc.Driver");//3.获取连接Connection con = DriverManager.getConnection("jdbc:mysql://mysql.sqlpub.com:3306/huangjin","laocooon","fc12f7a5215e8e0a");//4.获取执行者对象String sql = "SELECT * FROM message ORDER BY id DESC LIMIT ?,?"; PreparedStatement pstmt = con.prepareStatement(sql);pstmt.setInt(1,pageUtils.getStartPosition());pstmt.setInt(2,pageUtils.getPageSize());//5.执行sql语句,并且接收结果 ResultSet rs = pstmt.executeQuery();int i=pageUtils.getStartPosition()+1;while(rs.next()){String author=rs.getString(3);String id=""+rs.getInt(1);String message=rs.getString(2);String posttime=rs.getString(4);out.println("<div class='div1'>");out.println("<p id='p1'>作者:" + author + "</p>");out.println("<p id='p2'>#" + i++ + "</p>");out.println("<p id='p3'>" + message + "</p>");out.println("<p id='p4'>发布日期:" + posttime + "</p>");out.println("<p id='p5'><a href='./index.jsp?id=" + id + "#form'>回复</a></p> ");out.println("</div>"); out.println("<br><br> "); } rs.close();pstmt.close();con.close(); %>
<br>
<div style="text-align: right;">
[一页<%=pageUtils.getPageSize() %>个]
<a href="index.jsp?pages=1">第一页</a>
<a href="index.jsp?pages=<%=pageUtils.getCurrIndex()-1%>">上一页</a>
<a href="index.jsp?pages=<%=pageUtils.getCurrIndex()+1%>">下一页</a>
<a href="index.jsp?pages=<%=pageUtils.getTotalPage()%>">最后页</a>
</div>
<br>
<form action='doadd.jsp' method='get'>
<a id="form"> 用 户 名:</a><input type=text required name=you placeholder=请输入你的姓名 /><br><br><%String id=request.getParameter("id");if(id==null){ out.println("留言信息:<textarea rows=4 cols=60 required name=text placeholder=留言处></textarea><br>"); }else{out.println("留言信息:<textarea rows=4 cols=60 required name=text placeholder=留言处>回复#"+ id +":</textarea><br>");}%><input type='submit'> <input type='reset'></form>
</body>
</html>