From 73407176f75e66bd47f08046496e0a301217a010 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Thu, 31 Oct 2013 14:05:00 +0700 Subject: SQLHelpers.executeInt --- src/com/juick/server/PMQueries.java | 2 +- src/com/juick/server/SQLHelpers.java | 15 +++++++++++++++ src/com/juick/server/TagQueries.java | 19 +++---------------- src/com/juick/server/UserQueries.java | 23 ++--------------------- 4 files changed, 21 insertions(+), 38 deletions(-) (limited to 'src') diff --git a/src/com/juick/server/PMQueries.java b/src/com/juick/server/PMQueries.java index 91ebbee2..6399802f 100644 --- a/src/com/juick/server/PMQueries.java +++ b/src/com/juick/server/PMQueries.java @@ -237,7 +237,7 @@ public class PMQueries { } catch (SQLException e) { System.err.println(e); } finally { - Utils.finishSQL(null, stmt); + Utils.finishSQL(null, stmt2); } return msgs; diff --git a/src/com/juick/server/SQLHelpers.java b/src/com/juick/server/SQLHelpers.java index dd3c4bc3..479fd1cc 100644 --- a/src/com/juick/server/SQLHelpers.java +++ b/src/com/juick/server/SQLHelpers.java @@ -16,6 +16,21 @@ import java.util.ArrayList; */ public class SQLHelpers { + 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 param, int defvalue) { int ret = defvalue; PreparedStatement stmt = null; diff --git a/src/com/juick/server/TagQueries.java b/src/com/juick/server/TagQueries.java index 7d78e345..e811caf4 100644 --- a/src/com/juick/server/TagQueries.java +++ b/src/com/juick/server/TagQueries.java @@ -31,24 +31,11 @@ import java.util.ArrayList; public class TagQueries { public static int getTagID(Connection sql, String tag, boolean autoCreate) { - int tid = 0; - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT tag_id FROM tags WHERE name=?"); - stmt.setString(1, tag); - rs = stmt.executeQuery(); - if (rs.first()) { - tid = rs.getInt(1); - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); - } + int tid = SQLHelpers.getInt(sql, "SELECT tag_id FROM tags WHERE name=?", tag, 0); if (tid == 0 && autoCreate) { + PreparedStatement stmt = null; + ResultSet rs = null; try { stmt = sql.prepareStatement("INSERT INTO tags(name) VALUES (?)", Statement.RETURN_GENERATED_KEYS); stmt.setString(1, tag); diff --git a/src/com/juick/server/UserQueries.java b/src/com/juick/server/UserQueries.java index e56accdb..f127cbca 100644 --- a/src/com/juick/server/UserQueries.java +++ b/src/com/juick/server/UserQueries.java @@ -54,27 +54,8 @@ public class UserQueries { Utils.finishSQL(rs, stmt); } - PreparedStatement stmt2 = null; - try { - stmt2 = sql.prepareStatement("INSERT INTO useroptions(user_id) VALUES (?)"); - stmt2.setInt(1, uid); - stmt2.executeUpdate(); - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(null, stmt); - } - - PreparedStatement stmt3 = null; - try { - stmt3 = sql.prepareStatement("INSERT INTO subscr_users(user_id,suser_id) VALUES (2,?)"); - stmt3.setInt(1, uid); - stmt3.executeUpdate(); - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(null, stmt); - } + SQLHelpers.executeInt(sql, "INSERT INTO useroptions(user_id) VALUES (?)", uid); + SQLHelpers.executeInt(sql, "INSERT INTO subscr_users(user_id,suser_id) VALUES (2,?)", uid); return uid; } -- cgit v1.2.3