[转载]JSP分页技术实现

JSP分页技术实现
目前比较广泛使用的分页方式是将查询结果缓存在HttpSession或有状态bean中,翻页的时候从缓存中取出一页数据显示。这种方法有两个主要的缺 点:一是用户可能看到的是过期数据;二是如果数据量非常大时第一次查询遍历结果集会耗费很长时间,并且缓存的数据也会占用大量内存,效率明显下降。
   其它常见的方法还有每次翻页都查询一次数据库,从ResultSet中只取出一页数据(使用rs.last();rs.getRow()获得总计录条 数,使用rs.absolute()定位到本页起始记录)。这种方式差不多也是需要遍历所有记录,实验证明在记录数很大时速度非常慢。
  至于缓存结果集ResultSet的方法则完全是一种错误的做法。因为ResultSet在Statement或Connection关闭时也会被关闭,如果要使ResultSet有效势必长时间占用数据库连接。

   因此比较好的分页做法应该是每次翻页的时候只从数据库里检索页面大小的块区的数据。这样虽然每次翻页都需要查询数据库,但查询出的记录数很少,网络传输 数据量不大,如果使用连接池更可以略过最耗时的建立数据库连接过程。而在数据库端有各种成熟的优化技术用于提高查询速度,比在应用服务器层做缓存有效多 了。

  在oracle数据库中查询结果的行号使用伪列ROWNUM表示(从1开始)。例如select * from employee where rownum<10 返回前10条记录。但因为rownum是在查询之后排序之前赋值的,所以查询employee按birthday排序的第100到120条记录应该这么 写:
        select * from (
select my_table.*, rownum as my_rownum from (
select name, birthday from employee order by birthday
) my_table where rownum <120
) where my_rownum>=100

  mySQL可以使用LIMIT子句:
    select name, birthday from employee order by birthday LIMIT 99,20
  DB2有rownumber()函数用于获取当前行数。
  SQL Server没研究过,可以参考这篇文章: http://www.csdn.net/develop/article/18/18627.shtm

   在Web程序中分页会被频繁使用,但分页的实现细节却是编程过程中比较麻烦的事情。大多分页显示的查询操作都同时需要处理复杂的多重查询条件,sql语 句需要动态拼接组成,再加上分页需要的记录定位、总记录条数查询以及查询结果的遍历、封装和显示,程序会变得很复杂并且难以理解。因此需要一些工具类简化 分页代码,使程序员专注于业务逻辑部分。下面是我设计的两个工具类:
   PagedStatement 封装了数据库连接、总记录数查询、分页查询、结果数据封装和关闭数据库连接等操作,并使用了PreparedStatement支持动态设置参数。
   RowSetPage 参考PetStore的page by page iterator模式, 设计RowSetPage用于封装查询结果(使用OracleCachedRowSet缓存查询出的一页数据,关于使用CachedRowSet封装数据库查询结果请参考 JSP页面查询显示常用模式)以及当前页码、总记录条数、当前记录数等信息, 并且可以生成简单的HTML分页代码。
  PagedStatement 查询的结果封装成RowsetPage。

  下面是简单的 使用示例



public RowSetPage getEmployee(String gender, int pageNo) throws Exception{
String sql=;

PagedStatement pst =new PagedStatementOracleImpl(sql, pageNo, 5);
pst.setString(1, gender);
return pst.executeQuery();
}





int pageNo;
try{

pageNo = Integer.parseInt(request.getParameter() );
}catch(Exception ex){

pageNo=1;
}
String gender = request.getParameter( );
request.setAttribute(, myBean.getEmployee(gender, pageNo) );



import = %>





性别:
"gender ")%>">
" 查询 " οnclick= "doQuery()">

RowSetPage empPage = (RowSetPage)request.getAttribute( "empPage");
if (empPage == null ) empPage = RowSetPage.EMPTY_PAGE;
%>

"90%">
ID 代码 用户名 姓名
javax.sql.RowSet empRS = (javax.sql.RowSet) empPage.getRowSet();
if (empRS!=null) while (empRS.next() ) {
%>

"EMP_ID")%>
"EMP_CODE")%>
"USER_NAME")%>
"REAL_NAME")%>

} // end while
%>

//显示总页数和当前页数(pageno)以及分页代码。
//此处doQuery为页面上提交查询动作的java script函数名, pageno为标识当前页码的参数名
%>
"doQuery", "pageno")%>


  效果如图:
pagediv.jpg

   因为分页显示一般都会伴有查询条件和查询动作,页面应已经有校验查询条件和提交查询的java script方法(如上面的doQuery),所以RowSetPage.getHTML()生成的分页代码在用户选择新页码时直接回调前面的处理提交查 询的java script方法。注意在显示查询结果的时候上次的查询条件也需要保持,如">。同时由于页码的参数名可以指定,因此也 支持在同一页面中有多个分页区。
  另一种分页代码实现是生成每一页的URL,将查询参数和页码作为QueryString附在URL后面。这种方法的缺陷是在查询条件比较复杂时难以处理,并且需要指定处理查询动作的servlet,可能不适合某些定制的查询操作。
  如果对RowSetPage.getHTML()生成的默认分页代码不满意可以编写自己的分页处理代码,RowSetPage提供了很多getter方法用于获取相关信息(如当前页码、总页数、 总记录数和当前记录数等)。
  在实际应用中可以将分页查询和显示做成jsp taglib, 进一步简化JSP代码,屏蔽Java Code。

附:分页工具类的源代码, 有注释,应该很容易理解。

1.Page.java
2.RowSetPage.java(RowSetPage继承Page)
3.PagedStatement.java
4.PagedStatementOracleImpl.java(PagedStatementOracleImpl继承PagedStatement)



您可以任意使用这些源代码,但必须保留author evan_zhao@hotmail.com字样








package page;

import java.util.List;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;



public class Page implements java.io.Serializable {
public static final Page EMPTY_PAGE = new Page();
public static final int DEFAULT_PAGE_SIZE = 20;
public static final int MAX_PAGE_SIZE = 9999;

private int myPageSize = DEFAULT_PAGE_SIZE;

private int start;
private int avaCount,totalSize;
private Object data;

private int currentPageno;
private int totalPageCount;


protected Page(){
this.init(0,0,0,DEFAULT_PAGE_SIZE,new Object());
}


protected void init(int start, int avaCount, int totalSize, int pageSize, Object data){

this.avaCount =avaCount;
this.myPageSize = pageSize;

this.start = start;
this.totalSize = totalSize;

this.data=data;



if (avaCount>totalSize) {

}

this.currentPageno = (start -1)/pageSize +1;
this.totalPageCount = (totalSize + pageSize -1) / pageSize;

if (totalSize==0 && avaCount==0){
this.currentPageno = 1;
this.totalPageCount = 1;
}

}

public Object getData(){
return this.data;
}


public int getPageSize(){
return this.myPageSize;
}


public boolean hasNextPage() {

return (this.getCurrentPageNo()<this.getTotalPageCount());
}


public boolean hasPreviousPage() {

return (this.getCurrentPageNo()>1);
}


public int getStart(){
return start;
}


public int getEnd(){
int end = this.getStart() + this.getSize() -1;
if (end<0) {
end = 0;
}
return end;
}


public int getStartOfPreviousPage() {
return Math.max(start-myPageSize, 1);
}



public int getStartOfNextPage() {
return start + avaCount;
}


public static int getStartOfAnyPage(int pageNo){
return getStartOfAnyPage(pageNo, DEFAULT_PAGE_SIZE);
}


public static int getStartOfAnyPage(int pageNo, int pageSize){
int startIndex = (pageNo-1) * pageSize + 1;
if ( startIndex < 1) startIndex = 1;

return startIndex;
}


public int getSize() {
return avaCount;
}


public int getTotalSize() {
return this.totalSize;
}


public int getCurrentPageNo(){
return this.currentPageno;
}


public int getTotalPageCount(){
return this.totalPageCount;
}



public String getHTML(String queryJSFunctionName, String pageNoParamName){
if (getTotalPageCount()<1){
return ;
}
if (queryJSFunctionName == null || queryJSFunctionName.trim().length()<1) {
queryJSFunctionName = ;
}
if (pageNoParamName == null || pageNoParamName.trim().length()<1){
pageNoParamName = ;
}

String gotoPage = +queryJSFunctionName;

StringBuffer html = new StringBuffer();
html.append()
.append().append(gotoPage).append()
.append( )
.append( )
.append(pageNoParamName).append()
.append( ).append(pageNoParamName)
.append()
.append( ).append(queryJSFunctionName).append()
.append( )
.append( )



.append( )
.append(queryJSFunctionName).append()
.append( ).append(pageNoParamName)
.append()
.append( )

.append( )
.append( )
.append( )
.append( );
html.append(
" ")
.append( "
");
html.append( " 共" ).append( getTotalPageCount() ).append( "页")
.append( " [") .append(getStart()).append("..").append(getEnd())
.append("/").append(this.getTotalSize()).append("] ")
.append( " ")
.append( " ");
if (hasPreviousPage()){
html.append( "[上一页] ");
}
html.append( " 第")
.append( " ");
String selected = "selected";
for(int i=1;i<=getTotalPageCount();i++){
if( i == getCurrentPageNo() )
selected = "selected";
else selected = "";
html.append( " )
.append(selected).append(">").append(i).append(" "
);
}
if (getCurrentPageNo()>getTotalPageCount()){
html.append( " ").append(getCurrentPageNo())
.append(" ");
}
html.append( " 页 ");
if (hasNextPage()){
html.append( " [下一页] ");
}
html.append( "
");

return html.toString();

}
}




///
//
// RowSetPage.java
// author: evan_zhao@hotmail.com
//
///
package page;

import javax.sql.RowSet;


/**
*

Title: RowSetPage


*

Description: 使用RowSet封装数据的分页对象


*

Copyright: Copyright (c) 2003


* @author evan_zhao@hotmail.com
* @version 1.0
*/

public class RowSetPage extends Page {
private javax.sql.RowSet rs;

/**
*空页
*/

public static final RowSetPage EMPTY_PAGE = new RowSetPage();

/**
*默认构造方法,创建空页
*/

public RowSetPage(){
this(null, 0,0);
}

/**
*构造分页对象
*@param crs 包含一页数据的OracleCachedRowSet
*@param start 该页数据在数据库中的起始位置
*@param totalSize 数据库中包含的记录总数
*/

public RowSetPage(RowSet crs, int start, int totalSize) {
this(crs,start,totalSize,Page.DEFAULT_PAGE_SIZE);
}

/**
*构造分页对象
*@param crs 包含一页数据的OracleCachedRowSet
*@param start 该页数据在数据库中的起始位置
*@param totalSize 数据库中包含的记录总数
*@pageSize 本页能容纳的记录数
*/

public RowSetPage(RowSet crs, int start, int totalSize, int pageSize) {
try{
int avaCount=0;
if (crs!=null) {
crs.beforeFirst();
if (crs.next()){
crs.last();
avaCount = crs.getRow();
}
crs.beforeFirst();
}
rs = crs;
super.init(start,avaCount,totalSize,pageSize,rs);
} catch(java.sql.SQLException sqle){
throw new RuntimeException(sqle.toString());
}
}

/**
*取分页对象中的记录数据
*/

public javax.sql.RowSet getRowSet(){
return rs;
}


}




///
//
// PagedStatement.java
// author: evan_zhao@hotmail.com
//
///

package page;

import foo.DBUtil;

import java.math.BigDecimal;
import java.util.List;
import java.util.Iterator;
import java.util.Collections;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.Timestamp;
import javax.sql.RowSet;

/**
*

Title: 分页查询


*

Description: 根据查询语句和页码查询出当页数据


*

Copyright: Copyright (c) 2002


* @author evan_zhao@hotmail.com
* @version 1.0
*/
public abstract class PagedStatement {
public final static int MAX_PAGE_SIZE = Page.MAX_PAGE_SIZE;

protected String countSQL, querySQL;
protected int pageNo,pageSize,startIndex,totalCount;
protected javax.sql.RowSet rowSet;
protected RowSetPage rowSetPage;

private List boundParams;

/**
* 构造一查询出所有数据的PageStatement
* @param sql query sql
*/

public PagedStatement(String sql){
this(sql,1,MAX_PAGE_SIZE);
}


/**
* 构造一查询出当页数据的PageStatement
* @param sql query sql
* @param pageNo 页码
*/

public PagedStatement(String sql, int pageNo){
this(sql, pageNo, Page.DEFAULT_PAGE_SIZE);
}

/**
* 构造一查询出当页数据的PageStatement,并指定每页显示记录条数
* @param sql query sql
* @param pageNo 页码
* @param pageSize 每页容量
*/

public PagedStatement(String sql, int pageNo, int pageSize){
this.pageNo = pageNo;
this.pageSize = pageSize;
this.startIndex = Page.getStartOfAnyPage(pageNo, pageSize);
this.boundParams = Collections.synchronizedList( new java.util.LinkedList());

this.countSQL = "select count(*) from ( " + sql + ") ";
this.querySQL = intiQuerySQL(sql, this.startIndex, pageSize);
}


/**
*生成查询一页数据的sql语句
*@param sql 原查询语句
*@startIndex 开始记录位置
*@size 需要获取的记录数
*/

protected abstract String intiQuerySQL(String sql, int startIndex, int size);


/**
*使用给出的对象设置指定参数的值
*@param index 第一个参数为1,第二个为2,。。。
*@param obj 包含参数值的对象
*/

public void setObject( int index, Object obj) throws SQLException{
BoundParam bp = new BoundParam(index, obj);
boundParams.remove(bp);
boundParams.add( bp);
}

/**
*使用给出的对象设置指定参数的值
*@param index 第一个参数为1,第二个为2,。。。
*@param obj 包含参数值的对象
*@param targetSqlType 参数的数据库类型
*/

public void setObject( int index, Object obj, int targetSqlType) throws SQLException{
BoundParam bp = new BoundParam(index, obj, targetSqlType);
boundParams.remove(bp);
boundParams.add(bp );
}

/**
*使用给出的对象设置指定参数的值
*@param index 第一个参数为1,第二个为2,。。。
*@param obj 包含参数值的对象
*@param targetSqlType 参数的数据库类型(常量定义在java.sql.Types中)
*@param scale 精度,小数点后的位数
* (只对targetSqlType是Types.NUMBER或Types.DECIMAL有效,其它类型则忽略)
*/

public void setObject( int index, Object obj, int targetSqlType, int scale) throws SQLException{
BoundParam bp = new BoundParam(index, obj, targetSqlType, scale) ;
boundParams.remove(bp);
boundParams.add(bp);
}

/**
*使用给出的字符串设置指定参数的值
*@param index 第一个参数为1,第二个为2,。。。
*@param str 包含参数值的字符串
*/

public void setString( int index, String str) throws SQLException{
BoundParam bp = new BoundParam(index, str) ;
boundParams.remove(bp);
boundParams.add(bp);
}

/**
*使用给出的字符串设置指定参数的值
*@param index 第一个参数为1,第二个为2,。。。
*@param timestamp 包含参数值的时间戳
*/

public void setTimestamp( int index, Timestamp timestamp) throws SQLException{
BoundParam bp = new BoundParam(index, timestamp) ;
boundParams.remove(bp);
boundParams.add( bp );
}

/**
*使用给出的整数设置指定参数的值
*@param index 第一个参数为1,第二个为2,。。。
*@param value 包含参数值的整数
*/

public void setInt( int index, int value) throws SQLException{
BoundParam bp = new BoundParam(index, new Integer(value)) ;
boundParams.remove(bp);
boundParams.add( bp );
}

/**
*使用给出的长整数设置指定参数的值
*@param index 第一个参数为1,第二个为2,。。。
*@param value 包含参数值的长整数
*/

public void setLong( int index, long value) throws SQLException{
BoundParam bp = new BoundParam(index, new Long(value)) ;
boundParams.remove(bp);
boundParams.add( bp );
}

/**
*使用给出的双精度浮点数设置指定参数的值
*@param index 第一个参数为1,第二个为2,。。。
*@param value 包含参数值的双精度浮点数
*/

public void setDouble( int index, double value) throws SQLException{
BoundParam bp = new BoundParam(index, new Double(value)) ;
boundParams.remove(bp);
boundParams.add( bp);
}

/**
*使用给出的BigDecimal设置指定参数的值
*@param index 第一个参数为1,第二个为2,。。。
*@param bd 包含参数值的BigDecimal
*/

public void setBigDecimal( int index, BigDecimal bd) throws SQLException{
BoundParam bp = new BoundParam(index, bd ) ;
boundParams.remove(bp);
boundParams.add( bp);
}

private void setParams(PreparedStatement pst) throws SQLException{
if (pst==null || this.boundParams==null || this.boundParams.size()==0 ) return ;
BoundParam param;
for (Iterator itr = this.boundParams.iterator();itr.hasNext();){
param = (BoundParam) itr.next();
if (param==null) continue;
if (param.sqlType == java.sql.Types.OTHER){
pst.setObject(param.index, param.value);
} else{
pst.setObject(param.index, param.value, param.sqlType, param.scale);
}
}
}



/**
* 执行查询取得一页数据,执行结束后关闭数据库连接
* @return RowSetPage
* @throws SQLException
*/

public RowSetPage executeQuery() throws SQLException{
System.out.println( "executeQueryUsingPreparedStatement");
Connection conn = DBUtil.getConnection();
PreparedStatement pst = null;
ResultSet rs = null;
try{
pst = conn.prepareStatement( this.countSQL);
setParams(pst);
rs =pst.executeQuery();
if (rs.next()){
totalCount = rs.getInt(1);
} else {
totalCount = 0;
}

rs.close();
pst.close();

if (totalCount < 1 ) return RowSetPage.EMPTY_PAGE;

pst = conn.prepareStatement( this.querySQL);
System.out.println(querySQL);
pst.setFetchSize( this.pageSize);
setParams(pst);
rs =pst.executeQuery();
//rs.setFetchSize(pageSize);

this.rowSet = populate(rs);

rs.close();
rs = null;
pst.close();
pst = null;

this.rowSetPage = new RowSetPage( this.rowSet,startIndex,totalCount,pageSize);
return this.rowSetPage;
} catch(SQLException sqle){
//System.out.println("executeQuery SQLException");
sqle.printStackTrace();
throw sqle;
} catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e.toString());
} finally{
//System.out.println("executeQuery finally");
DBUtil.close(rs, pst, conn);
}
}

/**
*将ResultSet数据填充进CachedRowSet
*/

protected abstract RowSet populate(ResultSet rs) throws SQLException;

/**
*取封装成RowSet查询结果
*@return RowSet
*/

public javax.sql.RowSet getRowSet(){
return this.rowSet;
}


/**
*取封装成RowSetPage的查询结果
*@return RowSetPage
*/

public RowSetPage getRowSetPage() {
return this.rowSetPage;
}



/**
*关闭数据库连接
*/

public void close(){
//因为数据库连接在查询结束或发生异常时即关闭,此处不做任何事情
//留待扩充。
}



private class BoundParam {
int index;
Object value;
int sqlType;
int scale;

public BoundParam( int index, Object value) {
this(index, value, java.sql.Types.OTHER);
}

public BoundParam( int index, Object value, int sqlType) {
this(index, value, sqlType, 0);
}

public BoundParam( int index, Object value, int sqlType, int scale) {
this.index = index;
this.value = value;
this.sqlType = sqlType;
this.scale = scale;
}

public boolean equals(Object obj){
if (obj!=null && this.getClass().isInstance(obj)){
BoundParam bp = (BoundParam)obj;
if ( this.index==bp.index) return true;
}
return false;
}
}

}


///
//
// PagedStatementOracleImpl.java
// author: evan_zhao@hotmail.com
//
///
package page;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.RowSet;
import oracle.jdbc.rowset.OracleCachedRowSet;

/**
*

Title: 分页查询Oracle数据库实现


*

Copyright: Copyright (c) 2002


* @author evan_zhao@hotmail.com
* @version 1.0
*/
public class PagedStatementOracleImpl extends PagedStatement {

/**
* 构造一查询出所有数据的PageStatement
* @param sql query sql
*/

public PagedStatementOracleImpl(String sql){
super(sql);
}


/**
* 构造一查询出当页数据的PageStatement
* @param sql query sql
* @param pageNo 页码
*/

public PagedStatementOracleImpl(String sql, int pageNo){
super(sql, pageNo);
}

/**
* 构造一查询出当页数据的PageStatement,并指定每页显示记录条数
* @param sql query sql
* @param pageNo 页码
* @param pageSize 每页容量
*/

public PagedStatementOracleImpl(String sql, int pageNo, int pageSize){
super(sql, pageNo, pageSize);
}


/**
*生成查询一页数据的sql语句
*@param sql 原查询语句
*@startIndex 开始记录位置
*@size 需要获取的记录数
*/

protected String intiQuerySQL(String sql, int startIndex, int size){
StringBuffer querySQL = new StringBuffer();
if (size != super.MAX_PAGE_SIZE) {
querySQL.append( "select * from (select my_table.*,rownum as my_rownum from(")
.append( sql)
.append( ") my_table where rownum).append(startIndex + size)
.append(") where my_rownum>=").append(startIndex);
} else {
querySQL.append("select * from (select my_table.*,rownum as my_rownum from(")
.append(sql)
.append(") my_table ")
.append(") where my_rownum>=").append(startIndex);
}
return querySQL.toString();
}

/**
*将ResultSet数据填充进CachedRowSet
*/

protected RowSet populate(ResultSet rs) throws SQLException{
OracleCachedRowSet ocrs = new OracleCachedRowSet();
ocrs.populate(rs);
return ocrs;
}

}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/374079/viewspace-131067/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/374079/viewspace-131067/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值