jsp分页类

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 = “
<%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(“

“);
out.println(p.ShowPage(“page.jsp?”,””)[1]);
out.println(“

“);
%>

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理

相关文章

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部