JR 精品文章 - 一个可以得到指定页的RowSetDynaClass
AD: jr (at) javaresearch.org


首页 | 动态 | 文章 | FAQ  | 新闻 | 下载 | 代码 | 工作 | 调查 | 术语 | 站点 | 图书 | 论坛 | 帮助 | 全部  

TOP | 交流 | 软件 | 专栏 | 开源 | 译/著 | 源码 | API  | 推荐 | FTP  | 积分 | 统计 | 搜索 | Blog | 我们  
首页 » 研究文集 » JSP/Servlet/JSF 搜索标题相关文章 搜索标题相关文章    评论此文章 发表评论     开始监控此文章 开始监控   加入收藏夹  加入收藏夹
一个可以得到指定页的RowSetDynaClass
tangxingbin 整理   更新:2007-08-06 21:47:54  版本: 1.0   

大家都知道对于RowSetDynaClass,它可以返回所有的记录集,而一个新的类CustomRowSetDynaClass ,却可以根据页,每页的数据条数来得到指定的数据

mport java.io.Serializable;
import java.io.*;
import java.sql.*;
import java.util.*;

import org.apache.commons.beanutils.*;


public class CustomRowSetDynaClass implements DynaClass, Serializable {

//     ----------------------------------------------------------- Constructors

     /**
      * <p>Construct a new {@link RowSetDynaClass} for the specified
      * <code>ResultSet</code>.  The property names corresponding
      * to column names in the result set will be lower cased.</p>
      *
      * @param resultSet The result set to be wrapped
      *
      * @exception NullPointerException if <code>resultSet</code>
      *  is <code>null</code>
      * @exception SQLException if the metadata for this result set
      *  cannot be introspected
      */
     public CustomRowSetDynaClass(ResultSet resultSet) throws SQLException {

       this(resultSet, true);

     }

     /**
      * <p>Construct a new {@link RowSetDynaClass} for the specified
      * <code>ResultSet</code>.  The property names corresponding
      * to the column names in the result set will be lower cased or not,
      * depending on the specified <code>lowerCase</code> value.</p>
      *
      * <p><strong>WARNING</strong> - If you specify <code>false</code>
      * for <code>lowerCase</code>, the returned property names will
      * exactly match the column names returned by your JDBC driver.
      * Because different drivers might return column names in different
      * cases, the property names seen by your application will vary
      * depending on which JDBC driver you are using.</p>
      *
      * @param resultSet The result set to be wrapped
      * @param lowerCase Should property names be lower cased?
      *
      * @exception NullPointerException if <code>resultSet</code>
      *  is <code>null</code>
      * @exception SQLException if the metadata for this result set
      *  cannot be introspected
      */
     public CustomRowSetDynaClass(ResultSet resultSet, boolean lowerCase)
       throws SQLException {

       this(resultSet, 1, -1, lowerCase);

     }

     public CustomRowSetDynaClass(
       ResultSet resultSet,
       int page,
       int pageSize,
       boolean lowerCase)
       throws SQLException {

       if (resultSet == null) {
         throw new NullPointerException();
       }
       this.lowerCase = lowerCase;
       this.page = page;
       this.pageSize = pageSize;

       introspect(resultSet);
       copy(resultSet);

     }

     public CustomRowSetDynaClass(ResultSet resultSet, int page, int pageSize)
       throws SQLException {
       this(resultSet, page, pageSize, true);
     }

     // ----------------------------------------------------- Instance Variables

     /**
      * <p>Flag defining whether column names should be lower cased when
      * converted to property names.</p>
      */
     protected boolean lowerCase = true;

     protected int page = 1;
     protected int pageSize = -1;

     /**
      * <p>The set of dynamic properties that are part of this
      * {@link DynaClass}.</p>
      */
     protected DynaProperty properties[] = null;

     /**
      * <p>The set of dynamic properties that are part of this
      * {@link DynaClass}, keyed by the property name.  Individual descriptor
      * instances will be the same instances as those in the
      * <code>properties</code> list.</p>
      */
     protected Map propertiesMap = new HashMap();

     /**
      * <p>The list of {@link DynaBean}s representing the contents of
      * the original <code>ResultSet</code> on which this
      * {@link RowSetDynaClass} was based.</p>
      */
     protected List rows = new ArrayList();

     // ------------------------------------------------------ DynaClass Methods

     /**
      * <p>Return the name of this DynaClass (analogous to the
      * <code>getName()</code> method of <code>java.lang.Class</code), which
      * allows the same <code>DynaClass</code> implementation class to support
      * different dynamic classes, with different sets of properties.</p>
      */
     public String getName() {

       return (this.getClass().getName());

     }

     /**
      * <p>Return a property descriptor for the specified property, if it
      * exists; otherwise, return <code>null</code>.</p>
      *
      * @param name Name of the dynamic property for which a descriptor
      *  is requested
      *
      * @exception IllegalArgumentException if no property name is specified
      */
     public DynaProperty getDynaProperty(String name) {

       if (name == null) {
         throw new IllegalArgumentException("No property name specified");
       }
       return ((DynaProperty) propertiesMap.get(name));

     }

     /**
      * <p>Return an array of <code>ProperyDescriptors</code> for the properties
      * currently defined in this DynaClass.  If no properties are defined, a
      * zero-length array will be returned.</p>
      */
     public DynaProperty[] getDynaProperties() {

       return (properties);

     }

     /**
      * <p>Instantiate and return a new DynaBean instance, associated
      * with this DynaClass.  <strong>NOTE</strong> - This operation is not
      * supported, and throws an exception.</p>
      *
      * @exception IllegalAccessException if the Class or the appropriate
      *  constructor is not accessible
      * @exception InstantiationException if this Class represents an abstract
      *  class, an array class, a primitive type, or void; or if instantiation
      *  fails for some other reason
      */
     public DynaBean newInstance()
       throws IllegalAccessException, InstantiationException {

       throw new UnsupportedOperationException("newInstance() not supported");

     }

     // --------------------------------------------------------- Public Methods

     /**
      * <p>Return a <code>List</code> containing the {@link DynaBean}s that
      * represent the contents of each <code>Row</code> from the
      * <code>ResultSet</code> that was the basis of this
      * {@link RowSetDynaClass} instance.  These {@link DynaBean}s are
      * disconnected from the database itself, so there is no problem with
      * modifying the contents of the list, or the values of the properties
      * of these {@link DynaBean}s.  However, it is the application's
      * responsibility to persist any such changes back to the database,
      * if it so desires.</p>
      */
     public List getRows() {

       return (this.rows);

     }

     // ------------------------------------------------------ Protected Methods

     /**
      * <p>Copy the column values for each row in the specified
      * <code>ResultSet</code> into a newly created {@link DynaBean}, and add
      * this bean to the list of {@link DynaBean}s that will later by
      * returned by a call to <code>getRows()</code>.</p>
      *
      * @param resultSet The <code>ResultSet</code> whose data is to be
      *  copied
      *
      * @exception SQLException if an error is encountered copying the data
      */
     protected void copy(ResultSet resultSet) throws SQLException {
       int abs = 0;
       int rowsCount = 0;
       int currentPageRows = 0;
       resultSet.last();
       rowsCount = resultSet.getRow();
       if (pageSize != -1) {
         int totalPages = (int) Math.ceil(((double) rowsCount) / pageSize);
         if (page > totalPages)
           page = totalPages;
         if (page < 1)
           page = 1;
         abs = (page - 1) * pageSize;

         //currentPageRows=(page==totalPages?rowsCount-pageSize*(totalPages-1):pageSize);
       } else
         pageSize = rowsCount;
       if (abs == 0)
         resultSet.beforeFirst();
       else
         resultSet.absolute(abs);
       //int 
       while (resultSet.next() && ++currentPageRows <= pageSize) {
         DynaBean bean = new BasicDynaBean(this);
         for (int i = 0; i < properties.length; i++) {
           String name = properties[i].getName();
           bean.set(name, resultSet.getObject(name));
         }
         rows.add(bean);
       }

     }

     /**
      * <p>Introspect the metadata associated with our result set, and populate
      * the <code>properties</code> and <code>propertiesMap</code> instance
      * variables.</p>
      *
      * @param resultSet The <code>resultSet</code> whose metadata is to
      *  be introspected
      *
      * @exception SQLException if an error is encountered processing the
      *  result set metadata
      */
     protected void introspect(ResultSet resultSet) throws SQLException {

       // Accumulate an ordered list of DynaProperties
       ArrayList list = new ArrayList();
       ResultSetMetaData metadata = resultSet.getMetaData();
       int n = metadata.getColumnCount();
       for (int i = 1; i <= n; i++) { // JDBC is one-relative!
         DynaProperty dynaProperty = createDynaProperty(metadata, i);
         if (dynaProperty != null) {
           list.add(dynaProperty);
         }
       }

       // Convert this list into the internal data structures we need
       properties =
         (DynaProperty[]) list.toArray(new DynaProperty[list.size()]);
       for (int i = 0; i < properties.length; i++) {
         propertiesMap.put(properties[i].getName(), properties[i]);
       }

     }

     /**
      * <p>Factory method to create a new DynaProperty for the given index
      * into the result set metadata.</p>
      * 
      * @param metadata is the result set metadata
      * @param i is the column index in the metadata
      * @return the newly created DynaProperty instance
      */
     protected DynaProperty createDynaProperty(
       ResultSetMetaData metadata,
       int i)
       throws SQLException {

       String name = null;
       if (lowerCase) {
         name = metadata.getColumnName(i).toLowerCase();
       } else {
         name = metadata.getColumnName(i);
       }
       String className = null;
       try {
         className = metadata.getColumnClassName(i);
       } catch (SQLException e) {
         // this is a patch for HsqlDb to ignore exceptions
         // thrown by its metadata implementation
       }

       // Default to Object type if no class name could be retrieved
       // from the metadata
       Class clazz = Object.class;
       if (className != null) {
         clazz = loadClass(className);
       }
       return new DynaProperty(name, clazz);

     }

     /**
      * <p>Loads and returns the <code>Class</code> of the given name.
      * By default, a load from the thread context class loader is attempted.
      * If there is no such class loader, the class loader used to load this
      * class will be utilized.</p>
      *
      * @exception SQLException if an exception was thrown trying to load
      *  the specified class
      */
     protected Class loadClass(String className) throws SQLException {

       try {
         ClassLoader cl = Thread.currentThread().getContextClassLoader();
         if (cl == null) {
           cl = this.getClass().getClassLoader();
         }
         return (cl.loadClass(className));
       } catch (Exception e) {
         throw new SQLException(
           "Cannot load column class '" + className + "': " + e);
       }

     }

}




版权声明  
本篇文章对您是否有帮助?  投票:         投票结果:     8       0
作者其它文章: 作者全部文章
评论人:yanglenovo 发表时间: Tue Aug 21 00:04:37 CST 2007
好难![good]

这个文章共有 1 条评论
主题: Apusic OperaMasks能否开创Java Web新格局? 上一篇文章
返回文章列表 返回〔JSP/Servlet/JSF〕
下一篇文章 主题: 学习笔记:ServletContext接口


文字广告链接
        自主、快速定制基于JAVA的B/S业务系统          重量级企业在线自定义WEB报表平台
        Excel制表、零代码发布、打印、图表结合——快逸报表,免费、稳定、功能强大的java工具
        技术圈: 关于Java、dotNet、PHP、Ruby、奇客、Web2.0等更多资讯博客精选文章

关于 JR  |  版权声明  |  联系我们 

©2002-2006 JR 版权所有 沪ICP备05019622号