Package com. longweir;
// An After pagination
Import java. SQL .*;
Import com. longweir. util .*;
Public class pagebean {
Private int pagesize = 5; // The number of records displayed per page is 5
Private int currentpage = 1; // current page number
Private int pagecount = 1; // total number of pages
Private int totalcount = 0; // total number of records
// Calculate the total number of pages
Public void setpagecount ()
{
This. pagecount = (this. totalCount-1)/This. pagesize + 1;
}
// Obtain the total number of pages
Public int getpagecount ()
{
Return this. pagecount;
}
// Set and modify the current page number,
Public void setcurrentpage (INT currentpage ){
// Check the current page number
If (currentpage> This. pagecount)
This. currentpage = This. pagecount;
Else if (currentpage <1)
This. currentpage = 1;
Else
This. currentpage = currentpage;
}
// Obtain the current page number
Public int getcurrentpage (){
Return this. currentpage;
}
// Obtain the number of all records
Public int gettotalcount ()
{
Return this. totalcount;
}
// Set the total number of records
Public void settotalcount (INT totalcount)
{
This. totalcount = totalcount;
// After setting the total number of records, you must correct the total number of pages to be calculated.
This. pagecount = (this. totalCount-1)/This. pagesize + 1;
}
Public int getpagesize (){
Return pagesize;
}
Public void setpagesize (INT pagesize ){
This. pagesize = pagesize;
// Set the number of records displayed on each page and adjust the total number of pages after calculation
This. pagecount = (this. totalCount-1)/This. pagesize + 1;
}
}
Package com. longweir;
// Any business logic class can display data by page as long as this interface is implemented
Import java. util .*;
Import com. longweir .*;
Public interface spiltpage {
// Retrieve data by PAGE based on parameters in the paging object
Public Collection getpagedata (pagebean) throws exception;
// Obtain the number of all records
Public int getavailablecount () throws exception;
}
In this way, the main paging method has been completed. We have developed a business logic class productutil for database table operations to implement the above interface.
The following class is used to operate the data in the product table by page or all data:
Java code
[Java] view plaincopy
Package com. longweir;
/*
* This operation logic includes all product information
**/
Import java. Io .*;
Import java. SQL .*;
Import java. util .*;
Import com. longweir. spiltpage;
Import com. longweir. Bean. productinfovobean;
Import com. longweir. util. databaseconnection;
Public class productutil implements spiltpage {
Private connection conn;
// Rewrite the no-argument constructor to obtain the database connection
Public productutil ()
{
This. Conn = databaseconnection. getconnection (); // obtain the database connection object
}
// Implement methods in the interface to retrieve data display by PAGE
Public Collection getpagedata (pagebean) throws exception
{
Statement stmt = NULL;
Resultset rs = NULL;
Collection ret = new arraylist ();
If (conn. isclosed () Conn = databaseconnection. getconnection ();
String sqlstr = "select * From productinfo order by productid limit" + (pagebean. getcurrentpage ()-1) * pagebean. getpagesize () + "," + pagebean. getpagesize ();
Try
{
Stmt = conn. createstatement ();
Rs1_stmt.exe cutequery (sqlstr );
While (Rs. Next ())
{
Productinfovobean productinfo = new productinfovobean ();
Productinfo. setcategoryid (Rs. getstring ("catid "));
Productinfo. setproductname (Rs. getstring ("productname "));
Productinfo. setproductid (Rs. getstring ("productid "));
Productinfo. setpublishment (Rs. getstring ("publishment "));
Productinfo. setprice (Rs. getfloat ("price "));
Productinfo. setdescription (Rs. getstring ("descn "));
Ret. Add (productinfo );
}
Stmt. Close ();
Rs. Close ();
Conn. Close ();
}
Catch (exception E)
{
E. printstacktrace ();
}
Return ret;
}
// The total number of records obtained by implementing the Interface Method
Public int getavailablecount ()
{
Statement stmt = NULL;
Resultset rs = NULL;
Int counter = 0;
Try {
If (conn. isclosed () Conn = databaseconnection. getconnection ();
Stmt = conn. createstatement ();
Rs1_stmt.exe cutequery ("select count (*) from productinfo ");
While (Rs. Next ())
{
Counter = Rs. getint (1 );
}
Stmt. Close ();
Rs. Close ();
Conn. Close ();
}
Catch (exception e ){}
Return counter;
}
The key technology of paging is the paging query statement in MySQL:
"Select * From productinfo order by productid limit" + (pagebean. getcurrentpage ()-1) * pagebean. getpagesize () + "," + pagebean. getpagesize ();
Develop a viewproduct. jsp page to display data by page:
HTML code
[XHTML] view plaincopy
<% @ Page contenttype = "text/html; charset = GBK" %>
<% @ Page import = "Java. util. *" %>
<% @ Page import = "Java. Io. *" %>
<% @ Page import = "com. longweir. Bean. *" %>
<JSP: usebean id = "product" class = "com. longweir. productutil" Scope = "session"/>
<JSP: usebean id = "pagebean" class = "com. longweir. pagebean" Scope = "session"/>
<HTML>
<Head>
<Title> View information about all products </title>
<LINK rel = "stylesheet" type = "text/CSS" href = "CSS/style.css" mce_href = "CSS/style.css">
</Head>
<Body>
<H3 align = "center"> View All Product Information <Table width = "960" align = "center" border = "0" cellpadding = "2" cellspacing = "1" bgcolor = "#999999">
<Tr bgcolor = "# efeeed">
<TD width = "100"> product no. </TD>
<TD width = "100"> Category </TD>
<TD width = "200"> name </TD>
<TD width = "100"> publisher </TD>
<TD width = "80"> price </TD>
<TD width = "200" type = "option" text = "option"> description </TD type = "option" text = "/option">
<TD colspan = "2" width = "100" align = "center"> management </TD>
</Tr>
<%
String temppage = request. getparameter ("page ");
Int PNO = 1;
If (temppage! = NULL &&! (""). Equals (temppage ))
{
Try
{
PNO = integer. parseint (temppage); // obtain the submitted page number
}
Catch (exception E)
{
PNO = 1; // jump directly to the first entry if an exception occurs.
}
}
// The number of records in the table should be retrieved every time the page is refreshed, because the table records may be updated at any time during the page turning process.
Pagebean. settotalcount (product. getavailablecount ());
Pagebean. setcurrentpage (PNO );
%>
<%
Collection productproducts = product. getpagedata (pagebean); // display by PAGE
Iterator it = products. iterator ();
While (it. hasnext ())
{
Productinfovobean temp = (productinfovobean) it. Next ();
Out. println ("<tr bgcolor =/" # ffffff/"> ");
Out. println ("<TD>" + temp. getproductid () + "</TD> ");
Out. println ("<TD>" + temp. getcategoryid () + "</TD> ");
Out. println ("<TD>" + temp. getproductname () + "</TD> ");
Out. println ("<TD>" + temp. getpublishment () + "</TD> ");
Out. println ("<TD>" + temp. getprice () + "</TD> ");
Out. println ("<TD>" + temp. getdescription () + "</TD> ");
Out. println ("<TD algin =/" center/">" + "<a href =" # "mce_href =" # "> modify </a>" + "</TD> ");
Out. println ("<TD align =/" center/">" + "<a href ="/"mce_href ="/""/product/servlet/deleteproductservlet? Productid = "+ temp. getproductid () +"/"> Delete </a </TD> ");
Out. println ("</tr> ");
}
%>
</Table>
<Table width = "960" align = "center" border = "0" cellpadding = "1" cellspacing = "2">
<Tr>
<TD> </TD>
<Tr>
<Tr>
<TD align = "right">
Total pages <% = pagebean. getpagecount () %>
<%
For (INT I = 1; I <= pagebean. getpagecount (); I ++)
Out. println ("<a href ="/product/viewproduct. jsp? Page = "mce_href =" Product/viewproduct. jsp? Page = "" + I + ">" + I + "</a> ");
%>
</TD>
</Tr>
</Table>
</Body>
</Html>