Java's implementation of paging

Source: Internet
Author: User

about Java implementation paging

Transferred from: http://www.cnblogs.com/slliang/archive/2012/08/22/2651053.html

1. Paging tool class, encapsulating paging information

Package Com.student.util;import java.util.list;import com.student.entity.person;/** * Packaging Paging information * @author Administrator * *        @param <Person> */public class Pagemodel<e> {//result set private list<e> List;        Query record number private int totalrecords;        The first few pages of private int pageno;        How many records per page private int pageSize;    Total pages public int gettotalpages () {return (totalrecords + pageSize-1)/pagesize;    }//Home public int gettoppage () {return 1;        }//prev public int getpreviouspage () {if (pageno<=1) {return 1;    } return pageNo-1;        }//Next public int getnextpage () {if (Pageno>=getbottompage ()) {return getbottompage ();    } return pageno+1;    }//Last public int getbottompage () {return gettotalpages ();    } public list<e> GetList () {return List;  } public void setlist (list<e> list) {this.list = list;  } public int Gettotalrecords () {return totalrecords;    } public void Settotalrecords (int totalrecords) {this.totalrecords = totalrecords;    } public int Getpageno () {return pageno;    } public void Setpageno (int pageno) {This.pageno = PageNo;    } public int getpagesize () {return pageSize;    } public void SetPageSize (int pageSize) {this.pagesize = pageSize; }}

  2. Test class

 Public classpagemodeltest{ Public Static voidMain (string[] args) {intPageno=1; intpagesize=10;    Finduserlist (pageno,pagesize); }    /*** Paging Query *@paramPageNo Page *@parampageSize number of records per page *@returnpagemodel<e>*/     PublicPagemodel<person> Finduserlist (intPageNo,intpageSize) {Pagemodel<Person> Pagemodel =NULL; Connection Conn=NULL; PreparedStatement PS=NULL; ResultSet RS=NULL; //the paging statements for each database are not the same                /*Oracle Implementation paging, three-tier nesting, where 10 should be pageno*pagesize, 0 is (pageNo-1) *pagesize String sql= "Select Column1,column2,column 3,column4,column5 from (select RowNum rn,column1,column2,column3,column4,column5 from (sel         ECT COLUMN1,COLUMN2,COLUMN3,COLUMN4,COLUMN5 from table_name ORDER BY column DESC) where rownum<=10)        where rn>0 "; */        //MySQL Implementation pagingString sql= "SELECT * from the person ORDER by id DESC limit?,? "; Conn=dbutil.getutil (). getconnection (); Try{PS=conn.preparestatement (SQL); Ps.setint (1, (pageNo-1) *pageSize); Ps.setint (2, pageSize); RS=Ps.executequery (); List<Person> personlist =NewArraylist<person>();  while(Rs.next ()) { person person=NewPerson (); Person.setname (Rs.getstring ("Stu_name")); Person.setpassword (Rs.getstring ("STU_PSW")); Person.setnumber (Rs.getstring ("Stu_number")); Person.setbirthday (Rs.getdate ("Stu_birth")); Person.setsex (Rs.getint ("Stu_sex")); Person.setpolity (Rs.getint ("Stu_polity")); Person.setbrief (Rs.getstring ("Stu_brief")); Person.settype (Rs.getint ("Type")); Person.setstate (Rs.getint ("State"));            Personlist.add (person); } Pagemodel=NewPagemodel<person>();            Pagemodel.setlist (personlist);            Pagemodel.settotalrecords (Gettotalrecords (conn));            Pagemodel.setpagesize (pageSize);        Pagemodel.setpageno (PageNo); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{            Try {                if(rs!=NULL) {rs.close (); }                if(ps!=NULL) {ps.close (); }                if(conn!=NULL) {conn.close (); }            } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        }        returnPagemodel; }    /*** Get total record count, private method, external inaccessible, use in this class *@paramConn *@return     */    Private intGettotalrecords (Connection conn) {PreparedStatement PS=NULL; ResultSet RS=NULL; String SQL= "SELECT count (*) from person"; Conn=dbutil.getutil (). getconnection (); intCount=0; Try{PS=conn.preparestatement (SQL); RS=Ps.executequery ();  while(Rs.next ()) {//In this case, only one column is isolated from the SQL statement, otherwise it is not recommended to identify the field with intCount = Rs.getint (1); }        } Catch(SQLException e) {e.printstacktrace (); }finally{            Try {                if(rs!=NULL) {rs.close (); }                if(ps!=NULL) {ps.close (); }            } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        }        returncount; }}

Java's implementation of paging

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.