分页技术(2)
来源:互联网 发布:网络推手公司哪个好 编辑:程序博客网 时间:2024/06/10 21:28
- 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;
- //System.out.println("Page No to Start Index: " + pageNo + "-" + startIndex);
- 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 "<input type='hidden' name='"+pageNoParamName+"' value='1' >";
- }
- 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("<script language=\"Javascript1.2\">\n")
- .append("function ").append(gotoPage).append("(pageNo){ \n")
- .append( " var curPage=1; \n")
- .append( " try{ curPage = document.all[\"")
- .append(pageNoParamName).append("\"].value; \n")
- .append( " document.all[\"").append(pageNoParamName)
- .append("\"].value = pageNo; \n")
- .append( " ").append(queryJSFunctionName).append("(pageNo); \n")
- .append( " return true; \n")
- .append( " }catch(e){ \n")
- // .append( " try{ \n")
- // .append( " document.forms[0].submit(); \n")
- // .append( " }catch(e){ \n")
- .append( " alert('尚未定义查询方法:function ")
- .append(queryJSFunctionName).append("()'); \n")
- .append( " document.all[\"").append(pageNoParamName)
- .append("\"].value = curPage; \n")
- .append( " return false; \n")
- // .append( " } \n")
- .append( " } \n")
- .append( "}")
- .append( "</script> \n")
- .append( "");
- html.append( "<table border=0 cellspacing=0 cellpadding=0 align=center width=80%> \n")
- .append( " <tr> \n")
- .append( " <td align=left><br> \n");
- html.append( " 共" ).append( getTotalPageCount() ).append( "页")
- .append( " [") .append(getStart()).append("..").append(getEnd())
- .append("/").append(this.getTotalSize()).append("] \n")
- .append( " </td> \n")
- .append( " <td align=right> \n");
- if (hasPreviousPage()){
- html.append( "[<a href='javascript:").append(gotoPage)
- .append("(") .append(getCurrentPageNo()-1)
- .append( ")'>上一页</a>] \n");
- }
- html.append( " 第")
- .append( " <select name='")
- .append(pageNoParamName).append("' onChange='javascript:")
- .append(gotoPage).append("(this.value)'>\n");
- String selected = "selected";
- for(int i=1;i<=getTotalPageCount();i++){
- if( i == getCurrentPageNo() )
- selected = "selected";
- else selected = "";
- html.append( " <option value='").append(i).append("' ")
- .append(selected).append(">").append(i).append("</option> \n");
- }
- if (getCurrentPageNo()>getTotalPageCount()){
- html.append( " <option value='").append(getCurrentPageNo())
- .append("' selected>").append(getCurrentPageNo())
- .append("</option> \n");
- }
- html.append( " </select>页 \n");
- if (hasNextPage()){
- html.append( " [<a href='javascript:").append(gotoPage)
- .append("(").append((getCurrentPageNo()+1))
- .append( ")'>下一页</a>] \n");
- }
- html.append( "</td></tr></table> \n");
- return html.toString();
- }
- }
- ///////////////////////////////////
- //
- // RowSetPage.java
- // author: evan_zhao@hotmail.com
- //
- ///////////////////////////////////
- package page;
- import javax.sql.RowSet;
- public class RowSetPage extends Page {
- private javax.sql.RowSet rs;
-
- public static final RowSetPage EMPTY_PAGE = new RowSetPage();
-
- public RowSetPage(){
- this(null, 0,0);
- }
-
- public RowSetPage(RowSet crs, int start, int totalSize) {
- this(crs,start,totalSize,Page.DEFAULT_PAGE_SIZE);
- }
-
- 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;
- 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