posts - 16, comments - 6, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          JDBC 分頁查詢

          Posted on 2009-09-11 10:57 java-flying 閱讀(1018) 評論(0)  編輯  收藏
          以Oracle 數據庫為例:
          1.利用游標分頁
           1public void run() {
           2        // 總記錄數
           3        Integer total = getTotalCount();
           4        // 查詢最大行數
           5        Integer maxCount = 10000;
           6        // 總頁數
           7        Integer totalPage = (total - 1/ maxCount + 1;
           8        // 開始行號
           9        Integer startNo = 1;
          10        String sql = "select m.id from mn00_tbl_music m";
          11        Connection conn = null;
          12        PreparedStatement stat = null;
          13        ResultSet rs = null;
          14        try {
          15            conn = dataSource.getConnection();
          16            // conn.prepareStatement(sql,游標類型,能否更新記錄);
          17            // 游標類型:
          18            // ResultSet.TYPE_FORWORD_ONLY:只進游標
          19            // ResultSet.TYPE_SCROLL_INSENSITIVE:可滾動。但是不受其他用戶對數據庫更改的影響。
          20            // ResultSet.TYPE_SCROLL_SENSITIVE:可滾動。當其他用戶更改數據庫時這個記錄也會改變。
          21            // 能否更新記錄:
          22            // ResultSet.CONCUR_READ_ONLY,只讀
          23            // ResultSet.CONCUR_UPDATABLE,可更新
          24            stat = conn.prepareStatement(sql,
          25                    ResultSet.TYPE_SCROLL_INSENSITIVE,
          26                    ResultSet.CONCUR_READ_ONLY);
          27            rs = stat.executeQuery();
          28
          29            for (int i = 1; i <= totalPage; i++{
          30                // 將游標移動到指定行
          31                //Calling absolute(1) is the same as calling first(). Calling absolute(-1) is the same as calling last(). 
          32                //rs.first()相當于rs.absolute(1)
          33                rs.absolute(startNo);
          34                // 直接得出該行記錄
          35                Integer musicId = rs.getInt("id");
          36                logger.info(startNo + "-" + musicId + "~~~~~~~~~`~~" + maxCount
          37                        + "==");
          38                startNo += maxCount;
          39            }

          40        }
           catch (SQLException e) {
          41            logger.error("數據庫查詢錯誤!" + e + "," + sql);
          42        }
           finally {
          43            DaoUtil.close(conn, stat, rs);
          44        }

          45    }

          2.利用SQL語句分頁
           1從數據庫表中第M條記錄開始檢索N條記錄
           2
           3  SELECT *
           4
           5  FROM (SELECT ROWNUM r,t1.* From 表名稱 t1 where rownum < M + N) t2
           6
           7  where t2.r >= M
           8
           9例如從表Sys_option(主鍵為sys_id)中從10條記錄還是檢索20條記錄,語句如下:
          10
          11  SELECT *
          12
          13  FROM (SELECT ROWNUM R,t1.* From Sys_option where rownum < 30 ) t2
          14
          15  Where t2.R >= 10

          Hibernate 分頁

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 平武县| 宿迁市| 方正县| 禄劝| 交口县| 阳城县| 龙岩市| 通道| 清原| 莆田市| 会昌县| 台州市| 新巴尔虎右旗| 温宿县| 于田县| 黔西县| 凤庆县| 揭阳市| 汕尾市| 穆棱市| 包头市| 塘沽区| 天柱县| 建平县| 邛崃市| 宝山区| 班玛县| 依安县| 白玉县| 静海县| 兴山县| 尼木县| 额尔古纳市| 万宁市| 永宁县| 河北区| 吐鲁番市| 家居| 庆安县| 手机| 海原县|