DAO层实现分页的方法.代码作为记录.

当初看到这个代码没有深入研究,为方便用了一简便的servlet端分页,导致很严重后果.仅记录后台代码于此: public class PageDao { public PageDao() { } public List pageData(int cur
摘要由CSDN通过智能技术生成

当初看到这个代码没有深入研究,为方便用了一简便的servlet端分页,导致很严重后果.

仅记录后台代码于此:

 

public class PageDao {

 public PageDao() {
 }


 public List pageData(int currentPage, int pageCount) {
  List results = new ArrayList();

  String tableName = "demo_userinfo_pages";
  ResultSet rs = null;
  StringBuffer sql = new StringBuffer("");
  Statement stmt = null;
  Connection conn = null;
  try {
   int count = recordCount(); 
   int totalPage = (int) Math.ceil(1.0 * count / pageCount); 
   int start = 0;
   int end = 0;
   if (currentPage <= 0) {
    currentPage = 1;
    start = 1;
    end = currentPage * pageCount;
   }

   if (currentPage > totalPage) {
    currentPage = totalPage;
    return results;
   }
   if ((currentPage - 1) * pageCount >= 0) {
    start = (currentPage - 1) * pageCount + 1;
    end = currentPage * pageCount;
   }

   sql.append("SELECT * ").append("FROM (SELECT a.*,ROWNUM rn ")
     .append(" FROM (SELECT * ").append("  FROM " + tableName)
     .append("  ORDER BY   asc) a").append("  )").append(
       " WHERE rn >= " + start + " and rn <=" + end);
   

   conn = DBManager.getConnection();
   stmt = conn.createStatement();
   rs = stmt.executeQuery(sql.toString());
   while (rs.next()) {
    UserForm bean = new UserForm();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值