I finally understand the JSP pagination

Source: Internet
Author: User
Tags count integer query microsoft frontpage
js| Paging
When we query the database, if the query out of the result is a lot of, such as 1000, then, if all of the display to a page is simply intolerable things! So we want to page out, each page shows 10 or 20 rows, the purpose of paging is so simple.







There are many ways to page pagination, perhaps some efficiency is higher, I can only refer to the simplest way to achieve: Now query out how many records, and then calculate a total of several pages, and then display each page.



How many records are in the query:



Select COUNT (*) from learning







Calculate how many pages to divide:



if (counts%pagesizes==0) {pagecounts=counts/pagesizes;}



else{pagecounts=counts/pagesizes+1;}



int i= (intPage-1) *pagesizes; (The algorithm in the book is very stupid, with my own thinking, no wrong)



The above is to determine whether the last page is full, such as 93 records, each page shows 5, then the last page to display 3, a total of 19 pages show! When I started thinking about 100 pages, haha, very special, not representative, it seems that the way of thinking is very important, retarded?







And then the pages are displayed separately,



String query= "Select *" +



"From learning" +



"Where article_id like '% '";



Here we are in trouble, how to show? First, find out where 5 is. What kind of data structure? or the number of records that divide the database? Maybe there are at least 2 ways we can consider pagination here: here we use the latter, which is: the traversal of RS and the combination of the number of records per page:







int i= (intPage-1) *pagesizes;//determines how many records each page can traverse to



Ps=conn.preparestatement (query);



ResultSet Rs1=ps.executequery ();



for (int j=0; j<i; j + +)



Rs1.next ()///When it is the first page, RS starts with the No. 0, and if it is the second page, begin the traversal from the (2-1) * 5 tuple, if page 3rd, will begin traversing from the 10th section



Finally: while (Rs1.next () &&i<pagesizes) {i++;} The key is to control the number of bars displayed on each page, and control the scope of the calendar.



Finally, there is a question: ResultSet rs1 Why not the original RS? If you do not understand this, you can say that you do not understand this paging practice, because the RS in the query total number of times has pointed to the first, if you continue to use, will be from the query out of the second record to start showing! The problem is that I just debug the page when the bug appeared, it seems to be the first problem, then analyze the reasons, and then really understand the cause of the process.







The following also shows the content of the pagination plus a from, which provides links to the current number of pages id+1 (minus 1) to display the previous page or the next page of content!







Just understand the paging, immediately write down, deepen the impression, more dare not to enjoy the joy of understanding after!



The following is the code for JSP paging:












*****************************



<%cookie cookies[]=request.getcookies (); if (cookies==null) {response.sendredirect ("login.jsp"); %><%@ include file= "articleconn.jsp"%>





<%request.setcharacterencoding ("Big5"); String query= "Select *" + "learning" + "where article_id like '% '"; String querycount= "SELECT COUNT (*) from learning"; String strpage=request.getparameter ("strpage"); int intpage;if (strpage==null) {intpage=1;} Elseintpage=integer.parseint (strpage);



int Pagesizes=5;int Counts=1;int Pagecounts=1;



Try{preparedstatement ps=conn.preparestatement (Querycount); ResultSet rs=ps.executequery (); if (Rs.next ()) {Counts=integer.parseint (rs.getstring (1));}







%><body bgcolor= "#FFFFFF" >  <p> </p>  <table width= "1" border= " cellspacing= "0" cellpadding= "0" align= "center" bordercolorlight= "#000000" bordercolordark= "#FFFFFF" >     <tr bgcolor= "#99CCFF" >      <td height= "bgcolor=" "#B5D85E" >         <div align= "center" >           <b> Management Interface </b>        </div>       </td>    </tr>    <tr>      &LT;TD height= "an" >        <%--page--%>         <p align= "left" >          <a href= "add.jsp" > Add info </a>           <a href= "index.jsp" > Back to homepage &LT;/A&Gt;        </p>        <table Border= "1" cellspacing= "0" width= "90%" bgcolor= "#F0F8FF" bordercolorlight= "#000000" bordercolordark= "#FFFFFF" align= "Center" >          <tr>             <td width= "12%" align= "center" >               <strong>id </strong>             </td>            &LT;TD width= "13%" align= "center" >               <b> Types </b>            </td>             <td width= "47%" align= "center" >               <strong> Information Name </strong>             </td>             <td width= "14%" align= "center" >               <strong> Modification </strong>             </td>            <td width= "14 % "align=" center ">              <strong > Erase </strong>            </td>           </tr>          <% if (counts%pagesizes==0) {pagecounts=counts/pagesizes;} else{pagecounts=counts/pagesizes+1;} int i= (intPage-1) *pagesizes;



Ps=conn.preparestatement (query); ResultSet Rs1=ps.executequery () for (int  j=0; j<i; j + +) Rs1.next (); Out.println (rs+ "<BR><BR>< Br> "); Out.println (Rs.next ());  i=0;while (Rs1.next () &&i<pagesizes) {%>           <tr>             &LT;TD width= "12%" height= "7" >               <p align= "center" >                 <%= rs1.getstring ("article_id")%>               </p>            </td>             <td width= "13%" height= "7" >               <div align= "center ">                <%= Rs1.getstring ("Types")%>              </ div>            </td>             <td width= "47%" height= "7" >               <%= rs1.getstring ("title")%>             </td>             &LT;TD width= "14%" align= "center" height= "7" >               <a href= "edit.jsp?id=<%=rs1.getstring (" article_id ")%>" > Modify </a>             </td>             <td width= "14%" align= "center" height= "7" >               <a href= "delete.jsp?id=<%=rs1.getstring (" article_id ")%>" > Erase </a>             </td>           </tr>          <% i++;} %>        </table>        <%



}catch (SQLException se1) {se1.printstacktrace ();} finally{}



Stmt.close (); Conn.close (); %> </td> </tr> </table><form action= "manage.jsp" method= "POST" > section <%=intPage%> pages, total <%=pageCounts%> pages <%if (intpage<pagecounts)%><a href= "Manage.jsp?strpage=<%=intpage+1%>" > next page </a><%if (intpage>1)%><a href= "manage.jsp?strpage=<%=intpage-1%>" > previous page </a >



</form>



<P> </P></body>






Related Article

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.