Display records by page using jsp

Source: Internet
Author: User

First RecordThe number of records in all records. If the first record on each page is the position record in the total record, the position = (ShowPage-1) × PageSize + 1. For example, if you want to display the first page, you need to calculate the first record in the first page as the first record in the total record; if you want to display the second page, the first record on the second page is the fourth record in the total record. to display the third page, the first record on the first page is the ninth record in the total record.

<%! Int pageSize = 4; int pageCount; int showPage; %> <! -- Connect to the database and retrieve records from the database --> <% Connection con; Statement SQL; ResultSet rs; try {Class. forName ("com. mysql. jdbc. driver ");} catch (ClassNotFoundException e) {} try {con = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/message board", "root", "123456"); SQL = con. createStatement (ResultSet. TYPE_SCROLL_SENSITIVE, ResultSet. CONCUR_READ_ONLY); // return the result set rsw. SQL .exe cuteQuery ("select * from messageinfo") that can be rolled; // move the cursor to the last rs row. l Ast (); // get the row number int recordCount = rs. getRow (); // calculate the total number of pageCount after pagination = (recordCount % pageSize = 0 )? (LastRow/pageSize) :( lastRow/pageSize + 1); // obtain the number of pages to be displayed by the user: String integer = request. getParameter ("showPage"); if (integer = null) {integer = "1" ;}try {showPage = Integer. parseInt (integer);} catch (NumberFormatException e) {showPage = 1;} if (showPage <= 1) {showPage = 1;} if (showPage> = pageCount) {showPage = pageCount;} // to display the showPage page, the cursor should move to the position value: int position = (showPage-1) * pageSize + 1; // set the cursor location rs. absolute (position); // use fo R loop shows the records that should be displayed on this page for (int I = 1; I <= pageSize; I ++) {%> <table> <tr> <th> <% = rs. getString ("UserName") %> </th> <td> published at: <% = rs. getString ("datetime") %> </td> </tr> <th colspan = "3"> <textarea> <% = rs. getString ("content") %> </textarea> </th> </tr> </table> <% rs. next ();} rs. close (); con. close ();} catch (Exception e) {e. printStackTrace () ;}%> <br> page <% = showPage %> (total <% = pageCount %> pages) <br> <a href = "ShowMessages. jsp? ShowPage = 1 "> homepage </a> <a href =" ShowMessages. jsp? ShowPage = <% = showPage-1 %> "> previous page </a> <% // display the number of each page based on the pageCount value and attach the corresponding hyperlink for (int I = 1; I <= pageCount; I ++) {%> <a href = "ShowMessages. jsp? ShowPage = <% = I %> "> <% = I %> </a> <% }%> <a href =" ShowMessages. jsp? ShowPage = <% = showPage + 1%> "> next page </a> <a href =" ShowMessages. jsp? ShowPage = <% = pageCount %> "> last page </a> <! -- Submit the page number to be displayed through the form --> <form action = "" method = "get"> jump to the <input type = "text" name = "showPage" size =" 4 "> page <input type =" submit "name =" submit "value =" jump "> </form>


There are already too many other

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.