Database paging algorithm JAVA Implementation

Source: Internet
Author: User
// The paging method uses arraylist to save the result because if the resultset is used to save the result, the resultset requires that the database store the connection at any time. Once the database is disconnected, the RS data is gone. Public arraylist getfenye (INT pagenow) {arraylist Al = new arraylist (); // 1. connect to the database Ct = new conndb (). getconn (); // basic paging algorithm string SQL = "select * from (select T. *, rownum rn from "+" (select * from users order by userid) t "+" where rownum <= "+ pagesize * pagenow + ") where rn> = "+ (pagesize * (pageNow-1) + 1); try {PS = CT. preparestatement (SQL); RS = ps.exe cutequery (); // retrieve a record // each time a record is retrieved, it is put into a userbean while (RS. next () {userbean UB = new userbean (); ub. setuserid (RS. getint (1); ub. setusername (RS. getstring (2); ub. setpasswd (RS. getstring (3); ub. setemail (RS. getstring (4); ub. setgrade (RS. getint (5); // Add UB to arraylist Al. add (UB) ;}} catch (sqlexception e) {e. printstacktrace ();} finally {// close resource dbclose Dc = new dbclose (); DC. close () ;}return Al ;}

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.