Algorithm for paging:
You need to define four variables that have their own usefulness.
int pageSize: How many records are displayed per page
int pagenow: Want to show the first few pages
int PageCount: How many pages are there?
INT ROWCOUNT: How many records are there?
Description
PageSize is specified, pagenow refers to the user's choice.
The rowcount is obtained from the query in the table.
PageCount is calculated, the formula for this calculation is:
if (rowcount%pagesize==0) {
pagecount=rowcount/pagesize;}
else {
pagecount=rowcount/pagesize+1;
}
If you are using a statement: Select field Name list from table names where ID between. and.
This SQL statement is really fast, but there is a problem, that is, if the ID of the table is deleted, then a page may be less than one record.
Therefore, the final method is the following statement:
Select top pageSize field list from table name where ID not in (select Top pagesize* (pageNow-1) ID from table name)
The implementation code is:
Import javax.servlet.http.*;
Import java.io.*;
Import java.sql.*; public class Fenye extends httpservlet{public void doget (HttpServletRequest req,httpservletresponse res) {Co
Nnection Ct=null;
PreparedStatement Ps=null;
ResultSet Rs=null; int pagesize=3; You want to display the number of records per page int pagenow=1; Initializes the current page to the first page int pagecount=0; Total number of pages, which need to be computed by the int rowcount=0; The total number of records, check the table to learn String spagenow=req.getparameter ("Pagenow"); Receive the current page passed over if (spagenow!=null)//If a Non-null value is received, convert it to an integer {Pagenow=integer.parse
Int (Spagenow);
} try{PrintWriter pw=res.getwriter ();
Class.forName ("Com.microsoft.sqlserver.jdbc.SQLServerDriver");
Ct=drivermanager.getconnection ("Jdbc:sqlserver://127.0.0.1:1433;databasename=students", "sa", "password"); Ps=ct.preparestatement ("SELECT count (*) from [students].[ DBO]. [Students] ");
Get total number of records in a table Rs=ps.executequery (); while (Rs.next ()) {rowcount=rs.getint (1);
Gets the total number of records in the table} if (rowcount%pagesize==0)//calculates the total page count {pagecount=rowcount/pagesize;
else {pagecount=rowcount/pagesize+1; } ps=ct.preparestatement ("select Top" +pagesize+) * FROM [students].[ DBO]. [Students] where ID not in (select Top "+pagesize* (pageNow-1) +" IDs from [students].[ DBO].
[Students]);
Rs=ps.executequery (); Pw.println ("<body><center>");
The query results are presented in the form of Pw.println ("<table border=1");
Pw.println ("<tr><th>id</th><th>name</th><th>grade</th></tr>");
while (Rs.next ()) {pw.println ("<tr>");
Pw.println ("<td>" +rs.getint (1) + "</td>");
Pw.println ("<td>" +rs.getstring (2) + "</td>");
Pw.println ("<td>" +rs.getstring (3) + "</td>");
Pw.println ("</tr>"); } PW.PRINtln ("</table>"); if (pagenow==1)//previous hyperlink, the page no longer changes {pw.println ("<a href=fenye?pagenow=" +pagenow+ ">" + "forward" + "when you have already jumped to the first page)
</a> "); else//did not jump to the first page, each click of a hyperlink, the page to jump forward once {pw.println ("<a href=fenye?pagenow=" + (pageNow-1) + ">" + "forw
ard "+" </a> "); } if (pagecount<=5)//controls the number of page hyperlinks displayed {for (int i=1;i<=pagecount;i++) {pw.println ("
; a href=fenye?pagenow= "+i+" > "+i+" </a> "); }}else if (pagecount-pagenow<=5) {for (int i=pagenow;i<=pagecount;i++) pw.println ("<a HRE
f=fenye?pagenow= "+i+" > "+i+" </a>); }else///When there are too many pages, control the number of hyperlinks to display in order to be beautiful for the page {for (int i=pagenow;i<=pagenow+5;i++) pw.println ("<a href=fe
nye?pagenow= "+i+" > "+i+" </a>); if (Pagenow==pagecount)//has been the last page, click on the next page no longer jump {pw.println ("<a href=fenye?pagenow=" +p
agenow+ ">" + "Backward" + "</a>"); else {pw.println ("<a href=fenye?pagenow=" + (pagenow+1) + ">" + "Backward" + "</a>"
);
} pw.println ("</center></body>");
}catch (Exception ex) {ex.printstacktrace ();
} public void DoPost (HttpServletRequest req,httpservletresponse res) {this.doget (req,res);
}
}
Execution results:
When the number of records for each page shows 3 o'clock:
Click the appropriate connection to jump successfully.
The last page appears as:
Corresponding Code:
if (pagecount<=5)
{for
(int i=1;i<=pagecount;i++)
{
pw.println ("<a href=fenye?pagenow=" + i+ ">" +i+ "</a>");
}
Click Backward no longer jump.
To display the effect of controlling the number of page hyperlinks, change the number of records per page to 1.
First page display effect:
Corresponding Code:
else
{for
(int i=pagenow;i<=pagenow+5;i++)
pw.println ("<a href=fenye?pagenow=" +i+ ">" +i+ " </a> ");
}
The display of the current page number when it grows:
Corresponding Code:
else if (pagecount-pagenow<=5)
{for
(int i=pagenow;i<=pagecount;i++)
pw.println ("<a href= fenye?pagenow= "+i+" > "+i+" </a>);
}