Topic: 完全JSP分页代码

  Print this page

1.完全JSP分页代码 Copy to clipboard
Posted by: David_Zhuo
Posted on: 2002-12-19 12:47

来自:大天使 CNJSP.net
加入时间:2001-06-10 10:57:58
点击数:105

先声明一下,本人也是刚学习JSP没多少时间,期间得到了 何志强 先生的大力帮助,解决了很多Java平台方面的问题。

对于分页这个问题,我试着以以前写ASP和PHP的思路做了一下,是以纯JSP代码的形式,后来又写了一个bean来实现,实际的结果发现2者速度没有多大差别(我是做了20000条记录的分页:>)我还想试试servlet的结果如何
关于分页的代码如下:

//Author tar(bigangell@yesky.com)
//您可以任意拷贝复制该代码,但请加上以上作者信息
//有任何问题请与我联系
//效果请看 http://210.77.144.64/kk/java/java/article.jsp

<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<jsp:useBean id="sqlconn" scope="page" class="kk.database.sqlconn" />
<%! String aTitle,aAuthor,AType,ArticleType,InsertTime,Color; %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>ArticleList</TITLE>
<META NAME="Generator" CONTENT="jBuilder3.5 && jEdit1.0">
<META NAME="Author" CONTENT="bigangell">
<META NAME="Keywords" CONTENT="Java,Servlet,Jsp,Beans,PHP">
<META NAME="Description" CONTENT="A Pure World For Programming Include C/C++,Java">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=gb2312">
<link rel="stylesheet" href="style.css">
</head>

<body bgcolor="#FFFFFF">
<table width="760" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td>
<table width="750" border="0" cellspacing="2" cellpadding="2" align="center">
<tr align="center">
<td> </td>
</tr>
<tr>
<td>
<table width="740" border="0" cellspacing="1" cellpadding="1" align="center">
<tr align="center" bgcolor="#CCCCCC">
<td><b>标题</b></td>
<td><b>作者</b></td>
<td><b>类型</b></td>
<td><b>点击</b></td>
<td><b>加入时间</b></td>
</tr>
<%
int i;
int ipage=10; //分页单位
int allpage; //总页数
int pages; //接受的页码变量
int cpage=1; //当前页
int spage; //开始页

//数据库连接声明
java.lang.String sql,pagesql;
java.sql.ResultSet rs,pagers;
pagesql = "select count(a_id) as A from article";

//取总文章数
pagers = sqlconn.executeQuery(pagesql);
int allCol = pagers.getInt("A");

//获得总页面数
allpage = (int)Math.ceil((allCol + ipage-1) / ipage);

//判断参数pages是否为空
if (request.getParameter("pages")==null) {
pages=1;
} else {
pages = new Integer(request.getParameter("pages")).intValue();
}

//判断当前页
if(pages > allpage // pages == 0){
cpage = 1;
} else {
cpage = pages;
}

//判断起始页
spage=(cpage-1)*ipage;

sql = "select a_id,a_title,a_author,aID,a_time,a_readtime,sID from article order by a_id desc limit "+ spage +","+ ipage;
rs = sqlconn.executeQuery(sql);
int cu=1;

//循环取数据库值
while (rs.next()) {
int aID = rs.getInt("a_id");
aTitle = rs.getString("a_title");
aAuthor = rs.getString("a_author");
int ArticleTypeID = rs.getInt("aID");
int ReadTime = rs.getInt("a_readtime");
InsertTime = rs.getString("a_time");
if (cu % 2 == 0) { Color="#DBECFD"; }
else { Color="#C6E1FD"; }
switch (ArticleTypeID) {
case(1):
ArticleType="小说";
break;
case(2):
ArticleType="散文";
break;
case(3):
ArticleType="诗歌";
break;
case(4):
ArticleType="随笔";
break;
}
%>
<tr bgcolor="<%= Color %>">
<td width="300"><a href="viewarticle.jsp?aID=<%= aID %>"><%= aTitle %></a></td>
<td align="center" width="100"><%= aAuthor %></td>
<td align="center" width="50"><%= ArticleType %></td>
<td align="center" width="50"><%= ReadTime %></td>
<td align="center" width="240"><%= InsertTime %></td>
</tr>
<%
cu++;
}
%>
<tr bgcolor="#CCCCCC">
<td colspan="5" align="right" valign="middle">共 <%= allCol %>条记录 共 <%= allpage %>页 <% if (cpage>1) { %><a href="article.jsp?pages=<%= cpage-1 %>"><img src="images/prev.gif" border="0"></a><% } if (cpage<allpage) { %><a href="article.jsp?pages=<%= cpage+1 %>"><img src="images/next.gif" border="0"></a><% } %> / <% for (i=1;i<=allpage;i++) { if (i != pages) {%><a href="article.jsp?pages=<%= i %>"><%= i %></a> <% } }%></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>

如果我们的引用侵犯了您的权利,请立即 [通知我] ,我会立即删去

2.这种做法好原始,而且只适合mysql数据库 [Re: David_Zhuo] Copy to clipboard
Posted by: xjimmy
Posted on: 2002-12-19 13:03

这种做法好原始,而且只适合mysql数据库

3.一个通用各种数据库的分页方法 [Re: David_Zhuo] Copy to clipboard
Posted by: encamp
Posted on: 2002-12-19 17:13

如果要做一个通用各种数据库的分页方法,有没有什么好的想法?

4.Scrollable [Re: David_Zhuo] Copy to clipboard
Posted by: snowbug
Posted on: 2002-12-19 23:25

I wonder if we can use the ScrollableResultSet if the underline DB supports it.

int pagesize = 30;
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery(SQL);
if(rs.last()){
int numberOfRecords = rs.getRow();
int totalPages = numberOfRecords / pagesize + ( (numberOfRecords % pagesize)>0? 1 : 0);
//to fetch the n-th page (n is 1-based)
int start = pagesize * (( n > totalPages ? totalPages : n) -1 ) ;
rs.absolute(start);
do {
//fetch the data one record a time
//quit the loop if finished fetching one page of data
} while (rs.next());
}

rs.close();
stmt.close();

5.Re:完全JSP分页代码 [Re: David_Zhuo] Copy to clipboard
Posted by: floe
Posted on: 2002-12-21 21:25

就像上面说的,这是一种很糟糕的办法
应该用jdbc2.0标准的resultset.relative()函数
在jdbc1中用循环next()

6.Re:完全JSP分页代码 [Re: floe] Copy to clipboard
Posted by: snowbug
Posted on: 2002-12-22 09:45

floe wrote:
就像上面说的,这是一种很糟糕的办法
应该用jdbc2.0标准的resultset.relative()函数
在jdbc1中用循环next()

The relative() requires scrollable resultset. - so it should work then? Have you tried it before?

I used scrollable resultset before and it worked. What I am not sure is if it is a good idea to use it for the paging purpose. Any inputs?

7.Re:完全JSP分页代码 [Re: David_Zhuo] Copy to clipboard
Posted by: wyan716
Posted on: 2003-01-08 17:40

用SQL分页

8.Re:完全JSP分页代码 [Re: David_Zhuo] Copy to clipboard
Posted by: freeman99
Posted on: 2003-01-10 15:37

我做了一个taglib,支持分页,你只要在jsp里面引用这个taglib,然后设置一些参数,再写一个获取数据的helper class就搞定一切了。很方便。要的话把email贴出来。

9.Re:完全JSP分页代码 [Re: David_Zhuo] Copy to clipboard
Posted by: keron
Posted on: 2003-01-14 09:28

freeman99 来一份!
:)
mochale2000@yahoo.com.sg

10.Re:完全JSP分页代码 [Re: freeman99] Copy to clipboard
Posted by: lanya
Posted on: 2003-01-18 16:47

我做了一个taglib,支持分页,你只要在jsp里面引用这个taglib,然后设置一些参数,再写一个获取数据的helper class就搞定一切了。很方便。要的话把email贴出来。

给我发一个完整的例子,十分感谢!
lanya001@sina.com

11.Re:完全JSP分页代码 [Re: David_Zhuo] Copy to clipboard
Posted by: hangzxf
Posted on: 2003-01-21 19:10

也给我来一份吧,学习学习,谢谢
hangzxf@163.com

12.Re:完全JSP分页代码 [Re: David_Zhuo] Copy to clipboard
Posted by: damonzhang
Posted on: 2003-01-22 15:52

这个我也十分感兴趣,给我一份吧 zhangdt@neusoft.com

13.Re:完全JSP分页代码 [Re: David_Zhuo] Copy to clipboard
Posted by: kyle
Posted on: 2003-01-22 16:16

比较感兴趣,给我也来一份,wangzk@163.com。谢谢!

14.Re:完全JSP分页代码 [Re: David_Zhuo] Copy to clipboard
Posted by: carion
Posted on: 2003-01-24 09:59

一份carion@163.net

15.Re:完全JSP分页代码 [Re: David_Zhuo] Copy to clipboard
Posted by: attaguy
Posted on: 2003-02-04 14:08


16.Re:Scrollable [Re: snowbug] Copy to clipboard
Posted by: kkhui
Posted on: 2003-02-04 23:24

如果你看过了Oracle的ScrollableResultSet的实现你会发现这样和用next循环的效率一样
snowbug wrote:
I wonder if we can use the ScrollableResultSet if the underline DB supports it.

int pagesize = 30;
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery(SQL);
if(rs.last()){
int numberOfRecords = rs.getRow();
int totalPages = numberOfRecords / pagesize + ( (numberOfRecords % pagesize)>0? 1 : 0);
//to fetch the n-th page (n is 1-based)
int start = pagesize * (( n > totalPages ? totalPages : n) -1 ) ;
rs.absolute(start);
do {
//fetch the data one record a time
//quit the loop if finished fetching one page of data
} while (rs.next());
}

rs.close();
stmt.close();


17.Re:完全JSP分页代码 [Re: David_Zhuo] Copy to clipboard
Posted by: joesen
Posted on: 2003-02-08 08:52

我也要一份,pk3g9a@163.com

18.Re:完全JSP分页代码 [Re: David_Zhuo] Copy to clipboard
Posted by: 无花无酒锄作田
Posted on: 2003-02-10 09:36

也给我一份tiantao78@163.net

19.Re:完全JSP分页代码 [Re: freeman99] Copy to clipboard
Posted by: adelejxsx
Posted on: 2003-03-19 14:14

给我来一份,adelejxsx@163.com

20.Re:完全JSP分页代码 [Re: adelejxsx] Copy to clipboard
Posted by: dapan
Posted on: 2003-03-19 17:03

请不要跟贴索档,如需要请使用PM。

谢谢!

^_^


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923