JSP怎样利用List实现分页?
发布网友
发布时间:2022-04-23 20:17
我来回答
共1个回答
热心网友
时间:2022-04-23 21:47
/****给入参数 当前页 和每页显示的记录数 可以返回 想要的集合***/
public Collection getBooks(int page, int n) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
int sumRs;
int sumPage;
int currRs;
ArrayList<Bean> booklist = new ArrayList<Bean>();
try {
conn = getConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery("select * from 你的表");
rs.last();
sumRs = rs.getRow();
sumPage = (sumRs % n) == 0 ? (sumRs / n) : (sumRs / n + 1);
currRs = (page - 1) * n + 1;
rs.absolute(currRs);
for (int i = 1; i <= n; i++) {
Bean book = new Bean(rs.getInt(1), rs.getString(2), rs
.getString(3), rs.getString(4), rs.getString(5), rs
.getFloat(6), rs.getInt(7), rs.getString(8), rs
.getString(9));
booklist.add(book);
rs.next();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
closeResultSet(rs);
closeStatement(stmt);
closeConnection(conn);
}
return booklist;
}