aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/server/SQLHelpers.java
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2015-10-24 18:09:15 +0300
committerGravatar Vitaly Takmazov2015-10-24 18:09:15 +0300
commitd00fd7705cb1a4b085eed7f34df2f1c6d9a69f76 (patch)
tree70a83e0e30a4195855484f820c0322e84405bc04 /src/main/java/com/juick/server/SQLHelpers.java
parentb5df0e1c2664a8a1a0f9042745dd608773fedce3 (diff)
moving to Gradle
Diffstat (limited to 'src/main/java/com/juick/server/SQLHelpers.java')
-rw-r--r--src/main/java/com/juick/server/SQLHelpers.java163
1 files changed, 163 insertions, 0 deletions
diff --git a/src/main/java/com/juick/server/SQLHelpers.java b/src/main/java/com/juick/server/SQLHelpers.java
new file mode 100644
index 00000000..f5569993
--- /dev/null
+++ b/src/main/java/com/juick/server/SQLHelpers.java
@@ -0,0 +1,163 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package com.juick.server;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+
+/**
+ *
+ * @author ugnich
+ */
+public class SQLHelpers {
+
+ public static int execute(Connection sql, String query) {
+ int ret = -1;
+ PreparedStatement stmt = null;
+ try {
+ stmt = sql.prepareStatement(query);
+ ret = stmt.executeUpdate();
+ } catch (SQLException e) {
+ System.err.println(e);
+ } finally {
+ Utils.finishSQL(null, stmt);
+ }
+ return ret;
+ }
+
+ public static int executeInt(Connection sql, String query, int param) {
+ int ret = -1;
+ PreparedStatement stmt = null;
+ try {
+ stmt = sql.prepareStatement(query);
+ stmt.setInt(1, param);
+ ret = stmt.executeUpdate();
+ } catch (SQLException e) {
+ System.err.println(e);
+ } finally {
+ Utils.finishSQL(null, stmt);
+ }
+ return ret;
+ }
+
+ public static int getInt(Connection sql, String query, int defvalue) {
+ int ret = defvalue;
+ PreparedStatement stmt = null;
+ ResultSet rs = null;
+ try {
+ stmt = sql.prepareStatement(query);
+ rs = stmt.executeQuery();
+ if (rs.first()) {
+ ret = rs.getInt(1);
+ }
+ } catch (SQLException e) {
+ System.err.println(e);
+ } finally {
+ Utils.finishSQL(rs, stmt);
+ }
+ return ret;
+ }
+
+ public static int getInt(Connection sql, String query, int param, int defvalue) {
+ int ret = defvalue;
+ PreparedStatement stmt = null;
+ ResultSet rs = null;
+ try {
+ stmt = sql.prepareStatement(query);
+ stmt.setInt(1, param);
+ rs = stmt.executeQuery();
+ if (rs.first()) {
+ ret = rs.getInt(1);
+ }
+ } catch (SQLException e) {
+ System.err.println(e);
+ } finally {
+ Utils.finishSQL(rs, stmt);
+ }
+ return ret;
+ }
+
+ public static int getInt(Connection sql, String query, String param, int defvalue) {
+ int ret = defvalue;
+ PreparedStatement stmt = null;
+ ResultSet rs = null;
+ try {
+ stmt = sql.prepareStatement(query);
+ stmt.setString(1, param);
+ rs = stmt.executeQuery();
+ if (rs.first()) {
+ ret = rs.getInt(1);
+ }
+ } catch (SQLException e) {
+ System.err.println(e);
+ } finally {
+ Utils.finishSQL(rs, stmt);
+ }
+ return ret;
+ }
+
+ public static String getString(Connection sql, String query, int param) {
+ String ret = null;
+ PreparedStatement stmt = null;
+ ResultSet rs = null;
+ try {
+ stmt = sql.prepareStatement(query);
+ stmt.setInt(1, param);
+ rs = stmt.executeQuery();
+ if (rs.first()) {
+ ret = rs.getString(1);
+ }
+ } catch (SQLException e) {
+ System.err.println(e);
+ } finally {
+ Utils.finishSQL(rs, stmt);
+ }
+ return ret;
+ }
+
+ public static String getString(Connection sql, String query, String param) {
+ String ret = null;
+ PreparedStatement stmt = null;
+ ResultSet rs = null;
+ try {
+ stmt = sql.prepareStatement(query);
+ stmt.setString(1, param);
+ rs = stmt.executeQuery();
+ if (rs.first()) {
+ ret = rs.getString(1);
+ }
+ } catch (SQLException e) {
+ System.err.println(e);
+ } finally {
+ Utils.finishSQL(rs, stmt);
+ }
+ return ret;
+ }
+
+ public static ArrayList<Integer> getArrayInteger(Connection sql, String query, int param) {
+ ArrayList<Integer> ret = new ArrayList<Integer>();
+
+ PreparedStatement stmt = null;
+ ResultSet rs = null;
+ try {
+ stmt = sql.prepareStatement(query);
+ stmt.setInt(1, param);
+ rs = stmt.executeQuery();
+ rs.beforeFirst();
+ while (rs.next()) {
+ ret.add(rs.getInt(1));
+ }
+ } catch (SQLException e) {
+ System.err.println(e);
+ } finally {
+ Utils.finishSQL(rs, stmt);
+ }
+
+ return ret;
+ }
+}