Servlet implements paging technology, and servlet implements Paging

Source: Internet
Author: User

Servlet implements paging technology, and servlet implements Paging

Key code:

1 // ===================================== paging functions ======== ===================================== 2 int pageSize = 3; 3 int pageNow = 1; 4 int rowCount = 0; 5 int pageCount = 0; 6 String sPageNow = request. getParameter ("pageNow"); 7 if (sPageNow! = Null) {8 pageNow = Integer. parseInt (sPageNow); 9} 10 Connection con = null; 11 java. SQL. preparedStatement ps = null; 12 ResultSet rs = null; 13 try {14 // load driver 15 Class. forName ("com. mysql. jdbc. driver "); 16 17 // create a database url18 String url =" jdbc: mysql: // localhost: 3307/spdb? UseSSL = false "; 19 String user =" root "; 20 String password =" root "; 21 22 // set up connection 23 con = DriverManager. getConnection (url, user, password); 24 // query and return the result 25 ps = con. prepareStatement ("select count (*) from users"); 26 rs = ps.exe cuteQuery (); 27 // calculates rowCount28 if (rs. next () {29 rowCount = rs. getInt (1); 30} 31 // calculate pageCount32 if (rowCount % pageSize = 0) {33 pageCount = rowCount/pageSize; 34} else {35 pageCount = row Count/pageSize + 1; 36} 37 // query and return result 38 ps = con. prepareStatement ("select * from users limit" + (pageSize * (pageNow-1) + "," + pageSize); 39 rs = ps.exe cuteQuery (); 40 // The returned data is displayed in a table. println ("<table border = 1>"); 42 out. println ("<tr> <th> id </th> <th> name </th> <th> passwd </th> <th> mail </th> <th>> grade </th> </tr> "); 43 while (rs. next () {44 out. println ("<tr>"); 45 out. println ("<td>" + rs. getInt (1) + "</td>"); 46 out. pri Ntln ("<td>" + rs. getString (2) + "</td>"); 47 out. println ("<td>" + rs. getString (3) + "</td>"); 48 out. println ("<td>" + rs. getString (4) + "</td>"); 49 out. println ("<td>" + rs. getInt (5) + "</td>"); 50 out. println ("</tr>"); 51} 52 out. println ("</table>"); 53 // page number hyperlink at the bottom 54 if (pageNow! = 1) {55 out. println ("<a href = MainFrame? PageNow = "+ (pageNow-1) +"> previous page </a> "); 56} 57 for (int I = pageNow; I <= pageNow + pageSize; I ++) {58 if (I <= pageCount) {59 out. println ("<a href = MainFrame? PageNow = "+ I +"> "+ I +" </a> "); 60} 61} 62 if (pageNow! = PageCount) {63 out. println ("<a href = MainFrame? PageNow = "+ (pageNow + 1) +"> next page </a> "); 64}

Effect:

 

Note:

The LIMIT clause can be used to LIMIT the number of rows returned by the SELECT statement.

LIMIT takes one or two numeric independent variables. The independent variables must be non-negative integer constants (except when prepared statements are used ). When two independent variables are used, the first independent variable specifies the offset of the first row returned, and the second independent variable specifies the maximum number of returned rows. The offset of the initial row is 0 (not 1 ). When an independent variable is used, this value specifies the number of rows returned from the beginning of the result set.

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.