ID | 代碼 | 用戶名 | 姓名 |
<%= empRS.getString("EMP_ID")%> | <%= empRS.getString("EMP_CODE")%> | <%= empRS.getString("USER_NAME")%> | <%= empRS.getString("REAL_NAME")%> |
<%= empPage .getHTML("doQuery", "pageno")%> |
* Description: 用於包含數據及分頁信息的對象
* Page類實現了用於顯示分頁信息的基本方法,但未指定所含數據的類型, * 可根據需要實現以特定方式組織數據的子類,
* 如RowSetPage以RowSet封裝數據,ListPage以List封裝數據
* Copyright: Copyright (c) 2002
* @author [email protected]
* @version 1.0 */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()); } /** * 分頁數據初始方法,由子類調用 * @param start 本頁數據在數據庫中的起始位置 * @param avaCount 本頁包含的數據條數 * @param totalSize 數據庫中總記錄條數 * @param pageSize 本頁容量 * @param data 本頁包含的數據 */ 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; //System.out.println("avaCount:"+avaCount); //System.out.println("totalSize:"+totalSize); if (avaCount>totalSize) { //throw new RuntimeException("記錄條數大於總條數?!"); } this.currentPageno = (start -1)/pageSize +1; this.totalPageCount = (totalSize + pageSize -1) / pageSize; if (totalSize==0 && avaCount==0){ this.currentPageno = 1; this.totalPageCount = 1; } //System.out.println("Start Index to Page No: " + start + "-" + currentPageno); } public Object getData(){ return this.data; } /** * 取本頁數據容量(本頁能包含的記錄數) * @return 本頁能包含的記錄數 */ public int getPageSize(){ return this.myPageSize; } /** * 是否有下一頁 * @return 是否有下一頁 */ public boolean hasNextPage() { /* if (avaCount==0 && totalSize==0){ return false; } return (start + avaCount -1) < totalSize; */ return (this.getCurrentPageNo() 1; */ return (this.getCurrentPageNo()>1); } /** * 獲取當前頁第一條數據在數據庫中的位置 * @return */ public int getStart(){ return start; } /** * 獲取當前頁最後一條數據在數據庫中的位置 * @return */ public int getEnd(){ int end = this.getStart() + this.getSize() -1; if (end<0) { end = 0; } return end; } /** * 獲取上一頁第一條數據在數據庫中的位置 * @return 記錄對應的rownum */ public int getStartOfPreviousPage() { return Math.max(start-myPageSize, 1); } /** * 獲取下一頁第一條數據在數據庫中的位置 * @return 記錄對應的rownum */ public int getStartOfNextPage() { return start + avaCount; } /** * 獲取任一頁第一條數據在數據庫中的位置,每頁條數使用默認值 * @param pageNo 頁號 * @return 記錄對應的rownum */ public static int getStartOfAnyPage(int pageNo){ return getStartOfAnyPage(pageNo, DEFAULT_PAGE_SIZE); } /** * 獲取任一頁第一條數據在數據庫中的位置 * @param pageNo 頁號 * @param pageSize 每頁包含的記錄數 * @return 記錄對應的rownum */ public static int getStartOfAnyPage(int pageNo, int pageSize){ int startIndex = (pageNo-1) * pageSize + 1; if ( startIndex < 1) startIndex = 1; //System.out.println("Page No to Start Index: " + pageNo + "-" + startIndex); return startIndex; } /** * 取本頁包含的記錄數 * @return 本頁包含的記錄數 */ public int getSize() { return avaCount; } /** * 取數據庫中包含的總記錄數 * @return 數據庫中包含的總記錄數 */ public int getTotalSize() { return this.totalSize; } /** * 取當前頁碼 * @return 當前頁碼 */ public int getCurrentPageNo(){ return this.currentPageno; } /** * 取總頁碼 * @return 總頁碼 */ public int getTotalPageCount(){ return this.totalPageCount; } /** * * @param queryJSFunctionName 實現分頁的JS腳本名字,頁碼變動時會自動回調該方法 * @param pageNoParamName 頁碼參數名稱 * @return */ public String getHTML(String queryJSFunctionName, String pageNoParamName){ if (getTotalPageCount()<1){ return ""; } if (queryJSFunctionName == null || queryJSFunctionName.trim().length()<1) { queryJSFunctionName = "gotoPage"; } if (pageNoParamName == null || pageNoParamName.trim().length()<1){ pageNoParamName = "pageno"; } String gotoPage = "_"+queryJSFunctionName; StringBuffer html = new StringBuffer("/n"); html.append(" /n") .append( ""); html.append( "
/n"); html.append( " 共" ).append( getTotalPageCount() ).append( "頁") .append( " [") .append(getStart()).append("..").append(getEnd()) .append("/").append(this.getTotalSize()).append("] /n") .append( " | /n"); if (hasPreviousPage()){ html.append( "[上一頁] /n"); } html.append( " 第") .append( " 頁 /n"); if (hasNextPage()){ html.append( " [下一頁] /n"); } html.append( " |
Title: RowSetPage
*Description: 使用RowSet封裝數據的分頁對象
*Copyright: Copyright (c) 2003
* @author [email protected] * @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: [email protected]/////////////////////////////////////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 [email protected] * @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: [email protected]/////////////////////////////////////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 [email protected] * @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; }}