diff artifact-database/src/main/java/de/intevation/artifactdatabase/SQLExecutor.java @ 174:25d472a67a9f

Reduce the code repetition and the complexity of the backend. artifacts/trunk@1400 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 04 Mar 2011 16:43:03 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/SQLExecutor.java	Fri Mar 04 16:43:03 2011 +0000
@@ -0,0 +1,102 @@
+/*
+ * Copyright (c) 2011 by Intevation GmbH
+ *
+ * This program is free software under the LGPL (>=v2.1)
+ * Read the file LGPL.txt coming with the software for details
+ * or visit http://www.gnu.org/licenses/ if it does not exist.
+ */
+package de.intevation.artifactdatabase;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import javax.sql.DataSource;
+
+import org.apache.log4j.Logger;
+
+public class SQLExecutor {
+
+    private static Logger logger = Logger.getLogger(SQLExecutor.class);
+
+    public Connection        conn;
+    public PreparedStatement stmnt;
+    public ResultSet         result;
+
+    public SQLExecutor() {
+    }
+
+    public void reset() throws SQLException {
+        if (result != null) {
+            result.close();
+            result = null;
+        }
+        if (stmnt != null) {
+            result = null;
+            stmnt.close();
+        }
+    }
+
+    public PreparedStatement prepareStatement(String query)
+    throws SQLException {
+        return stmnt = conn.prepareStatement(query);
+    }
+
+    public void close() {
+        if (result != null) {
+            try { result.close(); }
+            catch (SQLException sqle) {}
+        }
+        if (stmnt != null) {
+            try { stmnt.close(); }
+            catch (SQLException sqle) {}
+        }
+        if (conn != null) {
+            try { conn.close(); }
+            catch (SQLException sqle) {}
+        }
+    }
+
+    public boolean runWrite() {
+        DataSource dataSource = DBConnection.getDataSource();
+        try {
+            conn = dataSource.getConnection();
+            try {
+                conn.setAutoCommit(false);
+                return doIt();
+            }
+            catch (SQLException sqle) {
+                conn.rollback();
+                throw sqle;
+            }
+        }
+        catch (SQLException sqle) {
+            logger.error(sqle.getLocalizedMessage(), sqle);
+        }
+        finally {
+            close();
+        }
+        return false;
+    }
+
+    public boolean runRead() {
+        DataSource dataSource = DBConnection.getDataSource();
+        try {
+            conn = dataSource.getConnection();
+            return doIt();
+        }
+        catch (SQLException sqle) {
+            logger.error(sqle.getLocalizedMessage(), sqle);
+        }
+        finally {
+            close();
+        }
+        return false;
+    }
+
+    public boolean doIt() throws SQLException {
+        return true;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org