From d440876bbb5d80873c107a107620614a326f93d5 Mon Sep 17 00:00:00 2001 From: t00thpick1 Date: Tue, 15 Jan 2013 22:08:59 -0500 Subject: [PATCH] Close statements correctly, add update function --- .../java/com/gmail/nossr50/util/Database.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/main/java/com/gmail/nossr50/util/Database.java b/src/main/java/com/gmail/nossr50/util/Database.java index 5520c96dd..8211f6920 100644 --- a/src/main/java/com/gmail/nossr50/util/Database.java +++ b/src/main/java/com/gmail/nossr50/util/Database.java @@ -243,6 +243,7 @@ public class Database { try { statement = connection.prepareStatement(sql); statement.executeUpdate(); + statement.close(); return true; } catch (SQLException ex) { @@ -263,6 +264,39 @@ public class Database { return false; } + /** + * Returns the number of rows affected by either a DELETE or UPDATE query + * + * @param sql SQL query to execute + * @return the number of rows affected + */ + public int update(String sql) { + int ret = 0; + if (checkConnected()) { + PreparedStatement statement = null; + try { + statement = connection.prepareStatement(sql); + ret = statement.executeUpdate(); + statement.close(); + return ret; + } catch (SQLException ex) { + printErrors(ex); + return 0; + } finally { + if (statement != null) { + try { + statement.close(); + } catch (SQLException e) { + printErrors(e); + return 0; + } + } + } + } + + return ret; + } + /** * Get the Integer. Only return first row / first field. *