Paging
Abstract class
==========================================================
Import java.util.ArrayList;
Import java.sql.Connection;
Import Java.sql.ResultSet;
Import Com.xxx.util.DBTool;
/**
* <pre>
* Pagination class. Default page size is 20
* This is an abstract class. Subclass requires refactoring method Selresult ()
* </pre>
*/
Public abstract class PageList {
/** the following member variables in subclasses
protected final static int min_page_size = 20; Default page size
protected int pageSize; Page size
protected int curpage; Current Page page number
protected int prepage; Previous page number
protected int nxtpage; Next Page page number
protected int totalpage; Total number of pages
protected int Totalitem; Total number of entries
protected String SQL; Selection criteria
protected ArrayList result; Result set
protected int from; Cursor position at start
/** Private Variable * *
Private Boolean hasfindresult = false; Whether the flag has found the result
Ensure Loadresult only once
/**
* Builder. Default page size is 20
* @param sql
* @param pageno
*/
Public pagelist (String sql, int pageno) {
init (SQL, min_page_size, pageno);
}
/**
* Builder
* @param SQL Condition SQL
* @param pageSize Page size
* @param pageno page number
*/
Public pagelist (String sql, int pageSize, int pageno) {
init (SQL, pageSize, pageno);
}
protected void init (String sql, int pageSize, int pageno) {
This.sql = SQL;
This.pagesize = (pagesize<=0? Min_page_size:pagesize);
This.curpage = (Pageno<=0?1:pageno);
}
/**
* Get page size
*/
public int getpagesize () {
if (!this.hasfindresult) {
This.loadresult ();
}
return this.pagesize;
}
/**
* Get current page number
*/
public int getcurpage () {
if (!this.hasfindresult) {
This.loadresult ();
}
return this.curpage;
}
/**
* Gets the page number of the previous page
*/
public int getprepage () {
if (!this.hasfindresult) {
This.loadresult ();
}
return this.prepage;
}
/**
* Get the page number of the next page
*/
public int getnxtpage () {
if (!this.hasfindresult) {
This.loadresult ();
}
return this.nxtpage;
}
/**
* Get the total number of page numbers
*/
public int gettotalpage () {
if (!this.hasfindresult) {
This.loadresult ();
}
return this.totalpage;
}
/**
* Get the total number of bars
*/
public int Gettotalitem () {
if (!this.hasfindresult) {
This.loadresult ();
}
return this.totalitem;
}
/**
* Determine if there is a page
* @return returns False if the total number of pages is 0; otherwise true
*/
public Boolean haspages () {
if (!this.hasfindresult) {
This.loadresult ();
}
Return (this.totalpage!= 0);
}
/**
* Get the result set of the specified page
*
* @return Specify the page result set;
*/
Public ArrayList GetResult () {
if (!this.hasfindresult) {
This.loadresult ();
}
return this.result;
}
/**
* Selected Results
*/
Protected abstract ArrayList selresult ();
/**
* Calculate page number information and get the result set.
* is a combination of calculatepagenoinfo and selresult.
*/
private void Loadresult () {
1. Calculate page number Related information
This.calculatepagenoinfo ();
2. Election of results
This.result = This.selresult ();
if (This.result = = null) {
This.result = new ArrayList ();
}
3. Ensure Loadresult () is only once
Hasfindresult = true;
}
/**
* Calculate page number related information calculate
*/
private void Calculatepagenoinfo () {
1. Get total number of entries
This.totalitem = Totalitem ();
2. Calculate page number Information
2.1 Total Pages
if (This.totalitem = = 0) {
this.totalpage = 0;
} else {
This.totalpage = (this.totalitem-1)/this.pagesize + 1;
}
2.2 Current page, previous page, back page
if (This.totalpage = = 0) {
this.curpage = 0;
this.prepage = 0;
this.nxtpage = 0;
This.from = 0;
} else {
This.curpage = (this.curpage>this.totalpage?this.totalpage:this.curpage);
This.prepage = ((this.curpage-1<1) 1: (this.curpage-1));
This.nxtpage = ((this.curpage==this.totalpage)? This.curpage: (this.curpage+1));
This.from = (this.curpage-1) *this.pagesize;
}
}
/**
* Get total number of entries
*/
private int Totalitem () {
Access db
int count = 0;
Dbtooldb = new Dbtool ();
ResultSet rs = null;
Db.conndb ();
rs = Db.advquery (this.sql);
try {
while (Rs.next ()) {
count++;
}
catch (Exception e) {
}
Db.closedb ();
return count;
}
}
//~
Use
===========================================================
Suppose I have a news entity object, now I want to construct a news-related paging class
As follows:
Import java.sql.Connection;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import Com.xxx.util.DBTool;
Import com.bokesoft.util.PageList;
Import Com.bokesoft.util.DateUtil;
/**
* News Page class
*/
public class Newspagelist extends PageList {
Public newspagelist (String sql, int pageno) {
Super (SQL, PageNo);
}
Public newspagelist (String sql, int pageSize, int pageno) {
Super (SQL, pageSize, pageno);
}
Protected Java.util.ArrayList Selresult () {
Java.util.ArrayList result = new Java.util.ArrayList (this.pagesize);
if (This.totalitem = = 0) {
return result;
}
News = null;
Dbtool db = new Dbtool ();
ResultSet rs = null;
Db.conndb ();
rs = Db.advquery (this.sql);
if (Rs!= null) {
try {//end loop Once an exception is caught
int count = 1;
if (This.curpage > 1) {
Rs.absolute (This.from); Jump to start line
}
while (Rs.next () && count<=pagesize) {
Loop constructs the news object and then joins the list
News = new News ();
News.setid (rs.getstring ("code") ==null "": Rs.getstring ("code"));
......
Result.add (news);
count++;
}
catch (Exception e) {
finally {
try {if (rs!= null) Rs.close ();} catch (SQLException Sqle) {}
}
}
Db.closedb ();
return result;
}
}
//~
Use of client programs (in JSP)
========================================================
<%
Get page numbers and display entries for jumps and selection criteria
//...
int Ipageno
int Ipagesize
String Sqlcond
Here is the specific use of the section of the paging class, very simple
Newspagelist NPL = new Newspagelist (Sqlcond, Ipagesize, Ipageno);
Java.util.ArrayList resultlist = Npl.getresult ();
int totalpage = Npl.gettotalpage ();
News = null;
int size = Resultlist.size ();
for (int i=0; i<size; i++) {
News = (News) resultlist.get (i);
//.....
}
%>