jsp分页类
package data;
import java.sql.*;
/*
*类名 Pagination
*功能 对结果集进行分页处理
*版本 1.0
*日期 2003-7-15
*作者 cngift
*版权 cngift
*/
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Pagination{
/*
*@param rs 要进行分页操作的结果集
*@param currentPage 当前页数
*@param sumPage 总页数
*@param listNumber 每页列表数
*@param pageParam 翻页传递的参数
*@param firstPage 首页标志
*@param upPage 上翻页标志
*@param downPage 下翻页标志
*@param lastPage 末页标志
*/
ResultSet rs;
int currentPage;
int sumPage;
int listNumber;
String pageParam;
String firstPage;
String upPage;
String downPage;
String lastPage;
public String getPointPage(int maxLine) throws Exception
{
String tempStr = new String();
if (this.getSumNumber() == 0)
{
tempStr = “本页没有记录”;
return tempStr;
}
if (this.getSumNumber() < maxLine)
{
tempStr = "目前只有一页";
return tempStr;
}
String qstr = this.getPageParam();
int start = this.getStartPoint(this.getCurrentPage());
int end = this.getEndPoint(this.getCurrentPage(),this.getSumPage());
if (start !=1)
{
tempStr += "97“;
}
else
{
tempStr += “97“;
}
for (int i=start;i<=end ;i++ )
{
if (i == this.getCurrentPage())
{
tempStr += "[“+i+”] “;
}
else
{
tempStr += ““+i+” “;
}
}
if (end < this.getSumPage())
{
String tempSum = String.valueOf(this.getSumPage());
tempStr += "8:“;
}
else
{
String tempSum = String.valueOf(this.getSumPage());
tempStr += “8:“;
}
return tempStr;
}
/*
*方法 changeResultSetCursor
*功能 移动结果集的指针到翻页的起始位置
*参数 无
*返回 boolean 操作成功返回true
*/
public boolean changeResuleSetCursor() throws Exception{
//计算当前页的起始记录数
int absoluteLocation=this.listNumber * ( this.currentPage – 1 )+1;
//移动结果集的指针
if(!rs.absolute(absoluteLocation)){
return false;
}
return true;
}
/*
*方法 getSumPage
*功能 得到总页数
*参数 无
*返回 int 总页数
*/
public int getSumPage() throws Exception{
//指针移动到最后
this.rs.last();
//计算总行数
int row = this.rs.getRow();
//计算总页数
int sum = this.ceil(row,this.listNumber);
this.sumPage = sum;
return sum;
}
/*
*方法 getSumNumber
*功能 得到总记录数
*参数 无
*返回 int 总页数
*/
public int getSumNumber() throws Exception{
//指针移动到最后
this.rs.last();
//计算总行数
int row = this.rs.getRow();
return row;
}
/*
*方法 ceil
*功能 当有余数时加1
*参数 a 除数
* b 被除数
*返回 int 结果
*/
private int ceil(int a,int b){
int c = (int) Math.ceil( a / b );
if( ( a % b ) > 0 ){
c = c+1;
}
return c;
}
/*
*方法 getFirstPage
*功能 得到首页连接
*参数 无
*返回 String 首页连接
*/
public String getFirstPage(){
String param;
String page = “[首页]”;
//判断是否设置了翻页标志
if( null != this.firstPage ){
//设置了翻页标志
page = this.firstPage;
}
//当前显示列表不是首页
if(this.currentPage > 1){
param = “” + page + “
“;
}else{
param = page;
}
return param;
}
/*
*方法 getUpPage
*功能 得到上翻页连接
*参数 无
*返回 String 首页连接
*/
public String getUpPage(){
String param = “”;
String page = “[前页]”;
//判断是否设置了翻页标志
if( null != this.upPage ){
//设置了翻页标志
page = this.upPage;
}
//当前显示列表有前页
if(this.currentPage > 1){
this.currentPage–;
param = “” + page + “
“;
this.currentPage++;
}else{
param = page;
}
return param;
}
/*
*方法 getDownPage
*功能 得到下翻页连接
*参数 无
*返回 String 首页连接
*/
public String getDownPage(){
String param = “”;
String page = “[后页]”;
//判断是否设置了翻页标志
if( null != this.downPage ){
//设置了翻页标志
page = this.downPage;
}
//当前显示列表有后页
if(this.currentPage < this.sumPage){
this.currentPage++;
param = "” + page + “
“;
this.currentPage–;
}else{
param = page;
}
return param;
}
/*
*方法 getLastPage
*功能 得到末页连接
*参数 无
*返回 String 首页连接
*/
public String getLastPage(){
String param = “”;
String page = “[末页]”;
//判断是否设置了翻页标志
if( null != this.lastPage ){
//设置了翻页标志
page = this.lastPage;
}
//列表的最后页
if(this.currentPage < this.sumPage && this.sumPage > 0){
param = “” + page + “
“;
}else{
param = page;
}
return param;
}
/*
*方法 setResultSet
*功能 设置本次操作的结果集
*参数 r 结果集
*返回 boolean 操作成功返回true
*/
public boolean setResultSet(ResultSet r){
this.rs = r;
return true;
}
/*
*方法 getResultSet
*功能 得到本次操作的结果集
*参数 无
*返回 ResultSet 本次操作的结果集
*/
public ResultSet getResultSet(){
return this.rs;
}
/*
*方法 setCurrentPage
*功能 设置当前显示的页数
*参数 c 当前页数
*返回 boolean 操作成功返回true
*/
public boolean setCurrentPage(String c){
if( null != c ){
this.currentPage = Integer.parseInt(c);
if( this.currentPage < 1 ){
this.currentPage = 1;
}
}else{
this.currentPage = 1;
}
return true;
}
/*
*方法 setCurrentPage
*功能 设置当前显示的页数
*参数 c 当前页数
*返回 boolean 操作成功返回true
*/
public boolean setCurrentPage(int c){
if(c > 0){
this.currentPage = c;
}else{
this.currentPage = 1;
}
return true;
}
/*
*方法 getCurrentPage
*功能 得到当前显示的页数
*参数 无
*返回 int 当前页数
*/
public int getCurrentPage(){
return this.currentPage;
}
/*
*方法 setListNumber
*功能 设置当前显示的列表数
*参数 l 当前列表数
*返回 boolean 操作成功返回true
*/
public boolean setListNumber(int l){
this.listNumber = l;
return true;
}
/*
*方法 setListNumber
*功能 得到当前显示的列表数
*参数 无
*返回 int 当前显示的列表数
*/
public int getListNumber(){
return this.listNumber;
}
/*
*方法 setFirstPageString
*功能 设置翻页的首页标志
*参数 f 首页标志
*返回 boolean 操作成功返回true
*/
public boolean setFirstPageString(String f){
this.firstPage = f;
return true;
}
/*
*方法 getFirstPageString
*功能 得到翻页的首页标志
*参数 无
*返回 String 翻页的首页标志
*/
public String getFirstPageString(){
return this.firstPage;
}
/*
*方法 setUpPageString
*功能 设置翻页的上翻页标志
*参数 u 上翻页标志
*返回 boolean 操作成功返回true
*/
public boolean setUpPageString(String u){
this.upPage = u;
return true;
}
/*
*方法 getUpPageString
*功能 得到翻页的上翻页标志
*参数 无
*返回 String 翻页的下翻页标志
*/
public String getUpPageString(){
return this.upPage;
}
/*
*方法 setDownPageString
*功能 得到翻页的下翻页标志
*参数 d 下翻页标志
*返回 boolean 操作成功返回true
*/
public boolean setDownPageString(String d){
this.downPage = d;
return true;
}
/*
*方法 getDownPageString
*功能 得到翻页的下翻页标志
*参数 无
*返回 String 翻页的下翻页标志
*/
public String getDownPageString(){
return this.downPage;
}
/*
*方法 setLastPageString
*功能 得到翻页的末页标志
*参数 l 末页标志
*返回 boolean 操作成功返回true
*/
public boolean setLastPageString(String l){
this.lastPage = l;
return true;
}
/*
*方法 getLastPageString
*功能 得到翻页的末页标志
*参数 无
*返回 String 翻页得末页标志
*/
public String getLastPageString(){
return this.lastPage;
}
/*
*方法 setPageParam
*功能 设置翻页所用参数
*参数 p 翻页参数
*返回 boolean 操作成功返回true
*/
public boolean setPageParam(String p){
this.pageParam = p;
return true;
}
/*
*方法 getPageParam
*功能 得到翻页所用参数
*参数 无
*返回 boolean 操作成功返回true
*/
public String getPageParam(){
return this.pageParam;
}
//
private int getStartPoint(int current)
{
String tempSubInt;
int tempInt = 0;
while (current > 0)
{
if (current ==1)
{
tempInt = current;
}
tempSubInt = (current+””).substring((current+””).length()-1);
if (Integer.parseInt(tempSubInt) == 0)
{ //substr取部份字串
tempInt = current;
break;
}
else
{
current–;
}
}
return tempInt;
}
//
private int getEndPoint(int current, int total) throws Exception
{
String tempSubInt;
int tempInt = 0;
while (current <= total)
{
if (current == total) {
tempInt = current;
}
tempSubInt = (current+"").substring((current+"").length()-1);
if (Integer.parseInt(tempSubInt) == 9)
{ //substr取部份字串
tempInt = current;
break;
}
else
{
current++;
}
}
return tempInt;
}
}
测试未成功;以后再测
<%@page contentType="text/HTML;charset=gb2312"%>
<%request.setCharacterEncoding("GB2312");%>
<%@ page import="com.zysoft.*"%>
<%@ include file="log/log_conn.jsp"%>
<%
String sql="select * from log_content order by log_id desc";
ResultSet rs=null;
int pages; //接受的页码变量
int i;
rs=stmt.executeQuery(sql);
pagenation ppg = new pagenation();
ppg.setResultSet(rs);
ppg.setListNumber(2);//设置每页显示条数
int sumnum=ppg.getSumNumber();//取得总条数
int sumpage=ppg.getSumPage();//取得总页数
//判断参数currentPage是否为空
if (request.getParameter("currentPage")==null) {
pages=1;
}else {
pages =new Integer(request.getParameter("currentPage")).intValue();
}
ppg.setCurrentPage(pages);
i=(pages-1)*ppg.getListNumber();
while(i
i++;
}
out.println(ppg.getPointPage(2));//显示上翻下翻页
%>
=====================
/*
* 创建日期 2005-3-27
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package mall.page;
import java.sql.*;
/**
* @author Administrator
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class Pageable {
ResultSet rs = null;
String currenPage = null; //传递来的参数页
int intPage = 0; //当前页
int pageSize = 0; //每页显示的记录数
int totalRecord = 0; //总记录数
int totalPage = 0; //总页数
int pageStart = 0; //每页开始的记录数
int backPages = 0;
int toPages = 0;
String s = “”;
String s1 = null;
String s2 = “”;
String s3 = “”;
String s4 = “”;
String url = null;
public void setUrl(String url)
{
this.url = url;
}
public void setPage(ResultSet rs,String currenPage,int pageSize)
{
try
{
rs.last();
totalRecord = rs.getRow();
totalPage = (totalRecord + pageSize -1)/pageSize;
if (currenPage == null || currenPage.length() <= 0)
{
intPage = 1;
}else
{
intPage = Integer.parseInt(currenPage);
if(intPage < 1)
{
intPage = 1;
}
if(intPage > totalPage)
{
intPage = totalPage;
}
}
pageStart = (intPage – 1)* pageSize + 1;
this.setPages();
this.viewPage();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void setPages()
{
s1 = “共有“+totalRecord+” 条信息,分为“+totalPage+”页 “;
if (intPage == 1)
{
s2 = ” “;
}
else
{
s2 = “首页 上一页 “;
}
if (intPage == totalPage)
{
s3 = ” “;
}
else
{
s3 = “下一页 末页“;
}
}
public void viewPage()
{
if ((intPage – 4) < 1)
{
backPages = 1;
}
else
{
backPages = intPage - 4;
}
if ((intPage + 4) > totalPage)
{
toPages = totalPage;
}
else
{
toPages = intPage + 4;
}
for ( int i = backPages;i<=toPages;i++)
{
if (i== intPage)
{
s4 = s4 + " “+ i +” “;
}
else
{
s4 = s4 + “[“+ i +”] “;
}
}
}
public String getPages()
{
s = s1 + s2 + s4 + s3 ;
return s;
}
public int getPageStart()
{
return pageStart;
}
}
<%@page contentType="text/HTML;charset=gb2312"%>
<%request.setCharacterEncoding("GB2312");%>
<%@ page import="com.zysoft.*"%>
<%@ include file="log/log_conn.jsp"%>
<%@ page import="java.util.ArrayList"%>
<%
page p = new page();
String sql="select * from log_content order by log_id desc";
ResultSet rs=null;
rs = stmt.executeQuery(sql);
p.setUrl("");
p.setPage(rs,"1",2);
int pages;
int i;
pages=request.getParameter("page");
if (pages==null) pages=1;
i=(pages-1)*2;
while(rs.next()&& i<2)
{out.println(rs.getSting("log_id"));}
out.println(p.getPages());
%>
=====================
插入代码:
package J_Blog.BluePoint.Page;
import java.util.regex.Pattern;
import java.io.Serializable;
/**
* 版权所有: 蓝点设计
* 创 建 人: 开 发 者
* 创建日期: 2006-2-23
* 创建时间: 22:57:08
* 版 本 号: Version 1.0
* 功能说明: [J-Blog]分页,带偏移量功能
*/
public class Page implements Serializable {
private int PageNum; /* 记录集总数 */
private int PageSize = 10; /* 游标长度 */
private int NowPage = 1; /* 当前页码 */
private int MovePage = 5; /* 页码偏移量 */
int TotalPage; /* 总页数 */
int NextPage; /* 下一页,双向 [<<][<] [>][>>] */
int StartPage; /* 下一页数据库记录开始游标 */
String PageHtml = “”; /* 分页HTML编码 */
public int getPageNum() {
return PageNum;
}
public void setPageNum(int pageNum) {
PageNum = pageNum;
}
public int getPageSize() {
return PageSize;
}
public void setPageSize(int pageSize) {
PageSize = pageSize;
}
public int getNowPage() {
return NowPage;
}
public void setNowPage(int nowPage) {
NowPage = nowPage;
}
public int getMovePage() {
return MovePage;
}
public void setMovePage(int movePage) {
MovePage = movePage;
}
public int[] SetPage(int SetPageNum,int SetPageSize,String SetNowPage,int SetMovePage){
int[] setPage = new int[2];
try{
this.setPageNum(SetPageNum);
this.setPageSize(SetPageSize);
/* 处理当前页的数值 */
/* [正则]如果填入非法字符,置 1 */
int pages = Pattern.matches(“[0-9]+”,SetNowPage) ? Integer.parseInt(SetNowPage) : 1;
/* 如果等于0, 置 1 */
this.setNowPage(pages == 0 ? 1 : pages);
/* 如果大于总页数,置 1 */
int totalpage = this.getPageNum() % this.getPageSize() == 0 ? this.getPageNum() / this.getPageSize() : this.getPageNum() / this.getPageSize() + 1;
this.setNowPage(this.getNowPage() > totalpage ? 1 : this.getNowPage());
this.setMovePage(SetMovePage);
setPage[0] = (this.getNowPage() – 1) * this.getPageSize();
setPage[1] = setPage[0] + this.getPageSize();
}catch(Exception err){
System.out.println(“设置分页参数失败!” + err);
}
return setPage;
}
public String[] ShowPage(String URL,String Style){
String[] showpage = new String[2];
try{
/* 设置总页数 */
this.TotalPage = this.getPageNum() % this.getPageSize() == 0 ? this.getPageNum() / this.getPageSize() : this.getPageNum() / this.getPageSize() + 1;
/* 设置下一页 */
this.NextPage = this.getNowPage() % this.getMovePage() == 0 ? this.getNowPage() / this.getMovePage() : this.getNowPage() / this.getMovePage() + 1;
/* 设置当然从第几条数据分页 */
int pagenum = this.TotalPage > this.getMovePage() ? this.getMovePage() : this.TotalPage;
if(this.getNowPage() > 1){
this.PageHtml = “[1]…”;
}
if(this.NextPage > 1){
int NextPageID = (this.NextPage – 1) * this.getMovePage();
this.PageHtml = this.PageHtml + “[<<]“;
}
if(this.getNowPage() > 1){
int pre = this.getNowPage() – 1;
this.PageHtml = this.PageHtml + “[<]“;
}
int i = (this.NextPage – 1) * this.getMovePage();
for(int j = i ; j < (i + pagenum) && j < this.TotalPage ;j++){
int newpage = j + 1;
if(this.getNowPage() == (j + 1)){
this.PageHtml = this.PageHtml + "[” + (j + 1) + “]“;
}else{
this.PageHtml = this.PageHtml + “[” + newpage + “]“;
}
}
if(this.getNowPage() < this.TotalPage){
int next = this.getNowPage() + 1;
this.PageHtml = this.PageHtml + "[>]“;
}
if(this.NextPage < (this.TotalPage / this.getMovePage())){
int nextpre = (this.NextPage + 1) * this.getMovePage();
this.PageHtml = this.PageHtml + "[>>]“;
}
if(this.getNowPage() < this.TotalPage){
this.PageHtml = this.PageHtml + "...[” + this.TotalPage + “]“;
}
showpage[0] = “共有” + this.getPageNum() + “条记录,每页” + this.getPageSize() + “条记录 ” + this.PageHtml;
showpage[1] = “ “;
}catch(Exception err){
System.out.println(“获取分页HTML内容失败!” + err);
}
return showpage;
}
}
使用方法
插入代码:
<%@ page import="J_Blog.BluePoint.Page.Page"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="J_Blog.BluePoint.sql.CreateSQL.Select"%>
<%
Page p = new Page();
/* 我们自己生成一个SQL语句类,大家在应用时改成相应的自己的方法 */
Select select = new Select();
/* 获取当前页码,如果为空置 1 */
String NowPage = request.getParameter("page") == null ? "1" : request.getParameter("page");
/* 设置需要查询的字段,为空表示查询所有字段 */
String[] Fields = {};
/* 查询数据,返回一个ArrayList */
ArrayList ResultList = select.SelectToDataBase("J_Blog_Comment",Fields,"","J_Blog_ID",1,0,0);
/* 设置分页参数,SetPage(总记录数,每页显示记录集数,"当前页码",偏移值) */
int[] p_config = p.SetPage(ResultList.size(),10,NowPage,5);
int i = 0;
/* 循环输出记录集 */
for(Object aResultList : ResultList){
String[][] List = (String[][])aResultList;
i++;
/**
* 所有数据库通用型分法,低效点
* 如果是MySQL数据 可以 写在 limit(p_config[0],p_config[1])
* MsSQL 可以写在top p_config[1],然后再云截取
* 其它数据库自己想吧
*/
if(i > p_config[0] && i<= p_config[1]){
//输出的表中的ID测试
out.println("test" + i + " => ” + List[i-1][0]);
}
}
/* 输出分页,ShowPage(“当前页面的URL注意要加?号”,”样式表只要填写名称”) */
out.println(p.ShowPage(“page.jsp?”,””)[0]);
/* 如果要有GO功能,如下 */
out.println(“
“);
%>