⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pagedstatement.java

📁 一个完整的代码管理系统的源代码
💻 JAVA
字号:
/*
 * Copyright (c) 2008-2010 Tanming1003 Inc.
 * All rights reserved.
 * 
 * tanming1003<tanming1003@163.com>
 * 
 * 
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions 
 * are met:
 * 
 * Redistributions of source code must retain the above copyright 
 * notice, this list of conditions and the following disclaimer. 
 * Redistributions in binary form must reproduce the above copyright 
 * notice, this list of conditions and the following disclaimer in the 
 * documentation and/or other materials provided with the distribution. 
 * Neither the name of tanming1003 nor the names of its contributors 
 * may be used to endorse or promote products derived from this 
 * software without specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 * REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 */
package hunnu.edu.cn.product.common.splitPages;

import hunnu.edu.cn.product.common.db.DBProcess;
import hunnu.edu.cn.product.common.db.DBWrapper;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.sql.RowSet;

/**
 * @author tanming1003@163.com
 * @date 2008-10-13
 * @time 下午04:03:54
 * @project_name Product
 * @package_name hunnu.edu.cn.product.common.splitPages
 * @file_name    PagedStatement.java
 * @version      1.0
 */
public abstract class PagedStatement
{
     protected String countSql,querySql;
     protected int pageNo,pageSize,start,totalSize;
     protected RowSet rowSet;
     protected RowSetPage rowSetPage;
     protected List<Param> params;
     protected ResultSet rs ;
     
     /**
      * 构造一查询出所有数据的PagedStatement 
      * @param sql  查询语句
      */
     public PagedStatement(String sql)
     {
    	 this(sql,1);
     }
     
     /**
      * 构造一查询出所有数据的PagedStatement 
      * @param sql      查询语句
      * @param pageNo   页码
      */
     public PagedStatement(String sql,int pageNo)
     {
    	this(sql,pageNo,Page.DEFAULT_PAGE_SIZE); 
     }
     
     /**
      * 构造一查询出所有数据的PagedStatement 
      * @param sql        查询语句
      * @param pageNo     页码
      * @param pageSize   页面数据大小
      */
     @SuppressWarnings("unchecked")
	 public PagedStatement(String sql,int pageNo,int pageSize)
     {
    	 this.pageNo=pageNo;
    	 this.pageSize=pageSize;
    	 this.start=Page.EMPTY_PAGE.getStartOfAnyPage(pageNo,pageSize);
    	 this.params=Collections.synchronizedList(new LinkedList());
    	 this.countSql="select count(*) from ("+sql+")";
    	 this.querySql=initQueryString(sql,this.start,this.pageSize);
     }
     
     /** 
      *生成查询一页数据的sql语句 
      *@param sql 原查询语句 
      *@start     开始记录位置 
      *@size      需要获取的记录数 
      */ 
     public abstract String initQueryString(String sql,int start,int size);
     
     /** 
     *使用给出的对象设置指定参数的值 
     *@param index 第一个参数为1,第二个为2,。。。 
     *@param obj 包含参数值的对象 
     */ 
     public void setObject(int index, Object obj) throws SQLException
     { 
        Param p = new Param(index, obj); 
        params.remove(p); 
        params.add( p); 
     } 
     
     /** 
      *使用给出的对象设置指定参数的值 
      *@param index 第一个参数为1,第二个为2,。。。 
      *@param obj 包含参数值的对象 
      *@param targetSqlType 参数的数据库类型 
      */ 
      public void setObject(int index, Object obj, int targetSqlType) throws SQLException
      { 
        Param bp = new Param(index, obj, targetSqlType); 
        params.remove(bp); 
        params.add(bp ); 
      } 

      /** 
      *使用给出的对象设置指定参数的值 
      *@param index 第一个参数为1,第二个为2,。。。 
      *@param obj 包含参数值的对象 
      *@param targetSqlType 参数的数据库类型(常量定义在java.sql.Types中) 
      *@param scale 精度,小数点后的位数 
      * (只对targetSqlType是Types.NUMBER或Types.DECIMAL有效,其它类型则忽略) 
      */ 
      public void setObject(int index, Object obj, int targetSqlType, int scale) throws SQLException
      { 
        Param bp = new Param(index, obj, targetSqlType, scale) ; 
        params.remove(bp); 
        params.add(bp); 
      } 
      
      /** 
       *使用给出的字符串设置指定参数的值 
       *@param index 第一个参数为1,第二个为2,。。。 
       *@param str 包含参数值的字符串 
       */ 
       public void setString(int index, String str)throws SQLException{ 
         Param bp = new Param(index, str) ; 
         params.remove(bp); 
         params.add(bp); 
       } 

       /** 
       *使用给出的字符串设置指定参数的值 
       *@param index 第一个参数为1,第二个为2,。。。 
       *@param timestamp 包含参数值的时间戳 
       */ 
       public void setTimestamp(int index, Timestamp timestamp)throws SQLException
       { 
         Param bp = new Param(index, timestamp) ; 
         params.remove(bp); 
         params.add( bp ); 
       } 

       /** 
       *使用给出的整数设置指定参数的值 
       *@param index 第一个参数为1,第二个为2,。。。 
       *@param value 包含参数值的整数 
       */ 
       public void setInt(int index, int value)throws SQLException
       { 
         Param bp = new Param(index, new Integer(value)) ; 
         params.remove(bp); 
         params.add( bp ); 
       } 

       /** 
       *使用给出的长整数设置指定参数的值 
       *@param index 第一个参数为1,第二个为2,。。。 
       *@param value 包含参数值的长整数 
       */ 
       public void setLong(int index, long value)throws SQLException{ 
         Param bp = new Param(index, new Long(value)) ; 
         params.remove(bp); 
         params.add( bp ); 
       } 

       /** 
       *使用给出的双精度浮点数设置指定参数的值 
       *@param index 第一个参数为1,第二个为2,。。。 
       *@param value 包含参数值的双精度浮点数 
       */ 
       public void setDouble(int index, double value)throws SQLException
       {   
         Param bp = new Param(index, new Double(value))   ; 
         params.remove(bp); 
         params.add( bp); 
       } 

       /** 
       *使用给出的BigDecimal设置指定参数的值 
       *@param index 第一个参数为1,第二个为2,。。。 
       *@param bd 包含参数值的BigDecimal 
       */ 
       public void setBigDecimal(int index, BigDecimal bd)throws SQLException
       { 
         Param bp =   new Param(index, bd )   ; 
         params.remove(bp); 
         params.add( bp); 
       } 
       
       /**
        * 设置参数列表
        * @param pst             preparedStatement,sql查询句柄
        * @throws SQLException   如果出现异常,则抛出sql异常
        */
       @SuppressWarnings({ "unchecked" })
	   private void setParams(PreparedStatement pst) throws SQLException
       { 
    	    if (pst==null || this.params==null || this.params.size()==0 ) return ; 
    	    Param param; 
    	    for (Iterator itr = this.params.iterator();itr.hasNext();)
    	    { 
    	        param = (Param) itr.next(); 
    	        if (param==null) continue; 
    	        if (param.sqlType == java.sql.Types.OTHER)
    	        { 
    	          pst.setObject(param.index, param.value); 
    	        }
    	        else
    	        { 
    	          pst.setObject(param.index, param.value, param.sqlType, param.scale); 
    	        } 
    	    } 
        }
       
       /** 
        *将ResultSet数据填充进CachedRowSet 
        */ 
        protected abstract RowSet populate(ResultSet rs) throws SQLException; 

        /** 
        *取封装成RowSet查询结果 
        *@return RowSet 
        */ 
        public javax.sql.RowSet getRowSet()
        { 
          return this.rowSet; 
        } 


        /** 
        *取封装成RowSetPage的查询结果 
        *@return RowSetPage 
        */ 
        public RowSetPage getRowSetPage() 
        { 
          return this.rowSetPage; 
        } 

        /** 
         * 执行查询取得一页数据,执行结束后关闭数据库连接 
         * @return RowSetPage 
         * @throws SQLException 
         */ 
         public RowSetPage executeQuery() throws SQLException
         { 
            DBProcess process=new DBWrapper();
            Connection conn=process.getConnection(); 
            PreparedStatement pst = null; 
             
            try
            { 
               pst = conn.prepareStatement(this.countSql); 
               setParams(pst); 
               rs =pst.executeQuery(); 
               if (rs.next())
               { 
                 totalSize = rs.getInt(1); 
               } 
               else 
               { 
                 totalSize = 0; 
               } 

               rs.close(); 
               pst.close(); 

               if (totalSize < 1 ) return RowSetPage.EMPTY_PAGE; 

               pst = conn.prepareStatement(this.querySql); 
           //    pst.setFetchSize(this.pageSize); 
               pst.setMaxRows(this.pageSize);
               
               setParams(pst); 
               rs =pst.executeQuery(); 
           
    

               this.rowSet = populate(rs); 
             

               rs.close(); 
               rs = null; 
               pst.close(); 
               pst = null; 

               this.rowSetPage = new RowSetPage(this.rowSet,start,totalSize,pageSize); 
               return this.rowSetPage; 
           }
           catch(SQLException sqle)
           { 
               sqle.printStackTrace(); 
               throw sqle; 
           }
           catch(Exception e)
           { 
               e.printStackTrace(); 
               throw new RuntimeException(e.toString()); 
           }
           finally
           { 
               if(conn!=null)
        	     process.closeConnection(conn);
           } 
        
         } 
         public ResultSet   getResultSet(){
      	   
      	   return this.rs;
         }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -