Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.apache.commons.dbutils;
 
 
Executes SQL queries with pluggable strategies for handling ResultSets. This class is thread safe.

 
 public class QueryRunner extends AbstractQueryRunner {

    
Constructor for QueryRunner.
 
     public QueryRunner() {
         super();
     }

    
Constructor for QueryRunner that controls the use of ParameterMetaData.

Parameters:
pmdKnownBroken Some drivers don't support ; if pmdKnownBroken is set to true, we won't even try it; if false, we'll try it, and if it breaks, we'll remember not to use it again.
 
     public QueryRunner(boolean pmdKnownBroken) {
         super(pmdKnownBroken);
     }

    
Constructor for QueryRunner that takes a DataSource to use. Methods that do not take a Connection parameter will retrieve connections from this DataSource.

Parameters:
ds The DataSource to retrieve connections from.
 
     public QueryRunner(DataSource ds) {
         super(ds);
     }

    
Constructor for QueryRunner that takes a DataSource and controls the use of ParameterMetaData. Methods that do not take a Connection parameter will retrieve connections from this DataSource.

Parameters:
ds The DataSource to retrieve connections from.
pmdKnownBroken Some drivers don't support ; if pmdKnownBroken is set to true, we won't even try it; if false, we'll try it, and if it breaks, we'll remember not to use it again.
 
     public QueryRunner(DataSource dsboolean pmdKnownBroken) {
         super(dspmdKnownBroken);
     }

    
Execute a batch of SQL INSERT, UPDATE, or DELETE queries.

Parameters:
conn The Connection to use to run the query. The caller is responsible for closing this Connection.
sql The SQL to execute.
params An array of query replacement parameters. Each row in this array is one set of batch replacement values.
Returns:
The number of rows updated per statement.
Throws:
java.sql.SQLException if a database access error occurs
Since:
DbUtils 1.1
 
     public int[] batch(Connection connString sqlObject[][] paramsthrows SQLException {
         return this.batch(connfalsesqlparams);
     }

    
Execute a batch of SQL INSERT, UPDATE, or DELETE queries. The Connection is retrieved from the DataSource set in the constructor. This Connection must be in auto-commit mode or the update will not be saved.

Parameters:
sql The SQL to execute.
params An array of query replacement parameters. Each row in this array is one set of batch replacement values.
Returns:
The number of rows updated per statement.
Throws:
java.sql.SQLException if a database access error occurs
Since:
DbUtils 1.1
    public int[] batch(String sqlObject[][] paramsthrows SQLException {
        Connection conn = this.prepareConnection();
        return this.batch(conntruesqlparams);
    }

    
Calls update after checking the parameters to ensure nothing is null.

Parameters:
conn The connection to use for the batch call.
closeConn True if the connection should be closed, false otherwise.
sql The SQL statement to execute.
params An array of query replacement parameters. Each row in this array is one set of batch replacement values.
Returns:
The number of rows updated in the batch.
Throws:
java.sql.SQLException If there are database or parameter errors.
    private int[] batch(Connection connboolean closeConnString sqlObject[][] paramsthrows SQLException {
        if (conn == null) {
            throw new SQLException("Null connection");
        }
        if (sql == null) {
            if (closeConn) {
                close(conn);
            }
            throw new SQLException("Null SQL statement");
        }
        if (params == null) {
            if (closeConn) {
                close(conn);
            }
            throw new SQLException("Null parameters. If parameters aren't need, pass an empty array.");
        }
        PreparedStatement stmt = null;
        int[] rows = null;
        try {
            stmt = this.prepareStatement(connsql);
            for (int i = 0; i < params.lengthi++) {
                this.fillStatement(stmtparams[i]);
                stmt.addBatch();
            }
            rows = stmt.executeBatch();
        } catch (SQLException e) {
            this.rethrow(esql, (Object[])params);
        } finally {
            close(stmt);
            if (closeConn) {
                close(conn);
            }
        }
        return rows;
    }

    
Execute an SQL SELECT query with a single replacement parameter. The caller is responsible for closing the connection.

Deprecated:
Use query(java.sql.Connection,java.lang.String,org.apache.commons.dbutils.ResultSetHandler,java.lang.Object[])
Parameters:
<T> The type of object that the handler returns
conn The connection to execute the query in.
sql The query to execute.
param The replacement parameter.
rsh The handler that converts the results into an object.
Returns:
The object returned by the handler.
Throws:
java.sql.SQLException if a database access error occurs
    @Deprecated
    public <T> T query(Connection connString sqlObject paramResultSetHandler<T> rshthrows SQLException {
        return this.<T>query(connfalsesqlrshnew Object[]{param});
    }

    
Execute an SQL SELECT query with replacement parameters. The caller is responsible for closing the connection.

Deprecated:
Use query(java.sql.Connection,java.lang.String,org.apache.commons.dbutils.ResultSetHandler,java.lang.Object[]) instead
Parameters:
<T> The type of object that the handler returns
conn The connection to execute the query in.
sql The query to execute.
params The replacement parameters.
rsh The handler that converts the results into an object.
Returns:
The object returned by the handler.
Throws:
java.sql.SQLException if a database access error occurs
    @Deprecated
    public <T> T query(Connection connString sqlObject[] paramsResultSetHandler<T> rshthrows SQLException {
                return this.<T>query(connfalsesqlrshparams);
    }

    
Execute an SQL SELECT query with replacement parameters. The caller is responsible for closing the connection.

Parameters:
<T> The type of object that the handler returns
conn The connection to execute the query in.
sql The query to execute.
rsh The handler that converts the results into an object.
params The replacement parameters.
Returns:
The object returned by the handler.
Throws:
java.sql.SQLException if a database access error occurs
    public <T> T query(Connection connString sqlResultSetHandler<T> rshObject... paramsthrows SQLException {
        return this.<T>query(connfalsesqlrshparams);
    }

    
Execute an SQL SELECT query without any replacement parameters. The caller is responsible for closing the connection.

Parameters:
<T> The type of object that the handler returns
conn The connection to execute the query in.
sql The query to execute.
rsh The handler that converts the results into an object.
Returns:
The object returned by the handler.
Throws:
java.sql.SQLException if a database access error occurs
    public <T> T query(Connection connString sqlResultSetHandler<T> rshthrows SQLException {
        return this.<T>query(connfalsesqlrsh, (Object[]) null);
    }

    
Executes the given SELECT SQL with a single replacement parameter. The Connection is retrieved from the DataSource set in the constructor.

Deprecated:
Use query(java.lang.String,org.apache.commons.dbutils.ResultSetHandler,java.lang.Object[])
Parameters:
<T> The type of object that the handler returns
sql The SQL statement to execute.
param The replacement parameter.
rsh The handler used to create the result object from the ResultSet.
Returns:
An object generated by the handler.
Throws:
java.sql.SQLException if a database access error occurs
    @Deprecated
    public <T> T query(String sqlObject paramResultSetHandler<T> rshthrows SQLException {
        Connection conn = this.prepareConnection();
        return this.<T>query(conntruesqlrshnew Object[]{param});
    }

    
Executes the given SELECT SQL query and returns a result object. The Connection is retrieved from the DataSource set in the constructor.

Deprecated:
Use query(java.lang.String,org.apache.commons.dbutils.ResultSetHandler,java.lang.Object[])
Parameters:
<T> The type of object that the handler returns
sql The SQL statement to execute.
params Initialize the PreparedStatement's IN parameters with this array.
rsh The handler used to create the result object from the ResultSet.
Returns:
An object generated by the handler.
Throws:
java.sql.SQLException if a database access error occurs
    @Deprecated
    public <T> T query(String sqlObject[] paramsResultSetHandler<T> rshthrows SQLException {
        Connection conn = this.prepareConnection();
        return this.<T>query(conntruesqlrshparams);
    }

    
Executes the given SELECT SQL query and returns a result object. The Connection is retrieved from the DataSource set in the constructor.

Parameters:
<T> The type of object that the handler returns
sql The SQL statement to execute.
rsh The handler used to create the result object from the ResultSet.
params Initialize the PreparedStatement's IN parameters with this array.
Returns:
An object generated by the handler.
Throws:
java.sql.SQLException if a database access error occurs
    public <T> T query(String sqlResultSetHandler<T> rshObject... paramsthrows SQLException {
        Connection conn = this.prepareConnection();
        return this.<T>query(conntruesqlrshparams);
    }

    
Executes the given SELECT SQL without any replacement parameters. The Connection is retrieved from the DataSource set in the constructor.

Parameters:
<T> The type of object that the handler returns
sql The SQL statement to execute.
rsh The handler used to create the result object from the ResultSet.
Returns:
An object generated by the handler.
Throws:
java.sql.SQLException if a database access error occurs
    public <T> T query(String sqlResultSetHandler<T> rshthrows SQLException {
        Connection conn = this.prepareConnection();
        return this.<T>query(conntruesqlrsh, (Object[]) null);
    }

    
Calls query after checking the parameters to ensure nothing is null.

Parameters:
conn The connection to use for the query call.
closeConn True if the connection should be closed, false otherwise.
sql The SQL statement to execute.
params An array of query replacement parameters. Each row in this array is one set of batch replacement values.
Returns:
The results of the query.
Throws:
java.sql.SQLException If there are database or parameter errors.
    private <T> T query(Connection connboolean closeConnString sqlResultSetHandler<T> rshObject... 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 result = null;
        try {
            stmt = this.prepareStatement(connsql);
            this.fillStatement(stmtparams);
            rs = this.wrap(stmt.executeQuery());
            result = rsh.handle(rs);
        } catch (SQLException e) {
            this.rethrow(esqlparams);
        } finally {
            try {
                close(rs);
            } finally {
                close(stmt);
                if (closeConn) {
                    close(conn);
                }
            }
        }
        return result;
    }

    
Execute an SQL INSERT, UPDATE, or DELETE query without replacement parameters.

Parameters:
conn The connection to use to run the query.
sql The SQL to execute.
Returns:
The number of rows updated.
Throws:
java.sql.SQLException if a database access error occurs
    public int update(Connection connString sqlthrows SQLException {
        return this.update(connfalsesql, (Object[]) null);
    }

    
Execute an SQL INSERT, UPDATE, or DELETE query with a single replacement parameter.

Parameters:
conn The connection to use to run the query.
sql The SQL to execute.
param The replacement parameter.
Returns:
The number of rows updated.
Throws:
java.sql.SQLException if a database access error occurs
    public int update(Connection connString sqlObject paramthrows SQLException {
        return this.update(connfalsesqlnew Object[]{param});
    }

    
Execute an SQL INSERT, UPDATE, or DELETE query.

Parameters:
conn The connection to use to run the query.
sql The SQL to execute.
params The query replacement parameters.
Returns:
The number of rows updated.
Throws:
java.sql.SQLException if a database access error occurs
    public int update(Connection connString sqlObject... paramsthrows SQLException {
        return update(connfalsesqlparams);
    }

    
Executes the given INSERT, UPDATE, or DELETE SQL statement without any replacement parameters. The Connection is retrieved from the DataSource set in the constructor. This Connection must be in auto-commit mode or the update will not be saved.

Parameters:
sql The SQL statement to execute.
Returns:
The number of rows updated.
Throws:
java.sql.SQLException if a database access error occurs
    public int update(String sqlthrows SQLException {
        Connection conn = this.prepareConnection();
        return this.update(conntruesql, (Object[]) null);
    }

    
Executes the given INSERT, UPDATE, or DELETE SQL statement with a single replacement parameter. The Connection is retrieved from the DataSource set in the constructor. This Connection must be in auto-commit mode or the update will not be saved.

Parameters:
sql The SQL statement to execute.
param The replacement parameter.
Returns:
The number of rows updated.
Throws:
java.sql.SQLException if a database access error occurs
    public int update(String sqlObject paramthrows SQLException {
        Connection conn = this.prepareConnection();
        return this.update(conntruesqlnew Object[]{param});
    }

    
Executes the given INSERT, UPDATE, or DELETE SQL statement. The Connection is retrieved from the DataSource set in the constructor. This Connection must be in auto-commit mode or the update will not be saved.

Parameters:
sql The SQL statement to execute.
params Initializes the PreparedStatement's IN (i.e. '?') parameters.
Returns:
The number of rows updated.
Throws:
java.sql.SQLException if a database access error occurs
    public int update(String sqlObject... paramsthrows SQLException {
        Connection conn = this.prepareConnection();
        return this.update(conntruesqlparams);
    }

    
Calls update after checking the parameters to ensure nothing is null.

Parameters:
conn The connection to use for the update call.
closeConn True if the connection should be closed, false otherwise.
sql The SQL statement to execute.
params An array of update replacement parameters. Each row in this array is one set of update replacement values.
Returns:
The number of rows updated.
Throws:
java.sql.SQLException If there are database or parameter errors.
    private int update(Connection connboolean closeConnString sqlObject... paramsthrows SQLException {
        if (conn == null) {
            throw new SQLException("Null connection");
        }
        if (sql == null) {
            if (closeConn) {
                close(conn);
            }
            throw new SQLException("Null SQL statement");
        }
        PreparedStatement stmt = null;
        int rows = 0;
        try {
            stmt = this.prepareStatement(connsql);
            this.fillStatement(stmtparams);
            rows = stmt.executeUpdate();
        } catch (SQLException e) {
            this.rethrow(esqlparams);
        } finally {
            close(stmt);
            if (closeConn) {
                close(conn);
            }
        }
        return rows;
    }
New to GrepCode? Check out our FAQ X