分页技术(2)

来源:互联网 发布:网络推手公司哪个好 编辑:程序博客网 时间:2024/06/10 21:28
  1.     public int getStart(){
  2.         return start;
  3.     }
  4.     
  5.     public int getEnd(){
  6.         int end this.getStart() this.getSize() -1;
  7.         if (end<0) {
  8.             end 0;
  9.         }
  10.         return end;
  11.     }
  12.     
  13.     public int getStartOfPreviousPage() {
  14.         return Math.max(start-myPageSize, 1);
  15.     }
  16.     
  17.     public int getStartOfNextPage() {
  18.         return start avaCount;
  19.     }
  20.     
  21.     public static int getStartOfAnyPage(int pageNo){
  22.         return getStartOfAnyPage(pageNo, DEFAULT_PAGE_SIZE);
  23.     }
  24.     
  25.     public static int getStartOfAnyPage(int pageNo, int pageSize){
  26.         int startIndex (pageNo-1) pageSize 1;
  27.         if startIndex 1) startIndex 1;
  28.         //System.out.println("Page No to Start Index: pageNo "-" startIndex);
  29.         return startIndex;
  30.     }
  31.     
  32.     public int getSize() {
  33.         return avaCount;
  34.     }
  35.     
  36.     public int getTotalSize() {
  37.         return this.totalSize;
  38.     }
  39.     
  40.     public int getCurrentPageNo(){
  41.         return  this.currentPageno;
  42.     }
  43.     
  44.     public int getTotalPageCount(){
  45.         return this.totalPageCount;
  46.     }
  47.     
  48.     public String getHTML(String queryJSFunctionName, String pageNoParamName){
  49.         if (getTotalPageCount()<1){
  50.             return "<input type='hidden' name='"+pageNoParamName+"' value='1' >";
  51.         }
  52.         if (queryJSFunctionName == null || queryJSFunctionName.trim().length()<1) {
  53.             queryJSFunctionName "gotoPage";
  54.         }
  55.         if (pageNoParamName == null || pageNoParamName.trim().length()<1){
  56.             pageNoParamName "pageno";
  57.         }
  58.         String gotoPage "_"+queryJSFunctionName;
  59.         StringBuffer html new StringBuffer("\n");
  60.         html.append("<script language=\"Javascript1.2\">\n")
  61.              .append("function ").append(gotoPage).append("(pageNo){  \n")
  62.              .append(    var curPage=1;  \n")
  63.              .append(    try{ curPage document.all[\"")
  64.              .append(pageNoParamName).append("\"].value;  \n")
  65.              .append(         document.all[\"").append(pageNoParamName)
  66.              .append("\"].value pageNo;  \n")
  67.              .append(         ").append(queryJSFunctionName).append("(pageNo); \n")
  68.              .append(         return true;  \n")
  69.              .append(    }catch(e){ \n")
  70. //             .append(       try{ \n")
  71. //             .append(            document.forms[0].submit();  \n")
  72. //             .append(       }catch(e){   \n")
  73.              .append(           alert('尚未定义查询方法:function ")
  74.              .append(queryJSFunctionName).append("()'); \n")
  75.              .append(           document.all[\"").append(pageNoParamName)
  76.              .append("\"].value curPage;  \n")
  77.              .append(           return false;  \n")
  78. //             .append(        \n")
  79.              .append(     \n")
  80.              .append(  "}")
  81.              .append(  "</script>  \n")
  82.              .append(  "");
  83.         html.append( "<table  border=0 cellspacing=0 cellpadding=0 align=center width=80%>  \n")
  84.              .append(  <tr>  \n")
  85.              .append(    <td align=left><br>  \n");
  86.         html.append(        共" ).append( getTotalPageCount() ).append( "页")
  87.              .append(        [".append(getStart()).append("..").append(getEnd())
  88.              .append("/").append(this.getTotalSize()).append("]  \n")
  89.              .append(    </td>  \n")
  90.              .append(    <td align=right>  \n");
  91.         if (hasPreviousPage()){
  92.              html.append( "[<a href='javascript:").append(gotoPage)
  93.              .append("(".append(getCurrentPageNo()-1) 
  94.              .append( ")'>上一页</a>]   \n");
  95.         }
  96.         html.append(        第")
  97.              .append(          <select name='")
  98.              .append(pageNoParamName).append("' onChange='javascript:")
  99.              .append(gotoPage).append("(this.value)'>\n");
  100.         String selected "selected";
  101.         for(int i=1;i<=getTotalPageCount();i++){
  102.             if== getCurrentPageNo() )
  103.                  selected "selected";
  104.             else selected "";
  105.             html.append(      <option value='").append(i).append("' ")
  106.               .append(selected).append(">").append(i).append("</option>  \n");
  107.         }
  108.         if (getCurrentPageNo()>getTotalPageCount()){
  109.             html.append(      <option value='").append(getCurrentPageNo())
  110.             .append("' selected>").append(getCurrentPageNo())
  111.             .append("</option>  \n");
  112.         }
  113.         html.append(    </select>页  \n");
  114.         if (hasNextPage()){
  115.              html.append(    [<a href='javascript:").append(gotoPage)
  116.                .append("(").append((getCurrentPageNo()+1)) 
  117.                .append( ")'>下一页</a>]   \n");
  118.         }
  119.         html.append( "</td></tr></table>  \n");
  120.         return html.toString();
  121.     }
  122. }
  123. ///////////////////////////////////
  124. //
  125. //  RowSetPage.java
  126. //  author: evan_zhao@hotmail.com
  127. //
  128. ///////////////////////////////////
  129. package page;
  130. import javax.sql.RowSet;
  131. public class RowSetPage extends Page {
  132.     private javax.sql.RowSet rs;
  133.     
  134.     public static final RowSetPage EMPTY_PAGE new RowSetPage();
  135.     
  136.     public RowSetPage(){
  137.       this(null0,0);
  138.     }
  139.     
  140.     public RowSetPage(RowSet crs, int start, int totalSize) {
  141.         this(crs,start,totalSize,Page.DEFAULT_PAGE_SIZE);
  142.     }
  143.     
  144.     public RowSetPage(RowSet crs, int start, int totalSize, int pageSize) {
  145.         try{
  146.             int avaCount=0;
  147.             if (crs!=null{
  148.                 crs.beforeFirst();
  149.                 if (crs.next()){
  150.                     crs.last();
  151.                     avaCount crs.getRow();
  152.                 }
  153.                 crs.beforeFirst();
  154.             }
  155.             rs crs;
  156.             super.init(start,avaCount,totalSize,pageSize,rs);
  157.         }catch(java.sql.SQLException sqle){
  158.             throw new RuntimeException(sqle.toString());
  159.         }
  160.     }
  161.     
  162.     public javax.sql.RowSet getRowSet(){
  163.         return rs;
  164.     }
  165. }
  166. ///////////////////////////////////
  167. //
  168. //  PagedStatement.java
  169. //  author: evan_zhao@hotmail.com
  170. //
  171. ///////////////////////////////////
  172. package page;
  173. import foo.DBUtil;
  174. import java.math.BigDecimal;
  175. import java.util.List;
  176. import java.util.Iterator;
  177. import java.util.Collections;
  178. import java.sql.Connection;
  179. import java.sql.SQLException;
  180. import java.sql.ResultSet;
  181. import java.sql.Statement;
  182. import java.sql.PreparedStatement;
  183. import java.sql.Timestamp;
  184. import javax.sql.RowSet;
  185. public abstract class PagedStatement {
  186.     public final static int MAX_PAGE_SIZE Page.MAX_PAGE_SIZE;
  187.     protected String countSQL, querySQL;
  188.     protected int pageNo,pageSize,startIndex,totalCount;
  189.     protected javax.sql.RowSet rowSet;
  190.     protected RowSetPage rowSetPage;
  191.     private List boundParams;
  192.     /**
  193.      构造一查询出所有数据的PageStatement
  194.      @param sql