From b944a64c6b54b9819542ab2d43d85114ef2908e1 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sat, 24 Aug 2013 15:07:51 +0700 Subject: PMQueries; UserQueries.getUserByUID; UserQueries.fillUsersByID --- src/com/juick/server/UserQueries.java | 62 +++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) (limited to 'src/com/juick/server/UserQueries.java') diff --git a/src/com/juick/server/UserQueries.java b/src/com/juick/server/UserQueries.java index c170b6cd..bb4d19df 100644 --- a/src/com/juick/server/UserQueries.java +++ b/src/com/juick/server/UserQueries.java @@ -79,6 +79,28 @@ public class UserQueries { return uid; } + public static com.juick.User getUserByUID(Connection sql, int uid) { + com.juick.User user = null; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT nick FROM users WHERE id=?"); + stmt.setInt(1, uid); + rs = stmt.executeQuery(); + if (rs.first()) { + user = new com.juick.User(); + user.UID = uid; + user.UName = rs.getString(1); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return user; + } + public static com.juick.User getUserByName(Connection sql, String username) { com.juick.User user = null; @@ -148,6 +170,42 @@ public class UserQueries { return users; } + public static boolean fillUsersByID(Connection sql, ArrayList users) { + boolean ret = false; + + String uids = ""; + final int usersSize = users.size(); + for (int i = 0; i < usersSize; i++) { + if (i > 0) { + uids += ","; + } + uids += users.get(i).UID; + } + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT id,nick FROM users WHERE id IN (" + uids + ")"); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + int uid = rs.getInt(1); + for (int i = 0; i < usersSize; i++) { + if (users.get(i).UID == uid) { + users.get(i).UName = rs.getString(2); + ret = true; + } + } + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return ret; + } + public static ArrayList getUsersByJID(Connection sql, ArrayList jids) { ArrayList users = new ArrayList(); @@ -180,6 +238,10 @@ public class UserQueries { return SQLHelpers.getInt(sql, "SELECT user_id FROM jids WHERE jid=?", jid, 0); } + public static int getUIDbyName(Connection sql, String uname) { + return SQLHelpers.getInt(sql, "SELECT id FROM users WHERE nick=?", uname, 0); + } + public static int getUIDbyHash(Connection sql, String hash) { return SQLHelpers.getInt(sql, "SELECT user_id FROM logins WHERE hash=?", hash, 0); } -- cgit v1.2.3