This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-dbutils.git

commit b3733d7198a2cf4b22cd9dfcce0cf5ecf5fda07c
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Mon Jul 31 15:08:40 2023 -0400

    Method that allocates a resources should release it.
    
    Use try-with-resources
---
 .../org/apache/commons/dbutils/QueryRunner.java    | 102 +++++++++------------
 1 file changed, 41 insertions(+), 61 deletions(-)

diff --git a/src/main/java/org/apache/commons/dbutils/QueryRunner.java 
b/src/main/java/org/apache/commons/dbutils/QueryRunner.java
index f8854b7..39e9edb 100644
--- a/src/main/java/org/apache/commons/dbutils/QueryRunner.java
+++ b/src/main/java/org/apache/commons/dbutils/QueryRunner.java
@@ -720,55 +720,6 @@ public class QueryRunner extends AbstractQueryRunner {
         }
     }
 
-    /**
-     * Calls update after checking the parameters to ensure nothing is null.
-     * @param conn The connection to use for the update call.
-     * @param closeConn True if the connection should be closed, false 
otherwise.
-     * @param sql The SQL statement to execute.
-     * @param params An array of update replacement parameters.  Each row in
-     * this array is one set of update replacement values.
-     * @return The number of rows updated.
-     * @throws SQLException If there are database or parameter errors.
-     */
-    private int update(final Connection conn, final boolean closeConn, final 
String sql, final 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");
-        }
-
-        Statement stmt = null;
-        int rows = 0;
-
-        try {
-            if (params != null && params.length > 0) {
-                final PreparedStatement ps = this.prepareStatement(conn, sql);
-                stmt = ps;
-                this.fillStatement(ps, params);
-                rows = ps.executeUpdate();
-            } else {
-                stmt = conn.createStatement();
-                rows = stmt.executeUpdate(sql);
-            }
-
-        } catch (final SQLException e) {
-            this.rethrow(e, sql, params);
-
-        } finally {
-            close(stmt);
-            if (closeConn) {
-                close(conn);
-            }
-        }
-
-        return rows;
-    }
-
     /**
      * Execute an SQL INSERT, UPDATE, or DELETE query without replacement
      * parameters.
@@ -779,7 +730,7 @@ public class QueryRunner extends AbstractQueryRunner {
      * @throws SQLException if a database access error occurs
      */
     public int update(final Connection conn, final String sql) throws 
SQLException {
-        return this.update(conn, false, sql, (Object[]) null);
+        return this.update(conn, sql, (Object[]) null);
     }
 
     /**
@@ -793,7 +744,7 @@ public class QueryRunner extends AbstractQueryRunner {
      * @throws SQLException if a database access error occurs
      */
     public int update(final Connection conn, final String sql, final Object 
param) throws SQLException {
-        return this.update(conn, false, sql, param);
+        return this.update(conn, sql, new Object[] { param });
     }
 
     /**
@@ -806,7 +757,36 @@ public class QueryRunner extends AbstractQueryRunner {
      * @throws SQLException if a database access error occurs
      */
     public int update(final Connection conn, final String sql, final Object... 
params) throws SQLException {
-        return update(conn, false, sql, params);
+        if (conn == null) {
+            throw new SQLException("Null connection");
+        }
+
+        if (sql == null) {
+            throw new SQLException("Null SQL statement");
+        }
+
+        Statement stmt = null;
+        int rows = 0;
+
+        try {
+            if (params != null && params.length > 0) {
+                final PreparedStatement ps = this.prepareStatement(conn, sql);
+                stmt = ps;
+                this.fillStatement(ps, params);
+                rows = ps.executeUpdate();
+            } else {
+                stmt = conn.createStatement();
+                rows = stmt.executeUpdate(sql);
+            }
+
+        } catch (final SQLException e) {
+            this.rethrow(e, sql, params);
+
+        } finally {
+            close(stmt);
+        }
+
+        return rows;
     }
 
     /**
@@ -821,9 +801,9 @@ public class QueryRunner extends AbstractQueryRunner {
      * @return The number of rows updated.
      */
     public int update(final String sql) throws SQLException {
-        final Connection conn = this.prepareConnection();
-
-        return this.update(conn, true, sql, (Object[]) null);
+        try (Connection conn = this.prepareConnection()) {
+            return this.update(conn, sql, (Object[]) null);
+        }
     }
 
     /**
@@ -839,9 +819,9 @@ public class QueryRunner extends AbstractQueryRunner {
      * @return The number of rows updated.
      */
     public int update(final String sql, final Object param) throws 
SQLException {
-        final Connection conn = this.prepareConnection();
-
-        return this.update(conn, true, sql, param);
+        try (Connection conn = this.prepareConnection()) {
+            return this.update(conn, sql, param);
+        }
     }
 
     /**
@@ -857,8 +837,8 @@ public class QueryRunner extends AbstractQueryRunner {
      * @return The number of rows updated.
      */
     public int update(final String sql, final Object... params) throws 
SQLException {
-        final Connection conn = this.prepareConnection();
-
-        return this.update(conn, true, sql, params);
+        try (Connection conn = this.prepareConnection()) {
+            return this.update(conn, sql, params);
+        }
     }
 }

Reply via email to