看了一下PETSTORE的分页和各位以前的讨论,对PETSTORE的分页程序有个疑问。在PETSTROE中是用返回PAGE类来分页的,每次只返回一页的数据,但是好像每次调用下一页的时候应该执行类似以下的GET方法,这样岂不是每次都去查询一次数据库(rs = ps.executeQuery();),我觉得应该有把第一次查询结果缓存的做法,还是我的理解有问题?请板桥指教!
public Page getProducts(String categoryID, int start,
int count, Locale l)
throws CatalogDAOSysException {
Connection c = null;
PreparedStatement ps = null;
ResultSet rs = null;
Page ret = null;
try {
c = getDataSource().getConnection();
ps = c.prepareStatement("select a.productid, name, descn "
+ "from (product a join "
+ "product_details b on "
+ "a.productid=b.productid) "
+ "where locale = ? "
+ "and a.catid = ? "
+ "order by name",
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ps.setString(1, l.toString());
ps.setString(2, categoryID);
rs = ps.executeQuery();
if (start >= 0 && rs.absolute(start+1)) {
boolean hasNext = false;
List items = new ArrayList();
do {
items.add(new Product(rs.getString(1).trim(),
rs.getString(2).trim(),
rs.getString(3).trim()));
} while ((hasNext = rs.next()) && (--count > 0));
ret = new Page(items, start, hasNext);
}
else {
ret = Page.EMPTY_PAGE;
}
rs.close();
ps.close();
c.close();
return ret;
}
catch (SQLException se) {
throw new CatalogDAOSysException("SQLException: "
+ se.getMessage());
}
}