diff options
author | Ugnich Anton | 2013-06-20 18:26:34 +0700 |
---|---|---|
committer | Ugnich Anton | 2013-06-20 18:26:34 +0700 |
commit | 505feda055a1f5c59aea53f26680bbbbb0553a0a (patch) | |
tree | 280e2f2bc73c002b098c947f611a609762727dbc /src/com/juick | |
parent | a1ecda62eef3b802dbfa044f75ec0050de6ba75e (diff) |
SQLHelpers.getInt
Diffstat (limited to 'src/com/juick')
-rw-r--r-- | src/com/juick/server/SQLHelpers.java | 19 | ||||
-rw-r--r-- | src/com/juick/server/UserQueries.java | 68 |
2 files changed, 23 insertions, 64 deletions
diff --git a/src/com/juick/server/SQLHelpers.java b/src/com/juick/server/SQLHelpers.java index dd83a915d..7879c2448 100644 --- a/src/com/juick/server/SQLHelpers.java +++ b/src/com/juick/server/SQLHelpers.java @@ -16,6 +16,25 @@ import java.util.ArrayList; */ public class SQLHelpers { + 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 ArrayList<Integer> getArrayInteger(Connection sql, String query, int param) { ArrayList<Integer> ret = new ArrayList<Integer>(); diff --git a/src/com/juick/server/UserQueries.java b/src/com/juick/server/UserQueries.java index 652809d6c..e44017fa9 100644 --- a/src/com/juick/server/UserQueries.java +++ b/src/com/juick/server/UserQueries.java @@ -367,78 +367,18 @@ public class UserQueries { } public static int getStatsIRead(Connection sql, int uid) { - int ret = 0; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT COUNT(*) FROM subscr_users WHERE suser_id=?"); - stmt.setInt(1, uid); - rs = stmt.executeQuery(); - if (rs.first()) { - ret = rs.getInt(1); - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); - } - return ret; + return SQLHelpers.getInt(sql, "SELECT COUNT(*) FROM subscr_users WHERE suser_id=?", uid, 0); } public static int getStatsMyReaders(Connection sql, int uid) { - int ret = 0; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT COUNT(*) FROM subscr_users WHERE user_id=?"); - stmt.setInt(1, uid); - rs = stmt.executeQuery(); - if (rs.first()) { - ret = rs.getInt(1); - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); - } - return ret; + return SQLHelpers.getInt(sql, "SELECT COUNT(*) FROM subscr_users WHERE user_id=?", uid, 0); } public static int getStatsMessages(Connection sql, int uid) { - int ret = 0; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT COUNT(*) FROM messages WHERE user_id=?"); - stmt.setInt(1, uid); - rs = stmt.executeQuery(); - if (rs.first()) { - ret = rs.getInt(1); - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); - } - return ret; + return SQLHelpers.getInt(sql, "SELECT COUNT(*) FROM messages WHERE user_id=?", uid, 0); } public static int getStatsReplies(Connection sql, int uid) { - int ret = 0; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT COUNT(*) FROM replies WHERE user_id=?"); - stmt.setInt(1, uid); - rs = stmt.executeQuery(); - if (rs.first()) { - ret = rs.getInt(1); - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); - } - return ret; + return SQLHelpers.getInt(sql, "SELECT COUNT(*) FROM replies WHERE user_id=?", uid, 0); } } |