Paging Technology for JSP Database Query

Source: Internet
Author: User

Package bean;

Public class page {
Private int totalpage; // the total number of pages.
Private int currentpage; // the current page number.
Private int totalrecord; // The total number of records
Private int currentrecord; // number of current records
Private int pagesize = 1; // number of records displayed per page
 
Public int getcurrentpage (){
Return currentpage;
}
 
Public void setcurrentpage (INT pcurrentrecord, int ppagesize ){
If (currentrecord % pagesize = 0)
Currentpage = currentrecord/pagesize;
Else
Currentpage = currentrecord/pagesize + 1;
}
 
Public int getcurrentrecord (){
Return currentrecord;
}
 
Public void setcurrentrecord (INT pcurrentrecord ){
Currentrecord = pcurrentrecord;
}
 
Public int getpagesize (){
Return pagesize;
}
 
Public void setpagesize (INT ppagesize ){
Pagesize = ppagesize;
}
 
Public int gettotalpage (){
Return totalpage;
}
 
Public void settotalpage (INT ptotalrecord, int ppagesize ){
If (ptotalrecord % ppagesize = 0)
Totalpage = ptotalrecord/ppagesize;
Else
Totalpage = ptotalrecord/ppagesize + 1;
}
 
Public int gettotalrecord (){
Return totalrecord;
}
 
Public void settotalrecord (INT ptotalrecord ){
Totalrecord = ptotalrecord;
}
}

 

 

 

<% @ Page import = "Java. util. List" %>
<% @ Page import = "bean. Guest" %>
<% @ Page import = "Java. util. arraylist" %>
<% @ Page contenttype = "text/html; charset = UTF-8" Language = "Java"
Import = "Java. SQL. *" errorpage = "" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<JSP: usebean id = "DB" class = "bean. ormdbutil" Scope = "page"> </jsp: usebean>
<JSP: usebean id = "pager" class = "bean. Page" Scope = "page"> </jsp: usebean>
<%
String SQL = "select * from guests ";
Int currentrecord = 0;
Arraylist <guest> result = dB. Select (SQL );
Pager. settotalrecord (result. Size ());
Pager. settotalpage (result. Size (), pager. getpagesize ());

If (request. getparameter ("currentrecord ")! = NULL ){
Currentrecord = integer. parseint (request. getparameter ("currentrecord "));
Pager. setcurrentrecord (currentrecord );
Pager. setcurrentpage (currentrecord, pager. getpagesize ());
}

// Retrieve the subset to be displayed on the current page
List <guest> subresult = NULL;
If (currentrecord = 0 );
Subresult = result. sublist (0, pager. getpagesize ());
If (pager. getcurrentrecord () + pager. getpagesize () <result. Size ())
Subresult = result. sublist (pager. getcurrentrecord (),
Pager. getcurrentrecord () + pager. getpagesize ());
Else
Subresult = result. sublist (pager. getcurrentrecord (),
Result. Size ());
%>
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> an usage example on pages </title>
</Head>

<Body>
<Font size = "+ 2"> <strong> This page uses Javabean to display the page by page. </Strong> <br/>
The following table shows the running result. </font>
<Table width = "387" border = "1" Height = "47">
<Tr>
<TD Height = "41"> NO. </TD>
<TD> name </TD>
<TD> phone number </TD>
<TD> email </TD>
</Tr>
<%
If (subresult. isempty () = false ){
For (INT I = 0; I <subresult. Size (); I ++ ){
Guest guest = subresult. Get (I );
Out. Print ("<tr> ");
Out. Print ("<TD>" + guest. GETID () + "</TD> ");
Out. Print ("<TD>" + guest. getcname () + "</TD> ");
Out. Print ("<TD>" + guest. getphone () + "</TD> ");
Out. Print ("<TD>" + guest. getemail () + "</TD> ");
Out. Print ("</tr> ");
}
}
%>
</Table>
<Span> <font size = "+ 2"> total <% = pager. gettotalrecord () %> record | Total <% = pager. gettotalpage () %> page | current <% = pager. getcurrentpage () + 1%> page | each page
<% = Pager. getpagesize () %> | <%
If (pager. getcurrentrecord ()-pager. getpagesize () <0)
Out. Print ("homepage | ");
Else
Out. Print ("<a href = 'pagequery. jsp? Currentrecord ="
+ (Pager. getcurrentrecord ()-pager. getpagesize ())
+ "& Pagesize =" + pager. getpagesize () + "'> Previous Page, </a> | ");

If (pager. getcurrentrecord () + pager. getpagesize ()> Pager
. Gettotalrecord ())
Out. Print ("last page ");
Else
Out. Print ("<a href = 'pagequery. jsp? Currentrecord ="
+ (Pager. getcurrentrecord () + pager. getpagesize ())
+ "& Pagesize =" + pager. getpagesize () + "'> next page </a> | ");
%>
</Font> </span>
</Body>
</Html>

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.