在JSP中如何分页显示数据!(请说的通俗明了一些,实用方便!)

nuanshuidai 2003-08-19 12:41:23
从数据库中检索出来的结果如何分页显示,用到哪些技术,看了一些例程,写的不是很明白,请赐教!
...全文
1184 39 打赏 收藏 转发到动态 举报
写回复
用AI写文章
39 条回复
切换为时间正序
请发表友善的回复…
发表回复
c654964294 2010-06-15
  • 打赏
  • 举报
回复
谢谢了。
kui 2003-10-09
  • 打赏
  • 举报
回复
在新版本的JSPStudio中,通过向导,只要输入一个文件名和选择数据源,自动产生所有数据库操作和表格分页显示,下载地址:ftp://162.105.80.43/incoming/

例如:输入文件名:TEST,并通过ODBC选择数据源,自动产生如下6个文件:
TEST.java
TESTAction.java
TESTDAO.java
TESTForm.java
TEST.jsp
TESTOut.jsp

代码见:
http://expert.csdn.net/Expert/topic/2228/2228055.xml?temp=.1732599
wh_cisco 2003-10-09
  • 打赏
  • 举报
回复
http://www.javaresearch.org/article/showarticle.jsp?column=106&thread=8893
很不错的分页代码,肯定比上边的“目前最好的分页技术”强!
seaman0916 2003-10-09
  • 打赏
  • 举报
回复
http://www.hnitline.com/bbs/dispbbs.asp?boardID=18&ID=807

目前最好的分页技术

思想确实很好,看看就知道了!
wkliangsdqd 2003-09-18
  • 打赏
  • 举报
回复
mark,学习,不错的提示信息
hzqq 2003-08-22
  • 打赏
  • 举报
回复
给个bean
public ResultSet execQueryPage(String table,String field,String condition,int p)
{
String sql0=null;
try{
conn=DriverManager.getConnection(url,uid,pwd);
Statement stmt=conn.createStatement();
//start=(p-1)*pagesize;
//sql0=sql+" limit "+start+", "+pagesize;
sql0="select top "+pagesize+" "+field+" from "+table+" where "+condition+" and id not in (select top "+((p-1)*pagesize)+" id from "+table+" where "+condition+" order by id desc) order by id desc";
rs=stmt.executeQuery(sql0);
}

catch(SQLException ex)
{
System.err.println("mysqlbean.execQueryPage:"+ex.getMessage());
System.err.println(sql0);
}
return rs;

}
nuanshuidai 2003-08-22
  • 打赏
  • 举报
回复
感谢大家!
javaboy 2003-08-22
  • 打赏
  • 举报
回复
这是我写的分页标记,全自动实现分页功能的参数获取和传送,并带有查询记忆恢复功能,使用只有三行标记,不能再简单了:
毛遂自荐一个,欢迎批评指教。
http://www.xdevelop.net/gb/main.jsp?catalogID=31

经常有人问,数据库查询往哪里输入,我的分页只处理分页,不负责数据库,因为数据库查询因应用而异,我的标记只做分页份内的事,告诉你该从第几记录开始查了,该查多少条记录等,至于怎么查出来,这是你们自己实现的事了。

分页最麻烦的是翻页时大量参数的传递,以及查询条件的传递,这个标记已经全部处理好了,不用你操心,只需按步就班的写三个标记即可。

一个组件通常只解决一个问题,这是我设计组件的一个原则。
nuanshuidai 2003-08-22
  • 打赏
  • 举报
回复
allan1031(唐加西亚~米兰)兄:

我改了相应的代码,运行结果如下所示,请问哪有问题啊?

500 Servlet Exception
Note: sun.tools.javac.Main has been deprecated.
/loupan/4.jsp:71: Method last() not found in class java.lang.String.
sqlRst.last();
^
/loupan/4.jsp:73: Method getRow() not found in class java.lang.String.
intRowCount = sqlRst.getRow();
^
/loupan/4.jsp:129: Method absolute(int) not found in class java.lang.String.
sqlRst.absolute((intPage-1) * intPageSize + 1);
^
/loupan/4.jsp:135: Method isAfterLast() not found in class java.lang.String.
while(i<intPageSize && !sqlRst.isAfterLast()){
^
/loupan/4.jsp:149: Method getString(int) not found in class java.lang.String.
out.print((sqlRst.getString(2)));
^
/loupan/4.jsp:151: Method getString(int) not found in class java.lang.String.
out.print((sqlRst.getString(3)));
^
/loupan/4.jsp:153: Method getString(int) not found in class java.lang.String.
out.print((sqlRst.getString(4)));
^
/loupan/4.jsp:155: Method getString(int) not found in class java.lang.String.
out.print((sqlRst.getString(5)));
^
/loupan/4.jsp:158: Method next() not found in class java.lang.String.
sqlRst.next();
^
/loupan/4.jsp:184: Method close() not found in class java.lang.String.
sqlRst.close();
^
10 errors, 1 warning



--------------------------------------------------------------------------------
Resin 2.1.4 (built Fri Aug 2 14:16:52 PDT 2002)
nuanshuidai 2003-08-21
  • 打赏
  • 举报
回复
上面这个方法还是用rs.next()啊!效率如何呢?
monbit 2003-08-20
  • 打赏
  • 举报
回复
先用sql查询,再用游标定位你要的数据段(就是你要显示在一页上的那些数据,可以根据页号和总页数判断),最后取出来扔到页面上不就行了么?然后下一页就把刚才的页号+1,还用这个方法去做,那不就是下一页了么。。。。呵呵
ttaomeng 2003-08-20
  • 打赏
  • 举报
回复
分页最好是用SQL了,上面那样数据一多的话,会出现很多问题哦
nuanshuidai 2003-08-20
  • 打赏
  • 举报
回复
allan1031(唐加西亚~米兰)兄,十分感谢!

我试试,如果再有不懂的还要向你请教啊!
gks_cn 2003-08-20
  • 打赏
  • 举报
回复
调用页面
<!--
分页bean的使用方法:
首先设置:要分页的视图名称,设置当前页码,每页显示的数量,视图的索引字段
然后调用其方法:inital()初始话,就可以了,如果不设置每页显示的数量,那么默认为每页显示20条
-->
<jsp:useBean id="mypage" class="util.db.PageBean"/>
<jsp:setProperty name="mypage" property="tableName" value="courseTeacher"/>
<jsp:setProperty name="mypage" property="curPage"/>
<jsp:setProperty name="mypage" property="perPage" value="10"/>
<jsp:setProperty name="mypage" property="index" value="teacherId"/>
<%
mypage.inital();
CachedRowSet rs=mypage.getRecord();
while(rs.next()){
%>
<tr class="back_color_2">
<td class=mode1> <div align="center"> <%=rs.getString("teacherId")%></div></td>
<td class=mode2> <div align="center"><%=rs.getString("teacherName")%></a></div></td>
<td class=mode2> <div align="center"><%=rs.getString("schoolName")%></div></td>
<td class=mode2><div align="center">
<input name="button" type="button" class="cl2_ipt" value="查看" onClick="go('edit.jsp?teacherId=<%=rs.getString("teacherId")%>')">
</div></td>
</tr>
<%}

%>
</table>
<table width="630" border="0" cellspacing="0" cellpadding="0">
<tr class="back_color_2">
<td width="104" class="mode2">第
<jsp:getProperty name="mypage" property="curPage"/>
页</td>
<td width="119" class="mode2"> <a href="teacher_index.jsp?curPage=<jsp:getProperty name="mypage" property="firstPage"/>">
<img src="../../images/cx_fy_sy.gif" width="13" height="13" border="0"></a><img src="../../images/blank.gif" width="8" height="8">
<a href="teacher_index.jsp?curPage=<jsp:getProperty name="mypage" property="previousPage"/>">
<img src="../../images/cx_fy_syy.gif" width="13" height="13" border="0"></a>
<img src="../../images/blank.gif"> <a href="teacher_index.jsp?curPage=<jsp:getProperty name="mypage" property="nextPage"/>"><img src="../../images/cx_fy_xyy.gif" border="0"></a>
<img src="../../images/blank.gif"> <a href="teacher_index.jsp?curPage=<jsp:getProperty name="mypage" property="lastPage"/>"><img src="../../images/cx_fy_wy.gif" width="13" height="13" border="0"></a>
</td>
<td width="136" class="mode2">
<%
for(int i=mypage.getCurPage();i<=mypage.getTotalPage() && i<mypage.getCurPage()+5;i++){
out.print(" <a href=teacher_index.jsp?curPage="+i+">["+i+"]</a> ");
}

%>
</td>
<td width="78" class="mode2"></td>
<td width="193" class="mode2">总共:
<jsp:getProperty name="mypage" property="totalPage"/></td>
</tr>
</table>
gks_cn 2003-08-20
  • 打赏
  • 举报
回复
package util.db;

import java.sql.*;
import java.util.*;
import sun.jdbc.rowset.CachedRowSet;

/**
*
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class PageBean {
private String tableName; //需要分页的数据库视图名称
private int totalPage;
private int curPage = 1;
private int nextPage;
private int perPage = 20; //默认为每页显示20条
private int previousPage;
private String index;
private int firstPage;
private int lastPage; //视图的索引字段
public int getTotalPage() {
return totalPage;
}

public PageBean() {
}

public void setCurPage(int curPage) {
this.curPage = curPage;
}

public int getCurPage() {
return curPage;
}

public int getNextPage() {
if (getCurPage() == this.totalPage) {
return this.totalPage; //到了最后面
}
else {
return this.getCurPage() + 1;
}
}

public void setPerPage(int perPage) {
this.perPage = perPage;
}

public int getPerPage() {
return perPage;
}

public int getPreviousPage() {
if (this.getCurPage() == 1) {
return 1; //到了最前面
}
else {
return this.getCurPage() - 1;
}
}

public String getTableName() {
return tableName;
}

public void inital() {
int i = 0;
try {
DB mydb = new DB();
String sql = "select count(*) from " + tableName;
CachedRowSet rs = mydb.executeQuery(sql);
System.out.println(sql);
rs.next();
if (rs.getInt(1) % this.getPerPage()==0){
i=rs.getInt(1)/this.getPerPage();
}
else{
i=rs.getInt(1)/this.getPerPage()+1;
}
this.totalPage = i;
this.firstPage=1;
this.lastPage=i;
}
catch (Exception e) {
System.out.println(e.getMessage());
}

}

public CachedRowSet getRecord() {
CachedRowSet rs = null;
DB mydb = new DB();
try {
int i = curPage - 1;
String sql = "select * from " + tableName + " where rownum<=" +
this.perPage + " and " + this.index + " not in (select " + this.index +
" from " + tableName +
" where rownum<=" + perPage + "*" + i + ")";
System.out.println(sql);
rs = mydb.executeQuery(sql);
}
catch (Exception e) {
System.out.println(e.getMessage());
}
return rs;
}

public static void main(String[] args) {
PageBean mypage = new PageBean();

mypage.setIndex("directionId");
mypage.setPerPage(2);
mypage.setCurPage(1);
mypage.setTableName("v_direction");
mypage.inital();
System.out.println("taltal page:" + mypage.getTotalPage());
System.out.println("nextpage:" + mypage.getNextPage());
System.out.println("prepage:" + mypage.getPreviousPage());
CachedRowSet rs = mypage.getRecord();
try {
while (rs.next()) {
System.out.println(rs.getString(1));
}
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}

public String getIndex() {
return index;
}

public void setIndex(String index) {
this.index = index;
}

public void setTableName(String tableName) {
this.tableName = tableName;
}
public int getFirstPage() {
return firstPage;
}
public void setFirstPage(int firstPage) {
this.firstPage = firstPage;
}
public int getLastPage() {
return lastPage;
}
public void setLastPage(int lastPage) {
this.lastPage = lastPage;
}

}
lynx1111 2003-08-20
  • 打赏
  • 举报
回复
http://www.csdn.net/develop/Read_Article.asp?Id=15464
http://www.csdn.net/develop/Read_Article.asp?Id=20368
http://www.csdn.net/develop/Read_Article.asp?Id=18730
http://www.csdn.net/develop/Read_Article.asp?Id=16268

随便贴的自己研究把!
nuanshuidai 2003-08-20
  • 打赏
  • 举报
回复
我的意思也是想要封装的通用一些,而且monbit(泼泼)兄提到的用游标的方法我觉得很好,资源利用合理,而且又保证了速度。
我有上面提到的源码,你们可以看看,如何改进,而又容易使用!
如何给你们,你们想个办法,一个一个的发邮件是不是有点慢了?
nuanshuidai 2003-08-20
  • 打赏
  • 举报
回复
我有源代码,如何给你们?
xqi8 2003-08-20
  • 打赏
  • 举报
回复
你会php,asp等别的分页方法吗,会的话,应该很容易的啊,转化一下就可以了~
monbit 2003-08-20
  • 打赏
  • 举报
回复
哈,你没搞错把。。。。上面那段程序只是定义了接口亚,没有什么具体的实现,内部实现都不知道啦,怎么知道用什么方法实现的?
加载更多回复(19)

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧