一:建Dao类
package dao;
import java.sql.ResultSet; import java.util.List; import java.util.Vector;
import bean.FwxxBean; import db.DBUtil;
public class FwxxDao {
private int pageNum=5; private int currentPage;
public int getCurrentPage() { return currentPage; }
public List getAll(int currentPage) { List list=new Vector(); this.currentPage=currentPage; try { DBUtil db=new DBUtil(); String sqlText= "select top "+pageNum+" * from view_fwxx where fwid not in(select top "+(currentPage-1)*5+" fwid from view_fwxx)"; ResultSet rs=db.executeQ(sqlText, null); while(rs.next()) { FwxxBean bean=new FwxxBean(); bean.setFwid(rs.getInt("fwid")); bean.setShi(rs.getInt("shi")); bean.setTing(rs.getInt("ting")); list.add(bean); } db.closeAll(); }catch(Exception ex) { ex.printStackTrace(); }finally { return list; } } //获得总记录数 public int getRowTotal() { int rowTotal=0; try { DBUtil db=new DBUtil(); ResultSet rs=db.executeQ("select Count(*) from view_fwxx", null); rs.next(); rowTotal=rs.getInt(1); db.closeAll(); }catch(Exception ex) { ex.printStackTrace(); }finally { return rowTotal; } } //获得总页数 public int getPageTotal() { int totalPage=this.getRowTotal()/pageNum; if(this.getRowTotal()%pageNum!=0) { totalPage++; } System.out.println(this.getRowTotal()+" "+totalPage); return totalPage; } } |
二:Jsp页面调用Dao方法
int pageNum=1; String pageN=request.getParameter("currentPage"); if(pageN!=null) pageNum=Integer.parseInt(pageN); FwxxDao dao=new FwxxDao(); List lst=dao.getAll(pageNum); int current_page=dao.getCurrentPage(); int page_total=dao.getPageTotal(); pageContext.setAttribute("fwxx",lst); pageContext.setAttribute("current_page", current_page); pageContext.setAttribute("page_total", page_total); |
三:分页
<tr> <td>标题</td> <td>月租金</td> <td>发布日期</td> </tr> <tr> <td colspan="3" > <hr> </td> </tr> <logic:present name="fwxx" scope="page"> <logic:iterate id="data" name="fwxx" scope="page"> <tr> <td>${data.title }</td> <td>${data.zj }</td> <td>${data.date }</td> </tr> </logic:iterate> </logic:present> <tr> <td colspan="3" > <hr> </td> </tr> <tr> <td colspan="3" > <a href="list.jsp?currentPage=1">首页</a> <a href="list.jsp?currentPage=${current_page-1 }">上一页</a> <a href="list.jsp?currentPage=${current_page+1 }">下一页</a> <a href="list.jsp?currentPage=${page_total }">尾页</a> </td> </tr> |