本文實(shí)例為大家分享了servlet實(shí)現(xiàn)分頁效果的具體代碼,供大家參考,具體內(nèi)容如下
分頁的算法:
需要定義四個(gè)變量,它們有各自的用處
int pagesize:每頁顯示多少條記錄
int pagenow:希望顯示第幾頁
int pagecount:一共有多少頁
int rowcount:一共有多少條記錄
說明:
pagesize是指定,pagenow是指用戶的選擇。
rowcount是從表中查詢得到的。
pagecount是計(jì)算出來的,該計(jì)算公式為:
1
2
3
4
5
|
if (rowcount%pagesize== 0 ) { pagecount=rowcount/pagesize; } else { pagecount=rowcount/pagesize+ 1 ; } |
如果使用語句:select 字段名列表 from 表名 where id between ? and ?
這個(gè)sql語句確實(shí)比較快,但是存在一個(gè)問題,即如果表的id被刪除了,那么某頁可能就會(huì)少一條記錄。
因此,最終方法是如下語句:
select top pagesize 字段名列表 from 表名 where id not in(select top pagesize*(pagenow-1) id from 表名)
實(shí)現(xiàn)代碼為:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
import javax.servlet.http.*; import java.io.*; import java.sql.*; public class fenye extends httpservlet{ public void doget(httpservletrequest req,httpservletresponse res) { connection ct= null ; preparedstatement ps= null ; resultset rs= null ; int pagesize= 3 ; //希望每頁顯示記錄的條數(shù) int pagenow= 1 ; //初始化當(dāng)前頁為第一頁 int pagecount= 0 ; //總頁數(shù),需要通過計(jì)算得知 int rowcount= 0 ; //記錄總數(shù),查表獲知 string spagenow=req.getparameter( "pagenow" ); //接收傳遞過來的當(dāng)前頁面 if (spagenow!= null ) //若接收到非空值,將其轉(zhuǎn)為整數(shù) { pagenow=integer.parseint(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" , "密碼" ); ps=ct.preparestatement( "select count(*) from [students].[dbo].[students]" ); //獲取表中記錄總數(shù) rs=ps.executequery(); while (rs.next()) { rowcount=rs.getint( 1 ); //獲取表中記錄總數(shù) } if (rowcount%pagesize== 0 ) //計(jì)算總頁面數(shù) { 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 )+ " id from [students].[dbo].[students])" ); rs=ps.executequery(); pw.println( "<body><center>" ); //將查詢結(jié)果以表的形式展現(xiàn) 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 ) //前一頁超鏈接,當(dāng)已經(jīng)跳轉(zhuǎn)到第一頁時(shí),頁面不再改變 { pw.println( "<a href=fenye?pagenow=" +pagenow+ ">" + "forward" + "</a>" ); } else //未跳轉(zhuǎn)到第一頁時(shí),每點(diǎn)擊一次超鏈接,頁面向前跳轉(zhuǎn)一次 { pw.println( "<a href=fenye?pagenow=" +(pagenow- 1 )+ ">" + "forward" + "</a>" ); } if (pagecount<= 5 ) //控制顯示頁數(shù)超鏈接的個(gè)數(shù) { 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 href=fenye?pagenow=" +i+ ">" +i+ "</a>" ); } else //當(dāng)頁面數(shù)過多時(shí),為了頁面美觀需要控制顯示超鏈接個(gè)數(shù) { for ( int i=pagenow;i<=pagenow+ 5 ;i++) pw.println( "<a href=fenye?pagenow=" +i+ ">" +i+ "</a>" ); } if (pagenow==pagecount) //已經(jīng)為最后一頁時(shí),點(diǎn)擊后一頁不再跳轉(zhuǎn) { pw.println( "<a href=fenye?pagenow=" +pagenow+ ">" + "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); } } |
執(zhí)行結(jié)果:
當(dāng)每頁顯示記錄數(shù)為3時(shí):
點(diǎn)擊相應(yīng)連接可以成功跳轉(zhuǎn)。
最后一頁顯示為:
對(duì)應(yīng)代碼:
1
2
3
4
5
6
7
|
if (pagecount<= 5 ) { for ( int i= 1 ;i<=pagecount;i++) { pw.println( "<a href=fenye?pagenow=" +i+ ">" +i+ "</a>" ); } } |
點(diǎn)擊backward不再跳轉(zhuǎn)。
為了顯示程序控制頁數(shù)超鏈接數(shù)目的效果如何,將每頁顯示記錄數(shù)改為1。
第一頁顯示效果:
對(duì)應(yīng)代碼:
1
2
3
4
5
|
else { for ( int i=pagenow;i<=pagenow+ 5 ;i++) pw.println( "<a href=fenye?pagenow=" +i+ ">" +i+ "</a>" ); } |
當(dāng)前頁碼逐漸增大時(shí)的顯示效果:
對(duì)應(yīng)代碼:
1
2
3
4
5
|
else if (pagecount-pagenow<= 5 ) { for ( int i=pagenow;i<=pagecount;i++) pw.println( "<a href=fenye?pagenow=" +i+ ">" +i+ "</a>" ); } |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/lissdy/article/details/7394030