草窩兒
          我的地盤兒
          posts - 4,  comments - 13,  trackbacks - 0

          package com.kingstargroup.eqa.pageshow.action;


          import java.util.List;

          import org.apache.struts.action.Action;
          import org.springframework.jdbc.core.JdbcTemplate;

          import com.kingstargroup.framework.dao.Page;

          public abstract class AbstractPageShowAction extends Action {
           protected final int defaultPageSize = 50;
           
           private JdbcTemplate jdbcTemplate;

           public JdbcTemplate getJdbcTemplate() {
            return jdbcTemplate;
           }

           public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
            this.jdbcTemplate = jdbcTemplate;
           }
           
           protected Page queryForPage(String sql, int pageNo, int pageCount, Object[] args) {
            String strSql = "";
            int startNo = Page.getStartOfPage(pageNo, pageCount);
            int endNo = startNo + pageCount;
            int argsCount = args.length;
            int totleCount = queryForCount(sql, args);
            Object[] new_args;
            
            if(startNo==0){
             strSql = "select * from (" + sql + ") where rownum <= ?";
             new_args = new Object[argsCount+1];
            }
            else{
             strSql = "select * from (select row_.*,rownum rownum_ from (" + sql + ") row_ where rownum <= ?) where rownum_>?";
             new_args = new Object[argsCount+2];
             new_args[argsCount + 1] = new Integer(startNo);
            }
            for(int i=0; i<argsCount; i++){
             new_args[i] =args[i];
            }
            new_args[argsCount] = new Integer(endNo);
            List list = jdbcTemplate.queryForList(strSql, new_args);   
            
            return new Page(startNo, totleCount, pageCount, list);
           }
           
           private int queryForCount(String sql, Object[] args){
            String strSql = "select count(rowid) from ( " + sql +  ")";
            return jdbcTemplate.queryForInt(strSql, args);
           }

          }





          package com.kingstargroup.framework.dao;

          import java.util.ArrayList;
          import java.util.Collection;

          import com.kingstargroup.framework.dao.hibernate.BaseHibernateDao;

          /**
           * 分頁對(duì)象. 包含數(shù)據(jù)及分頁信息.
           *
           * @author ajax
           */
          public class Page implements java.io.Serializable {
           /**
            * 當(dāng)前頁第一條數(shù)據(jù)的位置,從0開始
            */
           private int start;

           /**
            * 每頁的記錄數(shù)
            */
           private int pageSize = BaseHibernateDao.DEFAULT_PAGE_SIZE;

           /**
            * 當(dāng)前頁中存放的記錄
            */
           private Object data;

           /**
            * 總記錄數(shù)
            */
           private int totalCount;

           private int size;

           /**
            * 構(gòu)造方法,只構(gòu)造空頁
            */
           public Page() {
            this(0, 0, BaseHibernateDao.DEFAULT_PAGE_SIZE, new ArrayList());
           }

           /**
            * 默認(rèn)構(gòu)造方法
            *
            * @param start
            *            本頁數(shù)據(jù)在數(shù)據(jù)庫中的起始位置
            * @param totalSize
            *            數(shù)據(jù)庫中總記錄條數(shù)
            * @param pageSize
            *            本頁容量
            * @param data
            *            本頁包含的數(shù)據(jù)
            */
           public Page(int start, int totalSize, int pageSize, Object data) {
            this.pageSize = pageSize;
            this.start = start;
            this.totalCount = totalSize;
            this.data = data;
            if (data instanceof Collection)
             this.size = ((Collection) data).size();
            else
             this.size = ((Object[]) data).length;
           }

           /**
            * 取數(shù)據(jù)庫中包含的總記錄數(shù)
            */
           public int getTotalSize() {
            return this.totalCount;
           }

           /**
            * 取總頁數(shù)
            */
           public int getTotalPageCount() {
            if (totalCount % pageSize == 0)
             return totalCount / pageSize;
            else
             return totalCount / pageSize + 1;
           }

           /**
            * 取每頁數(shù)據(jù)容量
            */
           public int getPageSize() {
            return pageSize;
           }

           /**
            * 當(dāng)前頁中的記錄
            */
           public Object getResult() {
            return data;
           }

           /**
            * 取當(dāng)前頁碼,頁碼從1開始
            */
           public int getCurrentPageNo() {
            return (start / pageSize) + 1;
           }

           /**
            * 是否有下一頁
            */
           public boolean hasNextPage() {
            return (this.getCurrentPageNo() < this.getTotalPageCount() - 1);
           }

           /**
            * 是否有上一頁
            */
           public boolean hasPreviousPage() {
            return (this.getCurrentPageNo() > 1);
           }

           /**
            * 獲取任一頁第一條數(shù)據(jù)的位置,每頁條數(shù)使用默認(rèn)值
            */
           protected static int getStartOfPage(int pageNo) {
            return getStartOfPage(pageNo, BaseHibernateDao.DEFAULT_PAGE_SIZE);
           }

           /**
            * 獲取任一頁第一條數(shù)據(jù)的位置,startIndex從0開始
            */
           public static int getStartOfPage(int pageNo, int pageSize) {
            return (pageNo - 1) * pageSize;
           }

           public int getSize() {
            return this.size;
           }

          }

          posted on 2007-04-04 13:16 芨芨草 閱讀(2187) 評(píng)論(1)  編輯  收藏 所屬分類: JAVA

          FeedBack:
          # re: JDBC分頁(Oracle 10g)
          2007-05-17 10:11 | 我心依舊
          很不錯(cuò).JDBC運(yùn)行效率高...  回復(fù)  更多評(píng)論
            

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          想讓草兒飛上天 ...

          <2007年5月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          Hibernate

          Oracle

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 蒙城县| 江孜县| 常州市| 乾安县| 札达县| 额济纳旗| 沁水县| 天全县| 新营市| 嘉善县| 新沂市| 平潭县| 清水河县| 滨州市| 来凤县| 诏安县| 宣武区| 洛扎县| 隆安县| 太和县| 康乐县| 金华市| 延庆县| 山东省| 前郭尔| 胶州市| 盈江县| 涿鹿县| 荣昌县| 运城市| 获嘉县| 隆德县| 新巴尔虎右旗| 抚顺县| 乡城县| 米脂县| 南部县| 黄大仙区| 格尔木市| 陈巴尔虎旗| 芜湖市|