/**
* 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 里的内容一个一个输出。