I have been working on the final project recently, so I have encountered many problems and solved them. I will write them here to share with you ....
After one morning, I thought about it myself, and referred to some things, I made the database paging viewing function. I think it is still clear. I hope you will give me more advice...
The idea is as follows: Use javabean to operate paging control data, and then implement connections such as the last next page in jsp to add parameter value judgment, solved some problems perfectly.
For details, see the source code...
View. java
Public class View {
Private int currentPage; // record the current page number
Private int pageSize; // number of records displayed on each page
Private int recordCount; // The total number of records.
Public View (int pageSize, int recordCount, int currentPage ){
This. pageSize = pageSize;
This. recordCount = recordCount;
This. setCurrentPage (currentPage );
}
// Calculate the total number of pages
Public int getPageCount (){
Int size = recordCount/pageSize; // total number of pages = Total number of records/actual records per page
Int flag = recordCount % pageSize; // modulo operation, to calculate the number of entries to be displayed on the last page
If (flag! = 0 ){
Size ++;
}
// Determine the total number of returned pages based on the number of records
If (recordCount = 0) {// if there are 0 records
// Only one page is returned
Return 1;
}
// Return the calculated page number
Return size;
}
// Set the start position of the limit index in the SQL statement, starting from 0.
Public int getFromIndex (){
Return (currentPage-1) * pageSize; // key algorithm: (current page number-1) * Number of records per page = index start position
}
// Set the current page
Public void setCurrentPage (int currentPage ){
// Why?
/*
* Because: In the jsp page, two buttons are placed: the home page and the previous page, the next page, and the last page.
* When you click the next page on the last page, the number of pages obtained is greater than the actual page, so the page must be kept on the last page.
* When you click the previous page on the first page, the number of pages obtained is smaller than or equal to 0, and the page is always on the first page.
*/
Int vaildPage = currentPage <= 0? 1: currentPage;
VaildPage = vaildPage> this. getPageCount ()? This. getPageCount ()
: VaildPage;
This. currentPage = vaildPage;
}
// Obtain the current page for display in JSP
Public int getCurrentPage (){
Return currentPage;
}
// Obtain the number of records displayed on each page
Public int getPageSize (){
Return pageSize;
}
}
Index. jsp
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<% @ Page import = "java. SQL. Connection" %>
<% @ Page import = "java. SQL. DriverManager" %>
<% @ Page import = "java. SQL. PreparedStatement" %>
<% @ Page import = "java. SQL. ResultSet" %>
<% @ Page import = "com. view. View" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> View databases by page </title>
</Head>
<Body>
<%! // Database connection information
Public static final String URL = "jdbc: mysql: // localhost: 3306/j2ee ";
Public static final String USERNAME = "root ";
Public static final String PASSWORD = "root"; %>
<%
// Connect to the database and obtain the number of records
Class. forName ("com. mysql. jdbc. Driver ");
Connection conn = DriverManager. getConnection (URL, USERNAME,
PASSWORD );
String sqlCount = "select count (*) from employee"; // obtain the total number of records.
PreparedStatement ps = conn. prepareStatement (sqlCount );
ResultSet rs = ps.exe cuteQuery ();
Int recordCount = 0; // total number of records
If (rs. next ()){
RecordCount = rs. getInt (1 );
}
%>
<%
// Retrieve the paging object and pass the Parameter
String pageStr = request. getParameter ("page ");
Int currentPage = 1;
If (pageStr! = Null ){
CurrentPage = Integer. parseInt (pageStr );
}
View view = new View (5, recordCount, currentPage );
// Obtain parameters
Int fromIndex = view. getFromIndex (); // start position
Int pageSize = view. getPageSize (); // number of records displayed on each page
%>
<Table border = "1">
<%
// Execute SQL
String SQL = "select fname, lname from employee limit ?,? ";
Ps = conn. prepareStatement (SQL );
Ps. setInt (1, fromIndex );
Ps. setInt (2, pageSize );
Rs = ps.exe cuteQuery ();
While (rs. next ()){
String fname = rs. getString (1 );
String lname = rs. getString (2 );
%>
<Tr>
<Td> <% = fname %> </td>
<Td> <% = lname %> </td>
</Tr>
<%
}
%>
</Table>
<%
// Close the connection
If (ps! = Null ){
Ps. close ();
}
If (rs! = Null ){
Rs. close ();
}
If (conn! = Null ){
Conn. close ();
}
%>
Current page <% = view. getCurrentPage () %>, total page <% = view. getPageCount () %> <br/>
<A href = "index. jsp? Page = 1 "> homepage </a>
<%
If (view. getCurrentPage ()-1) <= 0 ){
%>
<A href = "index. jsp? Page = 1 "> previous page </a>
<%
} Else {
%>
<A href = "index. jsp? Page = <% = (view. getCurrentPage ()-1) %> "> previous page </a>
<%
}
%>
<%
If (view. getCurrentPage () + 1)> view. getPageCount ()){
%>
<%
} Else {
%>
<A href = "index. jsp? Page = <% = (view. getCurrentPage () + 1) %> "> next page </a>
<%
}
%>
<A href = "index. jsp? Page = <% = view. getPageCount () %> "> last page </a>
<Br>
</Body>
</Html>
From: Technical bean blog