dgraham     2003/07/09 17:27:30

  Modified:    mapper/src/share/org/apache/commons/mapper/jdbc
                        JdbcHelper.java
  Log:
  Added getConnection() method and made DataSource protected.
  
  Revision  Changes    Path
  1.5       +397 -375  
jakarta-commons-sandbox/mapper/src/share/org/apache/commons/mapper/jdbc/JdbcHelper.java
  
  Index: JdbcHelper.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/mapper/src/share/org/apache/commons/mapper/jdbc/JdbcHelper.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- JdbcHelper.java   10 Jun 2003 04:14:31 -0000      1.4
  +++ JdbcHelper.java   10 Jul 2003 00:27:29 -0000      1.5
  @@ -81,377 +81,399 @@
    */
   public class JdbcHelper {
   
  -    /**
  -     * An implementation of StatementPreparer that does nothing.  Useful when there
  -     * are no replacement parameters to be set on the PreparedStatement.
  -     */
  -    private static final StatementPreparer NULL_PREPARER = new StatementPreparer() {
  -        public void prepareStatement(PreparedStatement stmt, Object obj)
  -            throws SQLException {
  -            ; // do nothing
  -        }
  -    };
  -
  -    /**
  -     * An implementation of StatementPreparer that fills a PreparedStatement 
  -     * with values from an Object[].
  -     */
  -    private static final StatementPreparer ARRAY_PREPARER =
  -        new StatementPreparer() {
  -        public void prepareStatement(PreparedStatement stmt, Object obj)
  -            throws SQLException {
  -
  -            Object[] args = (Object[]) obj;
  -            for (int i = 0; i < args.length; i++) {
  -                stmt.setObject(i + 1, args[i]);
  -            }
  -        }
  -    };
  -
  -    private DataSource ds = null;
  -
  -    /**
  -     * Constructor for JdbcHelper.
  -     * @param ds The DataSource to get Connections from.
  -     */
  -    public JdbcHelper(DataSource ds) {
  -        super();
  -        this.ds = ds;
  -    }
  -
  -    /**
  -     * Executes the given INSERT, UPDATE, or DELETE SQL statement.  The 
  -     * statement is executed in it's own transaction that will be committed or 
rolled 
  -     * back depending on any SQLExceptions thrown.
  -     * @param sql The SQL statement to execute.
  -     * @param preparer Initializes the PreparedStatement's IN (ie. '?') parameters.
  -     * @param prepareObject An object to pass to the preparer to setup the 
  -     * PreparedStatement.
  -     * @throws MapperException
  -     * @return The number of rows updated.
  -     */
  -    public int executeUpdate(
  -        String sql,
  -        StatementPreparer preparer,
  -        Object prepareObject)
  -        throws MapperException {
  -
  -        Connection conn = null;
  -        PreparedStatement stmt = null;
  -        boolean autoCommit = false;
  -        int rows = 0;
  -
  -        try {
  -            conn = this.ds.getConnection();
  -            autoCommit = conn.getAutoCommit(); // save old value
  -            conn.setAutoCommit(false); // single transaction.
  -
  -            stmt = conn.prepareStatement(sql);
  -            preparer.prepareStatement(stmt, prepareObject);
  -            rows = stmt.executeUpdate();
  -
  -            conn.commit();
  -
  -        } catch (SQLException e) {
  -            this.rollback(conn);
  -            throw new MapperException(e);
  -        } finally {
  -            this.setAutoCommit(autoCommit, conn);
  -            this.closeStatement(stmt);
  -            this.closeConnection(conn);
  -        }
  -
  -        return rows;
  -    }
  -
  -    /**
  -     * Executes the given INSERT, UPDATE, or DELETE SQL statement.  The 
  -     * statement is executed in it's own transaction that will be committed or 
rolled 
  -     * back depending on any SQLExceptions thrown.
  -     * @param sql The SQL statement to execute.
  -     * @param params An array of values to fill the sql '?' markers with.
  -     * @throws MapperException
  -     * @return The number of rows updated.
  -     */
  -    public int executeUpdate(String sql, Object[] params) throws MapperException {
  -        return this.executeUpdate(sql, ARRAY_PREPARER, params);
  -    }
  -
  -    /**
  -     * Executes the given INSERT, UPDATE, or DELETE SQL statement.  The 
  -     * statement is executed in it's own transaction that will be committed or 
rolled 
  -     * back depending on any SQLExceptions thrown.  This is 
  -     * useful for queries with only one replacement parameter and is the equivalent 
of
  -     * calling executeUpdate(sql, new Object[] { param }).
  -     * @param sql The SQL statement to execute.
  -     * @param param An object to fill one sql '?' marker with.
  -     * @throws MapperException
  -     * @return The number of rows updated.
  -     */
  -    public int executeUpdate(String sql, Object param) throws MapperException {
  -        return this.executeUpdate(sql, ARRAY_PREPARER, new Object[] { param });
  -    }
  -
  -    /**
  -     * Executes the given SELECT SQL query and returns a List of results.
  -     * @param sql The SQL statement to execute.
  -     * @param preparer Initializes the PreparedStatement's IN parameters.
  -     * @param prepareObject An object to pass to the preparer to setup the 
  -     * PreparedStatement.
  -     * @param resultFactory The factory used to create the result objects from the 
  -     * ResultSet.
  -     * @return A list of objects generated by the resultFactory.
  -     * @throws MapperException
  -     */
  -    public List executeQuery(
  -        String sql,
  -        StatementPreparer preparer,
  -        Object prepareObject,
  -        ResultObjectFactory resultFactory)
  -        throws MapperException {
  -
  -        Connection conn = null;
  -        PreparedStatement stmt = null;
  -        ResultSet rs = null;
  -        ArrayList found = new ArrayList();
  -
  -        try {
  -            conn = this.ds.getConnection();
  -            stmt = conn.prepareStatement(sql);
  -
  -            preparer.prepareStatement(stmt, prepareObject);
  -
  -            rs = stmt.executeQuery();
  -            while (rs.next()) {
  -                found.add(resultFactory.createResultObject(rs));
  -            }
  -
  -        } catch (SQLException e) {
  -            throw new MapperException(e);
  -        } finally {
  -            this.closeResultSet(rs);
  -            this.closeStatement(stmt);
  -            this.closeConnection(conn);
  -        }
  -
  -        return found;
  -    }
  -
  -    /**
  -     * Executes the given SELECT SQL query and returns a List of results.
  -     * @param sql The SQL statement to execute.
  -     * @param params An array of values to fill the sql '?' markers with.
  -     * @param resultFactory
  -     * @return A list of objects generated by the resultFactory.
  -     * @throws MapperException
  -     * @see #executeQuery(String, StatementPreparer, Object, ResultObjectFactory)
  -     */
  -    public List executeQuery(
  -        String sql,
  -        Object[] params,
  -        ResultObjectFactory resultFactory)
  -        throws MapperException {
  -
  -        return this.executeQuery(sql, ARRAY_PREPARER, params, resultFactory);
  -    }
  -
  -    /**
  -     * Executes the given SELECT SQL query and returns a List of results.  This is 
  -     * useful for queries with only one replacement parameter and is the equivalent 
of
  -     * calling executeQuery(sql, new Object[] { param }, factory).
  -     * @param sql The SQL statement to execute.
  -     * @param param An object to fill one sql '?' marker with.
  -     * @param resultFactory
  -     * @return A list of objects generated by the resultFactory.
  -     * @throws MapperException
  -     * @see #executeQuery(String, Object[], ResultObjectFactory)
  -     */
  -    public List executeQuery(
  -        String sql,
  -        Object param,
  -        ResultObjectFactory resultFactory)
  -        throws MapperException {
  -
  -        return this.executeQuery(
  -            sql,
  -            ARRAY_PREPARER,
  -            new Object[] { param },
  -            resultFactory);
  -    }
  -
  -    /**
  -     * Executes a query that doesn't need parameter replacement.
  -     * @param sql The SQL statement to execute.
  -     * @param resultFactory
  -     * @return A list of objects generated by the resultFactory.
  -     * @throws MapperException
  -     * @see #executeQuery(String, StatementPreparer, Object, ResultObjectFactory)
  -     */
  -    public List executeQuery(String sql, ResultObjectFactory resultFactory)
  -        throws MapperException {
  -        return this.executeQuery(sql, NULL_PREPARER, null, resultFactory);
  -    }
  -
  -    /**
  -     * Performs just like the other getField method except that this one gets its 
own 
  -     * Connection.
  -     * @param query
  -     * @param column
  -     * @return The value of the selected field.
  -     * @throws MapperException
  -     */
  -    public String getField(String query, String column) throws MapperException {
  -        Connection conn = null;
  -        String field = null;
  -
  -        try {
  -            conn = this.ds.getConnection();
  -            field = this.getField(query, column, conn);
  -
  -        } catch (SQLException e) {
  -            throw new MapperException(e);
  -        } finally {
  -            this.closeConnection(conn);
  -        }
  -        return field;
  -    }
  -
  -    /**
  -     * Executes the query and returns the column's value from the first returned 
row.
  -     * This is helpful when finding the last generated ID in a table but can be 
used to
  -     * return any column value.
  -     * @param query A valid SQL SELECT query.
  -     * @param column The column whose value should be returned.
  -     * @param conn A Connection to run the query on.
  -     * @throws SQLException if a database access error occurs or the given query
  -     * returned 0 rows.
  -     */
  -    public String getField(String query, String column, Connection conn)
  -        throws SQLException {
  -        PreparedStatement stmt = conn.prepareStatement(query);
  -        String field = null;
  -
  -        try {
  -            ResultSet rs = stmt.executeQuery();
  -            if (!rs.next()) {
  -                throw new SQLException(
  -                    "Query: " + query + " didn't return any records.");
  -            }
  -            field = rs.getString(column);
  -            rs.close();
  -        } finally {
  -            stmt.close();
  -        }
  -        return field;
  -    }
  -
  -    /**
  -     * Rollback any changes made on the given connection.
  -     * @param conn The database Connection to rollback.  A null value is legal.
  -     * @throws MapperException if an SQLException occurred during the rollback.
  -     */
  -    public void rollback(Connection conn) throws MapperException {
  -        try {
  -            if (conn != null) {
  -                conn.rollback();
  -            }
  -        } catch (SQLException e) {
  -            throw new MapperException(e);
  -        }
  -    }
  -
  -    /**
  -     * Convenience method to reset autoCommit on a connection to its previous value
  -     * while wrapping any SQLException in a MapperException.  
  -     * We restore autoCommit after using the connection
  -     * for a create, delete, update, or find because other code might be using
  -     * the same DataSource object to get connections and might depend on the 
  -     * autoCommit flag being set a certain way.  We set it to false and then 
restore 
  -     * it when we're done with the connection.
  -     * @param autoCommit The autoCommit value to set on the Connection.
  -     * @param conn The Connection to set autoCommit on.
  -     * @throws MapperException if an SQLException occurred setting the autoCommit
  -     * status.
  -     */
  -    public void setAutoCommit(boolean autoCommit, Connection conn)
  -        throws MapperException {
  -        if (conn != null) {
  -            try {
  -                conn.setAutoCommit(autoCommit);
  -            } catch (SQLException e) {
  -                throw new MapperException(e);
  -            }
  -        }
  -    }
  -
  -    /**
  -     * Attempts to close the given connection wrapping any SQLExceptions that occur
  -     * in a MapperException.  Typically, a mapper's methods throw MapperException 
  -     * rather than SQLException so this simplifies Connection cleanup.  Connection 
  -     * objects should <strong>always</strong> be closed in 
  -     * case the DataSource is performing Connection pooling so the Connection can 
  -     * be returned to the pool.
  -     * @param conn The Connection to close.  A null value for this argument is 
legal.
  -     * @throws MapperException if an SQLException is thrown closing the Connection.
  -     */
  -    public void closeConnection(Connection conn) throws MapperException {
  -        try {
  -            if (conn != null) {
  -                conn.close();
  -            }
  -        } catch (SQLException e) {
  -            throw new MapperException(e);
  -        }
  -    }
  -
  -    /**
  -     * Attempts to close the given Statement wrapping any SQLExceptions that occur
  -     * in a MapperException.  Typically, a mapper's methods throw MapperException 
  -     * rather than SQLException so this simplifies Statement cleanup.  Statement 
  -     * objects (especially PreparedStatements) should <strong>always</strong> be 
  -     * closed in case the DataSource is performing Statement pooling so the 
  -     * Statement can be returned to the pool.
  -     * @param stmt The Statment to close.  A null value for this argument is legal.
  -     * @throws MapperException if an SQLException is thrown closing the Statement.
  -     */
  -    public void closeStatement(Statement stmt) throws MapperException {
  -        try {
  -            if (stmt != null) {
  -                stmt.close();
  -            }
  -        } catch (SQLException e) {
  -            throw new MapperException(e);
  -        }
  -    }
  -
  -    /**
  -     * Attempts to close the given ResultSet wrapping any SQLExceptions that occur
  -     * in a MapperException.
  -     * @param rs The ResultSet to close.  A null value for this argument is legal.
  -     * @throws MapperException if an SQLException is thrown closing the ResultSet.
  -     */
  -    public void closeResultSet(ResultSet rs) throws MapperException {
  -        try {
  -            if (rs != null) {
  -                rs.close();
  -            }
  -        } catch (SQLException e) {
  -            throw new MapperException(e);
  -        }
  -    }
  -
  -    /**
  -     * Converts a boolean to an integer.  False is 0, true is 1.
  -     */
  -    public int toInt(boolean bool) {
  -        return bool ? 1 : 0;
  -    }
  -
  -    /**
  -     * Converts an integer to a boolean.  Zero is false, anything else is true.
  -     */
  -    public boolean toBoolean(int value) {
  -        return (value == 0) ? false : true;
  -    }
  +     /**
  +      * An implementation of StatementPreparer that does nothing.  Useful when there
  +      * are no replacement parameters to be set on the PreparedStatement.
  +      */
  +     private static final StatementPreparer NULL_PREPARER = new StatementPreparer() 
{
  +             public void prepareStatement(PreparedStatement stmt, Object obj)
  +                     throws SQLException {
  +                     ; // do nothing
  +             }
  +     };
  +
  +     /**
  +      * An implementation of StatementPreparer that fills a PreparedStatement 
  +      * with values from an Object[].
  +      */
  +     private static final StatementPreparer ARRAY_PREPARER =
  +             new StatementPreparer() {
  +             public void prepareStatement(PreparedStatement stmt, Object obj)
  +                     throws SQLException {
  +
  +                     Object[] args = (Object[]) obj;
  +                     for (int i = 0; i < args.length; i++) {
  +                             stmt.setObject(i + 1, args[i]);
  +                     }
  +             }
  +     };
  +
  +     protected DataSource ds = null;
  +
  +     /**
  +      * Constructor for JdbcHelper.
  +      * @param ds The DataSource to get Connections from.
  +      */
  +     public JdbcHelper(DataSource ds) {
  +             super();
  +             this.ds = ds;
  +     }
  +
  +     /**
  +      * Executes the given INSERT, UPDATE, or DELETE SQL statement.  The 
  +      * statement is executed in it's own transaction that will be committed or 
rolled 
  +      * back depending on any SQLExceptions thrown.
  +      * @param sql The SQL statement to execute.
  +      * @param preparer Initializes the PreparedStatement's IN (ie. '?') parameters.
  +      * @param prepareObject An object to pass to the preparer to setup the 
  +      * PreparedStatement.
  +      * @throws MapperException
  +      * @return The number of rows updated.
  +      */
  +     public int executeUpdate(
  +             String sql,
  +             StatementPreparer preparer,
  +             Object prepareObject)
  +             throws MapperException {
  +
  +             Connection conn = null;
  +             PreparedStatement stmt = null;
  +             boolean autoCommit = false;
  +             int rows = 0;
  +
  +             try {
  +                     conn = this.getConnection();
  +                     autoCommit = conn.getAutoCommit(); // save old value
  +                     conn.setAutoCommit(false); // single transaction.
  +
  +                     stmt = conn.prepareStatement(sql);
  +                     preparer.prepareStatement(stmt, prepareObject);
  +                     rows = stmt.executeUpdate();
  +
  +                     conn.commit();
  +
  +             } catch (SQLException e) {
  +                     this.rollback(conn);
  +                     throw new MapperException(e);
  +             } finally {
  +                     this.setAutoCommit(autoCommit, conn);
  +                     this.closeStatement(stmt);
  +                     this.closeConnection(conn);
  +             }
  +
  +             return rows;
  +     }
  +
  +     /**
  +      * Executes the given INSERT, UPDATE, or DELETE SQL statement.  The 
  +      * statement is executed in it's own transaction that will be committed or 
rolled 
  +      * back depending on any SQLExceptions thrown.
  +      * @param sql The SQL statement to execute.
  +      * @param params An array of values to fill the sql '?' markers with.
  +      * @throws MapperException
  +      * @return The number of rows updated.
  +      */
  +     public int executeUpdate(String sql, Object[] params) throws MapperException {
  +             return this.executeUpdate(sql, ARRAY_PREPARER, params);
  +     }
  +
  +     /**
  +      * Executes the given INSERT, UPDATE, or DELETE SQL statement.  The 
  +      * statement is executed in it's own transaction that will be committed or 
rolled 
  +      * back depending on any SQLExceptions thrown.  This is 
  +      * useful for queries with only one replacement parameter and is the 
equivalent of
  +      * calling executeUpdate(sql, new Object[] { param }).
  +      * @param sql The SQL statement to execute.
  +      * @param param An object to fill one sql '?' marker with.
  +      * @throws MapperException
  +      * @return The number of rows updated.
  +      */
  +     public int executeUpdate(String sql, Object param) throws MapperException {
  +             return this.executeUpdate(sql, ARRAY_PREPARER, new Object[] { param });
  +     }
  +
  +     /**
  +      * Executes the given SELECT SQL query and returns a List of results.
  +      * @param sql The SQL statement to execute.
  +      * @param preparer Initializes the PreparedStatement's IN parameters.
  +      * @param prepareObject An object to pass to the preparer to setup the 
  +      * PreparedStatement.
  +      * @param resultFactory The factory used to create the result objects from the 
  +      * ResultSet.
  +      * @return A list of objects generated by the resultFactory.
  +      * @throws MapperException
  +      */
  +     public List executeQuery(
  +             String sql,
  +             StatementPreparer preparer,
  +             Object prepareObject,
  +             ResultObjectFactory resultFactory)
  +             throws MapperException {
  +
  +             Connection conn = null;
  +             PreparedStatement stmt = null;
  +             ResultSet rs = null;
  +             ArrayList found = new ArrayList();
  +
  +             try {
  +                     conn = this.getConnection();
  +                     stmt = conn.prepareStatement(sql);
  +
  +                     preparer.prepareStatement(stmt, prepareObject);
  +
  +                     rs = stmt.executeQuery();
  +                     while (rs.next()) {
  +                             found.add(resultFactory.createResultObject(rs));
  +                     }
  +
  +             } catch (SQLException e) {
  +                     throw new MapperException(e);
  +             } finally {
  +                     this.closeResultSet(rs);
  +                     this.closeStatement(stmt);
  +                     this.closeConnection(conn);
  +             }
  +
  +             return found;
  +     }
  +
  +     /**
  +      * Executes the given SELECT SQL query and returns a List of results.
  +      * @param sql The SQL statement to execute.
  +      * @param params An array of values to fill the sql '?' markers with.
  +      * @param resultFactory
  +      * @return A list of objects generated by the resultFactory.
  +      * @throws MapperException
  +      * @see #executeQuery(String, StatementPreparer, Object, ResultObjectFactory)
  +      */
  +     public List executeQuery(
  +             String sql,
  +             Object[] params,
  +             ResultObjectFactory resultFactory)
  +             throws MapperException {
  +
  +             return this.executeQuery(sql, ARRAY_PREPARER, params, resultFactory);
  +     }
  +
  +     /**
  +      * Executes the given SELECT SQL query and returns a List of results.  This is 
  +      * useful for queries with only one replacement parameter and is the 
equivalent of
  +      * calling executeQuery(sql, new Object[] { param }, factory).
  +      * @param sql The SQL statement to execute.
  +      * @param param An object to fill one sql '?' marker with.
  +      * @param resultFactory
  +      * @return A list of objects generated by the resultFactory.
  +      * @throws MapperException
  +      * @see #executeQuery(String, Object[], ResultObjectFactory)
  +      */
  +     public List executeQuery(
  +             String sql,
  +             Object param,
  +             ResultObjectFactory resultFactory)
  +             throws MapperException {
  +
  +             return this.executeQuery(
  +                     sql,
  +                     ARRAY_PREPARER,
  +                     new Object[] { param },
  +                     resultFactory);
  +     }
  +
  +     /**
  +      * Executes a query that doesn't need parameter replacement.
  +      * @param sql The SQL statement to execute.
  +      * @param resultFactory
  +      * @return A list of objects generated by the resultFactory.
  +      * @throws MapperException
  +      * @see #executeQuery(String, StatementPreparer, Object, ResultObjectFactory)
  +      */
  +     public List executeQuery(String sql, ResultObjectFactory resultFactory)
  +             throws MapperException {
  +                     
  +             return this.executeQuery(sql, NULL_PREPARER, null, resultFactory);
  +     }
  +
  +     /**
  +      * Performs just like the other getField method except that this one gets its 
own 
  +      * Connection.
  +      * @param query
  +      * @param column
  +      * @return The value of the selected field.
  +      * @throws MapperException
  +      */
  +     public String getField(String query, String column) throws MapperException {
  +             Connection conn = null;
  +             String field = null;
  +
  +             try {
  +                     conn = this.getConnection();
  +                     field = this.getField(query, column, conn);
  +
  +             } catch (SQLException e) {
  +                     throw new MapperException(e);
  +             } finally {
  +                     this.closeConnection(conn);
  +             }
  +             
  +             return field;
  +     }
  +
  +     /**
  +      * Executes the query and returns the column's value from the first returned 
row.
  +      * This is helpful when finding the last generated ID in a table but can be 
used to
  +      * return any column value.
  +      * @param query A valid SQL SELECT query.
  +      * @param column The column whose value should be returned.
  +      * @param conn A Connection to run the query on.
  +      * @throws SQLException if a database access error occurs or the given query
  +      * returned 0 rows.
  +      */
  +     public String getField(String query, String column, Connection conn)
  +             throws SQLException {
  +
  +             PreparedStatement stmt = conn.prepareStatement(query);
  +             ResultSet rs = null;
  +             String field = null;
  +
  +             try {
  +                     rs = stmt.executeQuery();
  +                     if (!rs.next()) {
  +                             throw new SQLException(
  +                                     "Query: " + query + " didn't return any 
records.");
  +                     }
  +                     field = rs.getString(column);
  +
  +             } finally {
  +                     if (rs != null) {
  +                             rs.close();
  +                     }
  +                     
  +                     stmt.close();
  +             }
  +             
  +             return field;
  +     }
  +
  +     /**
  +      * Rollback any changes made on the given connection.
  +      * @param conn The database Connection to rollback.  A null value is legal.
  +      * @throws MapperException if an SQLException occurred during the rollback.
  +      */
  +     public void rollback(Connection conn) throws MapperException {
  +             try {
  +                     if (conn != null) {
  +                             conn.rollback();
  +                     }
  +             } catch (SQLException e) {
  +                     throw new MapperException(e);
  +             }
  +     }
  +
  +     /**
  +      * Convenience method to reset autoCommit on a connection to its previous value
  +      * while wrapping any SQLException in a MapperException.  
  +      * We restore autoCommit after using the connection
  +      * for a create, delete, update, or find because other code might be using
  +      * the same DataSource object to get connections and might depend on the 
  +      * autoCommit flag being set a certain way.  We set it to false and then 
restore 
  +      * it when we're done with the connection.
  +      * @param autoCommit The autoCommit value to set on the Connection.
  +      * @param conn The Connection to set autoCommit on.
  +      * @throws MapperException if an SQLException occurred setting the autoCommit
  +      * status.
  +      */
  +     public void setAutoCommit(boolean autoCommit, Connection conn)
  +             throws MapperException {
  +             if (conn != null) {
  +                     try {
  +                             conn.setAutoCommit(autoCommit);
  +                     } catch (SQLException e) {
  +                             throw new MapperException(e);
  +                     }
  +             }
  +     }
  +
  +     /**
  +      * Attempts to close the given connection wrapping any SQLExceptions that occur
  +      * in a MapperException.  Typically, a mapper's methods throw MapperException 
  +      * rather than SQLException so this simplifies Connection cleanup.  Connection 
  +      * objects should <strong>always</strong> be closed in 
  +      * case the DataSource is performing Connection pooling so the Connection can 
  +      * be returned to the pool.
  +      * @param conn The Connection to close.  A null value for this argument is 
legal.
  +      * @throws MapperException if an SQLException is thrown closing the Connection.
  +      */
  +     public void closeConnection(Connection conn) throws MapperException {
  +             try {
  +                     if (conn != null) {
  +                             conn.close();
  +                     }
  +             } catch (SQLException e) {
  +                     throw new MapperException(e);
  +             }
  +     }
  +
  +     /**
  +      * Attempts to close the given Statement wrapping any SQLExceptions that occur
  +      * in a MapperException.  Typically, a mapper's methods throw MapperException 
  +      * rather than SQLException so this simplifies Statement cleanup.  Statement 
  +      * objects (especially PreparedStatements) should <strong>always</strong> be 
  +      * closed in case the DataSource is performing Statement pooling so the 
  +      * Statement can be returned to the pool.
  +      * @param stmt The Statment to close.  A null value for this argument is legal.
  +      * @throws MapperException if an SQLException is thrown closing the Statement.
  +      */
  +     public void closeStatement(Statement stmt) throws MapperException {
  +             try {
  +                     if (stmt != null) {
  +                             stmt.close();
  +                     }
  +             } catch (SQLException e) {
  +                     throw new MapperException(e);
  +             }
  +     }
  +
  +     /**
  +      * Attempts to close the given ResultSet wrapping any SQLExceptions that occur
  +      * in a MapperException.
  +      * @param rs The ResultSet to close.  A null value for this argument is legal.
  +      * @throws MapperException if an SQLException is thrown closing the ResultSet.
  +      */
  +     public void closeResultSet(ResultSet rs) throws MapperException {
  +             try {
  +                     if (rs != null) {
  +                             rs.close();
  +                     }
  +             } catch (SQLException e) {
  +                     throw new MapperException(e);
  +             }
  +     }
  +
  +     /**
  +      * Converts a boolean to an integer.  False is 0, true is 1.
  +      */
  +     public int toInt(boolean bool) {
  +             return bool ? 1 : 0;
  +     }
  +
  +     /**
  +      * Converts an integer to a boolean.  Zero is false, anything else is true.
  +      */
  +     public boolean toBoolean(int value) {
  +             return (value == 0) ? false : true;
  +     }
  +
  +     /**
  +      * JdbcHelper uses this method internally whenever it needs a database 
  +      * Connection.  This implementation retrieves a Connection from the 
  +      * DataSource.  Subclasses can override this method to change how 
  +      * Connections are retrieved, perhaps supplying a username and password 
  +      * other than what was configured in the DataSource. 
  +      * @return A database Connection.
  +      * @throws SQLException
  +      */
  +     protected Connection getConnection() throws SQLException {
  +             return this.ds.getConnection();
  +     }
   
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to