commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject cvs commit: jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils ResultSetIterator.java ResultSetIteratorV1.java DbUtils.java
Date Sun, 10 Nov 2002 07:32:38 GMT
bayard      2002/11/09 23:32:37

  Modified:    dbutils/src/java/org/apache/commons/dbutils DbUtils.java
  Added:       dbutils/src/java/org/apache/commons/dbutils
                        ResultSetIterator.java ResultSetIteratorV1.java
  Log:
  Added a pair of ResultSetIterators, one is a nice one and one is an old
  'works in Oracle' one. Both are available through DbUtils.
  
  Also added a selection of close and closeQuietly methods to DbUtils.
  
  Revision  Changes    Path
  1.2       +88 -0     jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/DbUtils.java
  
  Index: DbUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/DbUtils.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DbUtils.java	9 Nov 2002 02:59:22 -0000	1.1
  +++ DbUtils.java	10 Nov 2002 07:32:37 -0000	1.2
  @@ -57,10 +57,13 @@
   package org.apache.commons.dbutils;
   
   import java.sql.Connection;
  +import java.sql.Statement;
   import java.sql.ResultSet;
   import java.sql.ResultSetMetaData;
   import java.sql.SQLException;
   
  +import java.util.Iterator;
  +
   public final class DbUtils {
   
       /**
  @@ -99,6 +102,91 @@
               objs[i] = obj;
           }
           return objs;
  +    }
  +
  +    /**
  +     * Iterate over a result set, getting an Object[] back 
  +     * for each row of the result set.
  +     * This version uses JDBC-2 code, which Oracle 9i fails 
  +     * to support.
  +     */
  +    static public Iterator iterateResultSet(ResultSet rs) {
  +        return new ResultSetIterator(rs);
  +    }
  +
  +    /**
  +     * Iterate over a result set, getting an Object[] back 
  +     * for each row of the result set.
  +     * This version uses JDBC-1 code.
  +     */
  +    static public Iterator iterateResultSetVersion1(ResultSet rs) {
  +        return new ResultSetIteratorV1(rs);
  +    }
  +
  +    /**
  +     * Close a connection, avoid closing if null. 
  +     */
  +    static public void close(Connection conn) throws SQLException {
  +        if(conn == null) {
  +            return;
  +        }
  +        conn.close();
  +    }
  +
  +    /**
  +     * Close a statement, avoid closing if null. 
  +     */
  +    static public void close(Statement stat) throws SQLException {
  +        if(stat == null) {
  +            return;
  +        }
  +        stat.close();
  +    }
  +
  +    /**
  +     * Close a result set, avoid closing if null. 
  +     */
  +    static public void close(ResultSet rs) throws SQLException {
  +        if(rs == null) {
  +            return;
  +        }
  +        rs.close();
  +    }
  +
  +    /**
  +     * Close a connection, avoid closing if null and hide 
  +     * any exceptions that occur. 
  +     */
  +    static public void closeQuietly(Connection conn) {
  +        try {
  +            close(conn);
  +        } catch(SQLException sqle) {
  +            // quiet
  +        }
  +    }
  +
  +    /**
  +     * Close a statement, avoid closing if null and hide 
  +     * any exceptions that occur. 
  +     */
  +    static public void closeQuietly(Statement stat) {
  +        try {
  +            close(stat);
  +        } catch(SQLException sqle) {
  +            // quiet
  +        }
  +    }
  +
  +    /**
  +     * Close a result set, avoid closing if null and hide 
  +     * any exceptions that occur. 
  +     */
  +    static public void closeQuietly(ResultSet rs) {
  +        try {
  +            close(rs);
  +        } catch(SQLException sqle) {
  +            // quiet
  +        }
       }
   
   }
  
  
  
  1.1                  jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/ResultSetIterator.java
  
  Index: ResultSetIterator.java
  ===================================================================
  /*
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. 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.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 APACHE SOFTWARE FOUNDATION OR
   * ITS 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.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  package org.apache.commons.dbutils;
  
  import java.sql.ResultSet;
  import java.sql.ResultSetMetaData;
  import java.sql.SQLException;
  
  import java.util.Iterator;
  
  /// a version of result set iterator that is simpler, but needs isLast which oracle 
  /// fails to offer. As such this is sidelined until Oracle adds features
  class ResultSetIterator implements Iterator {
  
      private ResultSet rs;
  
      public ResultSetIterator(ResultSet rs) {
          this.rs = rs;
      }
  
      public boolean hasNext() {
          try {
              return !rs.isLast();
          } catch(SQLException sqle) {
              sqle.printStackTrace();
              return false;
          }
      }
  
      public Object next() {
          try {
              rs.next();
              return DbUtils.resultSetToArray(rs);
          } catch(SQLException sqle) {
              sqle.printStackTrace();
              return null;
          }
      }
  
      public void remove() {
  //        throw new UnsupportedOperationException("Cannot remove from a "+
  //            "java.sql.ResultSet." );
          try {
              this.rs.deleteRow();
          } catch(SQLException sqle) {
              sqle.printStackTrace();
          }
      }
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/ResultSetIteratorV1.java
  
  Index: ResultSetIteratorV1.java
  ===================================================================
  /*
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. 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.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 APACHE SOFTWARE FOUNDATION OR
   * ITS 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.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  package org.apache.commons.dbutils;
  
  import java.sql.ResultSet;
  import java.sql.ResultSetMetaData;
  import java.sql.SQLException;
  
  import java.util.Iterator;
  
  class ResultSetIteratorV1 implements Iterator {
  
      private ResultSet rs;
      private boolean hasNextCalledLast = false;
      private Object val = null;
  
      public ResultSetIteratorV1(ResultSet rs) {
          this.rs = rs;
      }
  
      public boolean hasNext() {
          if(!this.hasNextCalledLast) {
              this.hasNextCalledLast = true;
              doNext();
          }
          return this.val != null;
      }
  
      public Object next() {
          if(this.val == null) {
              doNext();
          }
          this.hasNextCalledLast = false;
          return this.val;
      }
  
      private void doNext() {
          try {
              rs.next();
              this.val = DbUtils.resultSetToArray(rs);
          } catch(SQLException sqle) {
              //sqle.printStackTrace();
              this.val = null;
          }
      }
  
      public void remove() {
          throw new UnsupportedOperationException("Cannot remove from a "+
              "java.sql.ResultSet." );
  //        try {
  //            this.rs.deleteRow();
  //        } catch(SQLException sqle) {
  //            sqle.printStackTrace();
  //        }
      }
  
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message