67,513
社区成员
发帖
与我相关
我的任务
分享
//每页显示列表的数量
int pageSize = 6;
String strPagePos = request.getParameter("pageNo");
//显示第几页
int pageNo;
if(strPagePos == null || strPagePos.equals("")){
pageNo = 1;
}else{
try{
pageNo = Integer.parseInt(strPagePos.trim());
}catch(NumberFormatException e){
pageNo = 1;
}
//判断传入参数
if(pageNo <= 0) pageNo = 1;
}
//查询一共有多少条数据
Connection conn = DB.getConn();
Statement count = conn.createStatement();
ResultSet rsCount = count.executeQuery("select count(*)from article where rootid="+id);
rsCount.next();
int RCount = rsCount.getInt(1);
//计算一共有多少页面
int pageCounts = RCount % pageSize == 0 ? RCount / pageSize : RCount /pageSize + 1 ;
//查询的位置大于页面的最大数
if(pageNo > pageCounts)pageNo = pageCounts;
//查询的起始位置
int startPos = (pageNo - 1) * pageSize;
List<Article> articles = new ArrayList<Article>();
//使用数据库分页显示
String sql = "select * from article where rootid=? order by pdate asc limit ?,?";
PreparedStatement pstmt = DB.getPstmt(conn,sql);
pstmt.setInt(1,id);
pstmt.setInt(2,startPos);
pstmt.setInt(3,pageSize);
//System.out.println(sql);
ResultSet rs = DB.getRsQuery(pstmt);
while(rs.next()){
Article a = new Article();
a.setId(rs.getInt("id"));
a.setRootid(rs.getInt("rootid"));
a.setPid(rs.getInt("pid"));
a.setTitle(rs.getString("title"));
a.setPdate(rs.getTimestamp("pdate"));
a.setCon(rs.getString("con"));
a.setIsleaf(rs.getInt("isleaf") == 0 ? true:false);
articles.add(a);
}