Java Paging Query--Paging Display

When there are too many data bars in the database, a page can not be displayed. This is to set up paging query. The first thing to use is the limit condition of database sql statement to realize grouping query.
The sql statement is roughly in the form of:
select * from table limit to start the index, showing the number of bars
With this statement, block queries are implemented and fixed number of entries are displayed on each page.
First, to realize background paging, we need to know how many pages it has and how many rows it has per page. This requires knowing how many rows it has in total. When calling sql statements, we also need to know the starting index of each page. The starting index is calculated according to the current number of pages, so we also need to know the current number of pages. After querying, we will return a list to store the current page data. Encapsulating these attributes and methods to get settings into a class has the following page class:

 1 public class Page<T> {
 2     private List<T> data;//Data list
 3     private int pagenum;//page
 4     private int pagesize;//Number of items displayed on the current page
 5     private int rows;//Total number of banks
 6     public Page(int rows,int pagenum, int pagesize) {
 7         super();
 8         data=new ArrayList<>();
 9         this.rows=rows;
10         this.setPagesize(pagesize);
11         this.setPagenum(pagenum);        
12     }
13     public Page() {
14         super();
15     }
16     public int getPagenum() {
17         return pagenum;
18     }
19     public void setPagenum(int pagenum) {
20         if(pagenum>getTotalpage())
21         {
22             this.pagenum=getTotalpage();
23         }
24         else {
25             this.pagenum = pagenum;
26         }    
27         if(pagenum<1)
28         {
29             this.pagenum=1;
30         }
31     }
32     public int getPagesize() {
33         return pagesize;
34     }
35     public void setPagesize(int pagesize) {
36         this.pagesize = pagesize;
37     }
38     public int getTotalpage() {
39         //Calculate the total number of pages
40         if(rows%pagesize==0)
41         {
42             return rows/pagesize;
43         }
44         else {
45             return rows/pagesize+1;
46         }        
47     }
48     public int getRows() {
49         return rows;
50     }
51     public void setRows(int rows) {
52         this.rows = rows;
53     }
54     public int getIndexnum() {
55         //Get the index value
56         return pagesize*(pagenum-1);
57     }
58     public List<T> getData() {
59         return data;
60     }
61     public void setData(List<T> data) {
62         this.data = data;
63     }
64 }
Class Page

When initializing, we only need to get the total number of data items, the number of pages and the number of display items per page. The total number of data bars can be obtained with the sql statement select count (*) from table. Each query only needs to pass in the current number of pages. The page object after each query is passed into the jsp front page and displayed with a div

 1 <div>
 2         <a href="/stuent8.11/stuServlet?pagenum=1">home page</a>
 3         <a href="/stuent8.11/stuServlet?pagenum=${apage.pagenum-1 }">Previous page</a>
 4         <c:choose>
 5             <c:when test="${apage.totalpage<=5 }">
 6                 <c:set var="begin" value="1"></c:set>
 7                 <c:set var="end" value="${apage.totalpage }"></c:set>
 8             </c:when>
 9             <c:when test="${apage.totalpage>5 }">
10                 <c:set var="begin" value="1"></c:set>
11                 <c:set var="end" value="5"></c:set>
12                 <c:if test="${apage.pagenum>3 }">
13                     <c:set var="begin" value="${apage.pagenum-2 }"></c:set>
14                     <c:set var="end" value="${apage.pagenum+2 }"></c:set>
15                 </c:if>
16                 <c:if test="${end>apage.totalpage }">
17                     <c:set var="begin" value="${apage.totalpage-4 }"></c:set>
18                     <c:set var="end" value="${apage.totalpage }"></c:set>
19                 </c:if>
20             </c:when>
21         </c:choose>
22         <c:forEach begin="${begin }" end="${end }" step="1" var="num">
23             <c:if test="${apage.pagenum==num }">
24                 [${num }]
25             </c:if>
26             <c:if test="${apage.pagenum!=num }">
27                 <a href="/stuent8.11/stuServlet?pagenum=${num }">${num }</a>    
28             </c:if>
29         </c:forEach>
30         <a href="/stuent8.11/stuServlet?pagenum=${apage.pagenum+1 }">next page</a>
31         <a href="/stuent8.11/stuServlet?pagenum=${apage.totalpage }">Last</a>
32     </div>
Display div

One of the choose s is to specify the maximum number of pages displayed on the page. Here is five pages. That is to say, when page 4 is on, page 1 will disappear and page 6 will appear.

Keywords: Java SQL Database JSP

Added by brandon on Tue, 04 Jun 2019 21:40:26 +0300