Java technology accumulation--using the most original code witness paging query Implementation principle

Source: Internet
Author: User

About paging query, everyone is not unfamiliar, Easyui in the package of the DataGrid will be the table of a series of operations to use the most thoroughly, paged query is one of them. However, we are not aware of the basic implementation of the paging query principle, how it is implemented, today we will take you together to explore the exactly!

Look at the implementation of the interface:

Seemingly complex things, in fact, the most important thing to achieve its core is two:

first, the implementation of paged query SQL statement

No matter what the bottom of the things do not need to do, so the most primitive SQL statement we should still understand.

Second, the realization of paged query entity

Package Com.bjpowernode.drp.util;import Java.util.list;public class Pagemodel<e> {private list<e> list;// The specific type of table to which the page appears, using generics to implement the private int totalrecords; Total record number private int pageSize; The number of bars displayed per page private int pageno; Page public list<e> getList () {return List;} public void setlist (list<e> list) {this.list = list;} /** * Total pages * */public int gettotalpages () {return (totalrecords+pagesize-1)/pagesize;} /*** get home */public int Gettoppageno () {return 1;} /** * Get last * @return */public int Getbottompageno () {return gettotalpages ();} /** * prev * @return */public int Getpreviouspageno () {if (pageno<=1) {return 1;} return pageNo-1;} public int Getnextpageno () {if (Pageno>=getbottompageno ()) {return Getbottompageno ();} return pageno+1;} public void settotalrecords (int totalrecords) {this.totalrecords = totalrecords;} public int gettotalpages (int totalrecords) {return totalrecords;} public int getpagesize () {return pageSize;} public void setpagesize (int pageSize) {this.pagesize = pageSize;} Public INT Getpageno () {return pageno;} public void Setpageno (int pageno) {This.pageno = PageNo;}}

The above has already put the core of the paged query is finished, the next only we need to put our business logic and our above two cores together into a line, so that the function of paging query can be achieved!

Iii. integration with business logic business logic implementation:

/** * Paged Query * @param pageno page * @param pageSize how many data per page * @return Pagemodel */public pagemodel<user> finduserlist (i NT Pageno,int pageSize) {stringbuffer sbsql=new stringbuffer (); Sbsql.append ("Select User_id,user_name,password, Contact_tel,email,create_date "). Append (" from "). Append (" ("). Append (" Select RowNum Rn,user_id,user_name,password, Contact_tel,email,create_date "). Append (" from "). Append (" ("). Append (" Select User_id,user_name,password,contact_ Tel,email,create_date from T_user where user_id <> ' root ' order by user_id '). Append (") where RowNum <=?"). Append (") where rn>?"); Pagemodel<user> Pagemodel=null; Connection Conn=null; PreparedStatement Pstmt=null; ResultSet Rs=null; User user=null;try{conn=dbutil.getconnection ();p stmt = Conn.preparestatement (sbsql.tostring ());p Stmt.setint (1, PageNo * pageSize);p Stmt.setint (2, (pageNo-1) * pageSize);//pstmt.setstring (1,userid); Rs=pstmt.executequery (); List<user> userlist=new arraylist<user> (); while (Rs.next ()) {User=new User (); User.setuserid (rs.getstring ("user_id")); User.setusername (rs.getstring ("user_name")); User.setpassword (rs.getstring ("password")); User.setcontacttel (rs.getstring ("Contact_tel")); User.setemail ( Rs.getstring ("email")); User.setcreatedate (Rs.gettimestamp ("create_date")); Userlist.add (user); Pagemodel=new Pagemodel ();p agemodel.setlist (userlist);p agemodel.settotalrecords (Gettotalrecords (conn)); Pagemodel.setpagesize (pageSize);p Agemodel.setpageno (PageNo);} catch (SQLException e) {e.printstacktrace ();} finally{//is closed in order Dbutil.close (RS);D butil.close (pstmt);D butil.close (conn);} return Pagemodel;} private int gettotalrecords (Connection conn) throws sqlexception{string sql= "SELECT COUNT (*) from T_user where user_id &lt ;> ' root '; PreparedStatement Pstmt=null; ResultSet rs=null;int count=0;try{pstmt=conn.preparestatement (SQL); Rs=pstmt.executequery (); Rs.next (); count= Rs.getint (1);} Finally{dbutil.close (RS);D butil.close (pstmt);} return count;}


Front-end invocation:

<%int Pageno=1;int pagesize=3; String pagenostring=request.getparameter ("PageNo"), if (pagenostring!=null) {pageno=integer.parseint (pageNoString) ;} Pagemodel<user> pagemodel=usermanager.getinstance (). Finduserlist (PageNo, pageSize); %>

at this point, the paging query basically has been implemented, through this study, let me once again understand the basic implementation of paging query principle, and then use the Easyui package of the DataGrid when no longer confused Ah!


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java technology accumulation--using the most original code witness paging query Implementation principle

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.