Java Code Examples for org.apache.commons.dbutils.ResultSetHandler#handle()

The following examples show how to use org.apache.commons.dbutils.ResultSetHandler#handle() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JdbcHelper.java    From obevo with Apache License 2.0 5 votes vote down vote up
public <T> T query(Connection conn, String sql, ResultSetHandler<T> resultSetHandler) {
    Pair<Statement, ResultSet> stmtRsPair = null;
    try {
        stmtRsPair = queryAndLeaveStatementOpen(conn, sql);
        return resultSetHandler.handle(stmtRsPair.getTwo());
    } catch (SQLException e) {
        throw new DataAccessException(e);
    } finally {
        if (stmtRsPair != null) {
            DbUtils.closeQuietly(stmtRsPair.getOne());
            DbUtils.closeQuietly(stmtRsPair.getTwo());
        }
    }
}
 
Example 2
Source File: QueryRunner.java    From sqlhelper with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Calls query after checking the parameters to ensure nothing is null.
 *
 * @param conn      The connection to use for the query call.
 * @param closeConn True if the connection should be closed, false otherwise.
 * @param sql       The SQL statement to execute.
 * @param params    An array of query replacement parameters.  Each row in
 *                  this array is one set of batch replacement values.
 * @return The results of the query.
 * @throws SQLException If there are database or parameter errors.
 */
private <T> T query(Connection conn, boolean closeConn, String sql, ResultSetHandler<T> rsh, Object... params)
        throws SQLException {
    if (conn == null) {
        throw new SQLException("Null connection");
    }

    if (sql == null) {
        if (closeConn) {
            close(conn);
        }
        throw new SQLException("Null SQL statement");
    }

    if (rsh == null) {
        if (closeConn) {
            close(conn);
        }
        throw new SQLException("Null ResultSetHandler");
    }

    PreparedStatement stmt = null;
    ResultSet rs = null;
    T r = null;

    try {
        if (!PAGING_CONTEXT.isPagingRequest() || !SQLs.isSelectStatement(sql) || SQLs.isSelectCountStatement(sql)) {
            stmt = this.prepareStatement(conn, sql);
            this.fillStatement(stmt, params);
            rs = this.wrap(stmt.executeQuery());
            r = rsh.handle(rs);
        } else {
            r = doPagingQuery(conn, sql, rsh, params);
        }
    } catch (SQLException e) {
        this.rethrow(e, sql, params);

    } finally {
        try {
            close(rs);
        } finally {
            close(stmt);
            if (closeConn) {
                close(conn);
            }
        }
    }

    return r;
}