dgraham 2003/10/19 12:38:51
Modified: dbutils/src/java/org/apache/commons/dbutils QueryRunner.java
Log:
Added more execute methods.
Revision Changes Path
1.3 +105 -65
jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/QueryRunner.java
Index: QueryRunner.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/QueryRunner.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- QueryRunner.java 19 Oct 2003 19:22:33 -0000 1.2
+++ QueryRunner.java 19 Oct 2003 19:38:51 -0000 1.3
@@ -72,7 +72,7 @@
/**
* Executes SQL queries with pluggable strategies for handling
- * <code>ResultSet</code>s.
+ * <code>ResultSet</code>s. This class is thread safe.
*
* @see ResultSetHandler
* @see ResultSetMetaDataHandler
@@ -201,8 +201,9 @@
result = rsh.handle(rs, params, userObject);
- } catch (SQLException sqle) {
- this.rethrow(sqle, query, params);
+ } catch (SQLException e) {
+ this.rethrow(e, query, params);
+
} finally {
DbUtils.closeQuietly(rs);
DbUtils.closeQuietly(stmt);
@@ -222,6 +223,59 @@
}
/**
+ * Executes the given SELECT SQL query and returns a result object.
+ * @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
+ */
+ public Object executeQuery(String sql, Object[] params, ResultSetHandler rsh)
+ throws SQLException {
+
+ Connection conn = this.ds.getConnection();
+
+ Object result = null;
+
+ try {
+ result = this.executeQuery(conn, sql, params, rsh, null, null);
+
+ } finally {
+ DbUtils.close(conn);
+ }
+
+ return result;
+ }
+
+ /**
+ * Executes the given SELECT SQL without any replacement parameters.
+ * @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
+ */
+ public Object executeQuery(String sql, ResultSetHandler rsh)
+ throws SQLException {
+
+ return this.executeQuery(sql, null, rsh);
+ }
+
+ /**
+ * Execute an SQL INSERT, UPDATE, or DELETE query without replacement
+ * parameters.
+ * @param conn The connection to use to run the query.
+ * @param query The SQL to execute.
+ * @return The number of rows updated.
+ * @throws SQLException
+ */
+ public int executeUpdate(Connection conn, String query) throws SQLException {
+ return this.executeUpdate(conn, query, null);
+ }
+
+ /**
* Execute an SQL INSERT, UPDATE, or DELETE query.
* @param conn The connection to use to run the query.
* @param query The SQL to execute.
@@ -251,6 +305,51 @@
}
/**
+ * Executes the given INSERT, UPDATE, or DELETE SQL statement without
+ * any replacement parameters. 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.
+ * @throws SQLException
+ * @return The number of rows updated.
+ */
+ public int executeUpdate(String sql) throws SQLException {
+ return this.executeUpdate(sql, null);
+ }
+
+ /**
+ * 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 Initializes the PreparedStatement's IN (ie. '?')
+ * parameters.
+ * @throws SQLException
+ * @return The number of rows updated.
+ */
+ public int executeUpdate(String sql, Object[] params) throws SQLException {
+
+ Connection conn = this.ds.getConnection();
+
+ int rows = 0;
+
+ try {
+ conn.setAutoCommit(false); // single transaction.
+ rows = this.executeUpdate(conn, sql, params);
+ conn.commit();
+
+ } catch (SQLException e) {
+ DbUtils.rollback(conn);
+ throw e;
+
+ } finally {
+ DbUtils.close(conn);
+ }
+
+ return rows;
+ }
+
+ /**
* Fill the <code>PreparedStatement</code> replacement parameters with
* the given objects.
* @param stmt
@@ -313,65 +412,6 @@
*/
public void setDataSource(DataSource dataSource) {
this.ds = dataSource;
- }
-
- /**
- * Executes the given SELECT SQL query and returns a result object.
- * @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
- */
- public Object executeQuery(String sql, Object[] params, ResultSetHandler rsh)
- throws SQLException {
-
- Connection conn = this.ds.getConnection();
-
- Object result = null;
-
- try {
- result = this.executeQuery(conn, sql, params, rsh, null, null);
-
- } finally {
- DbUtils.close(conn);
- }
-
- return result;
- }
-
- /**
- * 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 Initializes the PreparedStatement's IN (ie. '?')
- * parameters.
- * @throws SQLException
- * @return The number of rows updated.
- */
- public int executeUpdate(String sql, Object[] params) throws SQLException {
-
- Connection conn = this.ds.getConnection();
- conn.setAutoCommit(false); // single transaction.
-
- int rows = 0;
-
- try {
- rows = this.executeUpdate(conn, sql, params);
- conn.commit();
-
- } catch (SQLException e) {
- DbUtils.rollback(conn);
- throw e;
-
- } finally {
- DbUtils.close(conn);
- }
-
- return rows;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]