最好的办法是利用sql语句进行分页,这样每次查询出的结果集中就只包含某页的数据内容。再sql语句无法实现分页的情况下,可以考虑对大的结果集通过游标定位方式来获取某页的数据。
sql语句分页,不同的数据库下的分页方案各不一样,下面是主流的三种数据库的分页sql:
sql server:
String sql =
"select top " + pageSize + " * from students where id not in" +
"(select top " + pageSize * (pageNumber-1) + " id from students order by id)" +
"order by id";
mysql:
String sql =
"select * from students order by id limit " + pageSize*(pageNumber-1) + "," + pageSize;
oracle:
String sql =
"select * from " +
(select *,rownum rid from (select * from students order by postime desc) where rid<=" + pagesize*pagenumber + ") as t" +
"where t>" + pageSize*(pageNumber-1);