数据库分页

用来返回某一页上显示的部分帖子:

/**
    *  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();
       final ArrayList result = new ArrayList();
       final PreparedStatement stat
           = conn.prepareStatement( "select * from threads where category_id=" + category
     + "order by last_update DESC" );
       final ResultSet resultSet = stat.executeQuery();
       if( resultSet.absolute( startIndex ) )
       {
           do{
               result.add( new Thread( resultSet.getString("id"),
                                       resultSet.getString("title" ),
                                       resultSet.getString("content"),
                                       resultSet.getString("author"),
                                       resultSet.getLong("last_update"))
           }while( resultSet.next() && (resultSet.getRow() <= endIndex) );
       }
       stat.close();
       conn.close();
       return result;
   }

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值