免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: RoadStar
打印 上一主题 下一主题

JSP中我想把查询结果分页? [复制链接]

论坛徽章:
0
11 [报告]
发表于 2002-12-12 09:18 |只看该作者

JSP中我想把查询结果分页?

http://www.ambysoft.com/persistenceLayer.pdf

论坛徽章:
0
12 [报告]
发表于 2002-12-12 09:22 |只看该作者

JSP中我想把查询结果分页?

偶用过ASP,那里边从数据库里读出数据来(放在recordset对象里边),就可以在对象里边直接设置pagesize属性,用以分页。不知道jsp是不是也可以如此。

论坛徽章:
0
13 [报告]
发表于 2002-12-12 09:44 |只看该作者

JSP中我想把查询结果分页?

[这个贴子最后由cinc在 2002/12/12 09:48am 编辑]
下面引用由eclipse2002/12/12 09:13am 发表的内容:

JSP数据库操数据分页显示  
--------------------------------------------------------------------------------
源作者:何志强                   人气:3614  
...

对。没错。
下面是从别人那里抄来的一段代码。用来返回某一页上显示的部分帖子:

/**
     *  Get the top-level threads under the given category from start index
     *
     *  @param category - The category
     *  @param startIndex - The start index for retrieveing the records
     *  @param length - The number of records to be tretieved
     *  @return Collection - A collection of threads under the given category
     */
    public Collection findByCategory( final int category,
                                      final int startIndex,
                                      final int endIndex )
                      throws SQLException, CategoryNotFoundException {
        final Connection conn = getConnection()&#59;
        final ArrayList result = new ArrayList()&#59;
        final PreparedStatement stat
            = conn.prepareStatement( "select * from threads where category_id=" + category
      + "order by last_update DESC" )&#59;
        final ResultSet resultSet = stat.executeQuery()&#59;
        if( resultSet.absolute( startIndex ) )
        {
            do{
                result.add( new Thread( resultSet.getString("id&quot,
                                        resultSet.getString("title" ),
                                        resultSet.getString("content&quot,
                                        resultSet.getString("author&quot,
                                        resultSet.getLong("last_update&quot)
            }while( resultSet.next() &amp;&amp; (resultSet.getRow() <= endIndex) )&#59;
        }
        stat.close()&#59;
        conn.close()&#59;
        return result&#59;
    }

Thread 表示一个帖子
category 是论坛编号
resultSet.absolute( startIndex ) 用于定位到开始的那个帖子
结果存放在 ArrayList 中返回。

然后在 jsp 里用 while 循环把 collection 里的内容一个一个输出。

论坛徽章:
0
14 [报告]
发表于 2002-12-12 09:46 |只看该作者

JSP中我想把查询结果分页?

[这个贴子最后由cinc在 2002/12/12 09:49am 编辑]
下面引用由imagegarden2002/12/11 06:26pm 发表的内容:
这种好像是常用的办法。可是,我一直有个疑问?如果查询结果很大的话,怎么办?难道也一起放入数组?
而且,结果太大的select文,在查询时就花时间啊

ResultSet 在执行完 execute 后好像还不会把数据库中的记录都读入内存。

只有在 resultSet.next(), resultSet.getXXX(&quot;column_name&quot 的时候才会去读数据库里的数据

这样说来,查询结果比较多应该不会造成性能的下降。
看我上面的代码段。

论坛徽章:
0
15 [报告]
发表于 2002-12-13 10:58 |只看该作者

JSP中我想把查询结果分页?

[这个贴子最后由pwair在 2002/12/13 11:00am 编辑]

各位:我试了一下,
Select top 20 * from table&#59;
不过好像不好用?标准sql支持select top么?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP