/** * Executes the given INSERT SQL statement. The * <code>Connection</code> is retrieved from the <code>DataSource</code> * set in the constructor. This <code>Connection</code> must be in * auto-commit mode or the insert will not be saved. * @param <T> The type of object that the handler returns * @param sql The SQL statement to execute. * @param rsh The handler used to create the result object from * the <code>ResultSet</code> of auto-generated keys. * @param params Initializes the PreparedStatement's IN (i.e. '?') * @return An object generated by the handler. * @throws SQLException if a database access error occurs * @since 1.6 */ public <T> T insert(String sql, ResultSetHandler<T> rsh, Object... params) throws SQLException { return insert(this.prepareConnection(), true, sql, rsh, params); }
/** * Executes the given SELECT SQL without any replacement parameters. * The <code>Connection</code> is retrieved from the * <code>DataSource</code> set in the constructor. * @param <T> The type of object that the handler returns * @param sql The SQL statement to execute. * @param rsh The handler used to create the result object from * the <code>ResultSet</code>. * * @return An object generated by the handler. * @throws SQLException if a database access error occurs */ public <T> T query(String sql, ResultSetHandler<T> rsh) throws SQLException { Connection conn = this.prepareConnection(); return this.<T>query(conn, true, sql, rsh, (Object[]) null); }
/** * Executes the given INSERT SQL without any replacement parameters. * The <code>Connection</code> is retrieved from the * <code>DataSource</code> set in the constructor. * @param <T> The type of object that the handler returns * @param sql The SQL statement to execute. * @param rsh The handler used to create the result object from * the <code>ResultSet</code> of auto-generated keys. * @return An object generated by the handler. * @throws SQLException if a database access error occurs * @since 1.6 */ public <T> T insert(String sql, ResultSetHandler<T> rsh) throws SQLException { return insert(this.prepareConnection(), true, sql, rsh, (Object[]) null); }
/** * Executes the given INSERT, UPDATE, or DELETE SQL statement without * any replacement parameters. The <code>Connection</code> is retrieved * from the <code>DataSource</code> set in the constructor. This * <code>Connection</code> must be in auto-commit mode or the update will * not be saved. * * @param sql The SQL statement to execute. * @throws SQLException if a database access error occurs * @return The number of rows updated. */ public int update(String sql) throws SQLException { Connection conn = this.prepareConnection(); return this.update(conn, true, sql, (Object[]) null); }
/** * Execute a batch of SQL INSERT, UPDATE, or DELETE queries. The * <code>Connection</code> is retrieved from the <code>DataSource</code> * set in the constructor. This <code>Connection</code> must be in * auto-commit mode or the update will not be saved. * * @param sql The SQL to execute. * @param params An array of query replacement parameters. Each row in * this array is one set of batch replacement values. * @return The number of rows updated per statement. * @throws SQLException if a database access error occurs * @since DbUtils 1.1 */ public int[] batch(String sql, Object[][] params) throws SQLException { Connection conn = this.prepareConnection(); return this.batch(conn, true, sql, params); }
/** * Executes the given INSERT, UPDATE, or DELETE SQL statement with * a single replacement parameter. The <code>Connection</code> is * retrieved from the <code>DataSource</code> set in the constructor. * This <code>Connection</code> must be in auto-commit mode or the * update will not be saved. * * @param sql The SQL statement to execute. * @param param The replacement parameter. * @throws SQLException if a database access error occurs * @return The number of rows updated. */ public int update(String sql, Object param) throws SQLException { Connection conn = this.prepareConnection(); return this.update(conn, true, sql, new Object[]{param}); }
/** * Executes the given INSERT, UPDATE, or DELETE SQL statement. The * <code>Connection</code> is retrieved from the <code>DataSource</code> * set in the constructor. This <code>Connection</code> must be in * auto-commit mode or the update will not be saved. * * @param sql The SQL statement to execute. * @param params Initializes the PreparedStatement's IN (i.e. '?') * parameters. * @throws SQLException if a database access error occurs * @return The number of rows updated. */ public int update(String sql, Object... params) throws SQLException { Connection conn = this.prepareConnection(); return this.update(conn, true, sql, params); }
/** * Executes the given SELECT SQL query and returns a result object. * The <code>Connection</code> is retrieved from the * <code>DataSource</code> set in the constructor. * @param <T> The type of object that the handler returns * @param sql The SQL statement to execute. * @param rsh The handler used to create the result object from * the <code>ResultSet</code>. * @param params Initialize the PreparedStatement's IN parameters with * this array. * @return An object generated by the handler. * @throws SQLException if a database access error occurs */ public <T> T query(String sql, ResultSetHandler<T> rsh, Object... params) throws SQLException { Connection conn = this.prepareConnection(); return this.<T>query(conn, true, sql, rsh, params); }
/** * Executes the given batch of INSERT SQL statements. The * <code>Connection</code> is retrieved from the <code>DataSource</code> * set in the constructor. This <code>Connection</code> must be in * auto-commit mode or the insert will not be saved. * @param <T> The type of object that the handler returns * @param sql The SQL statement to execute. * @param rsh The handler used to create the result object from * the <code>ResultSet</code> of auto-generated keys. * @param params Initializes the PreparedStatement's IN (i.e. '?') * @return The result generated by the handler. * @throws SQLException if a database access error occurs * @since 1.6 */ public <T> T insertBatch(String sql, ResultSetHandler<T> rsh, Object[][] params) throws SQLException { return insertBatch(this.prepareConnection(), true, sql, rsh, params); }
/** * Executes the given SELECT SQL with a single replacement parameter. * The <code>Connection</code> is retrieved from the * <code>DataSource</code> set in the constructor. * @param <T> The type of object that the handler returns * @param sql The SQL statement to execute. * @param param The replacement parameter. * @param rsh The handler used to create the result object from * the <code>ResultSet</code>. * * @return An object generated by the handler. * @throws SQLException if a database access error occurs * @deprecated Use {@link #query(String, ResultSetHandler, Object...)} */ @Deprecated public <T> T query(String sql, Object param, ResultSetHandler<T> rsh) throws SQLException { Connection conn = this.prepareConnection(); return this.<T>query(conn, true, sql, rsh, new Object[]{param}); }
/** * Executes the given SELECT SQL query and returns a result object. * The <code>Connection</code> is retrieved from the * <code>DataSource</code> set in the constructor. * @param <T> The type of object that the handler returns * @param sql The SQL statement to execute. * @param params Initialize the PreparedStatement's IN parameters with * this array. * * @param rsh The handler used to create the result object from * the <code>ResultSet</code>. * * @return An object generated by the handler. * @throws SQLException if a database access error occurs * @deprecated Use {@link #query(String, ResultSetHandler, Object...)} */ @Deprecated public <T> T query(String sql, Object[] params, ResultSetHandler<T> rsh) throws SQLException { Connection conn = this.prepareConnection(); return this.<T>query(conn, true, sql, rsh, params); }
/** * Execute an SQL statement, including a stored procedure call, which * returns one or more result sets. * Any parameters which are instances of {@link OutParameter} will be * registered as OUT parameters. * <p> * Use this method when: a) running SQL statements that return multiple * result sets; b) invoking a stored procedure that return result * sets and OUT parameters. Otherwise you may wish to use * {@link #query(java.lang.String, org.apache.commons.dbutils.ResultSetHandler, java.lang.Object...) } * (if there are no OUT parameters) or * {@link #execute(java.lang.String, java.lang.Object...) } * (if there are no result sets). * * @param <T> The type of object that the handler returns * @param sql The SQL to execute. * @param rsh The result set handler * @param params The query replacement parameters. * @return A list of objects generated by the handler * @throws SQLException if a database access error occurs */ public <T> List<T> execute(String sql, ResultSetHandler<T> rsh, Object... params) throws SQLException { Connection conn = this.prepareConnection(); return this.execute(conn, true, sql, rsh, params); }
/** * Execute an SQL statement, including a stored procedure call, which does * not return any result sets. * Any parameters which are instances of {@link OutParameter} will be * registered as OUT parameters. * <p> * Use this method when invoking a stored procedure with OUT parameters * that does not return any result sets. If you are not invoking a stored * procedure, or the stored procedure has no OUT parameters, consider using * {@link #update(java.lang.String, java.lang.Object...) }. * If the stored procedure returns result sets, use * {@link #execute(java.lang.String, org.apache.commons.dbutils.ResultSetHandler, java.lang.Object...) }. * <p> * The <code>Connection</code> is retrieved from the <code>DataSource</code> * set in the constructor. This <code>Connection</code> must be in * auto-commit mode or the update will not be saved. * * @param sql The SQL statement to execute. * @param params Initializes the CallableStatement's parameters (i.e. '?'). * @throws SQLException if a database access error occurs * @return The number of rows updated. */ public int execute(String sql, Object... params) throws SQLException { Connection conn = this.prepareConnection(); return this.execute(conn, true, sql, params); }
@Override public Connection prepareConnection() throws SQLException { if (connectionManager == null) return super.prepareConnection(); else return connectionManager.getConnection(this.getDataSource()); }