这段代码也许对你有用
源码来自:
http://www.blogjava.net/supercrsky/archive/2008/09/23/230723.html
/**
* 查询用户(带分页)
*
* @param startNo
* 开始条数
* @param maxCount
* 最大记录数
* @return
*/
public List<User> getAll(int startNo, int maxCount)
{
Connection conn = DBConnection.getConn();
String sql = "select * from person";
List<User> users = new ArrayList<User>();
try
{
if (this.getAllSize() <= 0)
return null;
PreparedStatement pstmt = conn.prepareStatement(sql,
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
// 最大查询到第几条记录
pstmt.setMaxRows(startNo + maxCount - 1);
ResultSet rs = pstmt.executeQuery();
// 将游标移动到第一条记录
rs.first();
// 游标移动到要输出的第一条记录
rs.relative(startNo - 2);
while (rs.next())
{
User user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setAge(rs.getInt("age"));
user.setRemark(rs.getString("remark"));
users.add(user);
}
} catch (SQLException e)
{
e.printStackTrace();
} finally
{
try
{
conn.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
return users;
}