分页技术(4)

来源:互联网 发布:网络推手公司哪个好 编辑:程序博客网 时间:2024/05/22 03:31
  1.     
  2.     public  RowSetPage executeQuery() throws SQLException{
  3.         System.out.println("executeQueryUsingPreparedStatement");
  4.         Connection conn DBUtil.getConnection();
  5.         PreparedStatement pst null;
  6.         ResultSet rs null;
  7.         try{
  8.             pst conn.prepareStatement(this.countSQL);
  9.             setParams(pst);
  10.             rs =pst.executeQuery();
  11.             if (rs.next()){
  12.                 totalCount rs.getInt(1);
  13.             else {
  14.                 totalCount 0;
  15.             }
  16.             rs.close();
  17.             pst.close();
  18.             if (totalCount return RowSetPage.EMPTY_PAGE;
  19.             pst conn.prepareStatement(this.querySQL);
  20.             System.out.println(querySQL);
  21.             pst.setFetchSize(this.pageSize);
  22.             setParams(pst);
  23.             rs =pst.executeQuery();
  24.             //rs.setFetchSize(pageSize);
  25.             this.rowSet populate(rs);
  26.             rs.close();
  27.             rs null;
  28.             pst.close();
  29.             pst null;
  30.             this.rowSetPage new RowSetPage(this.rowSet,startIndex,totalCount,pageSize);
  31.             return this.rowSetPage;
  32.         }catch(SQLException sqle){
  33.             //System.out.println("executeQuery SQLException");
  34.             sqle.printStackTrace();
  35.             throw sqle;
  36.         }catch(Exception e){
  37.             e.printStackTrace();
  38.             throw new RuntimeException(e.toString());
  39.         }finally{
  40.             //System.out.println("executeQuery finally");
  41.             DBUtil.close(rs, pst, conn);
  42.         }
  43.     }
  44.     
  45.     protected abstract RowSet populate(ResultSet rs) throws SQLException;
  46.     
  47.     public javax.sql.RowSet getRowSet(){
  48.         return this.rowSet;
  49.     }
  50.     
  51.     public RowSetPage getRowSetPage() {
  52.         return this.rowSetPage;
  53.     }
  54.     
  55.     public void close(){
  56.         //因为数据库连接在查询结束或发生异常时即关闭,此处不做任何事情
  57.         //留待扩充。
  58.     }
  59.     private class BoundParam {
  60.         int index;
  61.         Object value;
  62.         int sqlType;
  63.         int scale;
  64.         public BoundParam(int index, Object value) {
  65.             this(index, value, java.sql.Types.OTHER);
  66.         }
  67.         public BoundParam(int index, Object value, int sqlType) {
  68.             this(index, value, sqlType, 0);
  69.         }
  70.         public BoundParam(int index, Object value, int sqlType, int scale) {
  71.             this.index index;
  72.             this.value value;
  73.             this.sqlType sqlType;
  74.             this.scale scale;
  75.         }
  76.         public boolean equals(Object obj){
  77.             if (obj!=null && this.getClass().isInstance(obj)){
  78.                 BoundParam bp (BoundParam)obj;
  79.                 if (this.index==bp.index) return true;
  80.             }
  81.             return false;
  82.         }
  83.     }
  84. }
  85. ///////////////////////////////////
  86. //
  87. //  PagedStatementOracleImpl.java
  88. //  author: evan_zhao@hotmail.com
  89. //
  90. ///////////////////////////////////
  91. package page;
  92. import java.sql.ResultSet;
  93. import java.sql.SQLException;
  94. import javax.sql.RowSet;
  95. import oracle.jdbc.rowset.OracleCachedRowSet;
  96. public class PagedStatementOracleImpl extends PagedStatement {
  97.     
  98.     public PagedStatementOracleImpl(String sql){
  99.         super(sql);
  100.     }
  101.     
  102.     public PagedStatementOracleImpl(String sql, int pageNo){
  103.         super(sql, pageNo);
  104.     }
  105.     
  106.     public PagedStatementOracleImpl(String sql, int pageNo, int pageSize){
  107.         super(sql, pageNo, pageSize);
  108.     }
  109.     
  110.     protected String intiQuerySQL(String sql, int startIndex, int size){
  111.         StringBuffer querySQL new StringBuffer();
  112.         if (size != super.MAX_PAGE_SIZE) {
  113.             querySQL.append("select from (select my_table.*,rownum as my_rownum from(")
  114.                     .append(  sql)
  115.                     .append(") my_table where rownum<").append(startIndex size)
  116.                     .append(") where my_rownum>=").append(startIndex);
  117.         else {
  118.             querySQL.append("select from (select my_table.*,rownum as my_rownum from(")
  119.                     .append(sql)
  120.                     .append(") my_table ")
  121.                     .append(") where my_rownum>=").append(startIndex);
  122.         }
  123.         return querySQL.toString();
  124.     }
  125.     
  126.     protected  RowSet populate(ResultSet rs) throws SQLException{
  127.         OracleCachedRowSet ocrs new OracleCachedRowSet();
  128.         ocrs.populate(rs);
  129.         return ocrs;
  130.     }
  131. }
原创粉丝点击