From 0ac9d76d6348f487ada697678395d15abee244c9 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 15 Jan 2016 15:20:29 +0300 Subject: spring-jdbc WIP --- .../java/com/juick/server/MessagesQueries.java | 48 +++++++++------------- 1 file changed, 19 insertions(+), 29 deletions(-) (limited to 'src/main/java/com/juick') diff --git a/src/main/java/com/juick/server/MessagesQueries.java b/src/main/java/com/juick/server/MessagesQueries.java index 91ee1913..4086cb13 100644 --- a/src/main/java/com/juick/server/MessagesQueries.java +++ b/src/main/java/com/juick/server/MessagesQueries.java @@ -272,35 +272,25 @@ public class MessagesQueries { }); } - public static com.juick.Message getReply(Connection sql, int mid, int rid) { - com.juick.Message msg = null; - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT replies.user_id,users.nick,replies.replyto,DATE_FORMAT(replies.ts,'%Y-%m-%d %H:%i:%s'),replies.attach,replies.txt FROM replies INNER JOIN users ON replies.user_id=users.id WHERE replies.message_id=? AND replies.reply_id=?"); - stmt.setInt(1, mid); - stmt.setInt(2, rid); - rs = stmt.executeQuery(); - if (rs.first()) { - msg = new com.juick.Message(); - msg.MID = mid; - msg.RID = rid; - msg.User = new com.juick.User(); - msg.User.UID = rs.getInt(1); - msg.User.UName = rs.getString(2); - msg.ReplyTo = rs.getInt(3); - msg.TimestampString = rs.getString(4); - msg.AttachmentType = rs.getString(5); - msg.Text = rs.getString(6); - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } - - return msg; + public static com.juick.Message getReply(JdbcTemplate sql, int mid, int rid) { + return sql.queryForObject("SELECT replies.user_id,users.nick," + + "replies.replyto,DATE_FORMAT(replies.ts,'%Y-%m-%d %H:%i:%s')," + + "replies.attach,replies.txt FROM replies INNER JOIN users " + + "ON replies.user_id=users.id " + + "WHERE replies.message_id=? AND replies.reply_id=?", + new Object[]{mid, rid}, (rs, num) -> { + Message msg = new Message(); + msg.MID = mid; + msg.RID = rid; + msg.User = new com.juick.User(); + msg.User.UID = rs.getInt(1); + msg.User.UName = rs.getString(2); + msg.ReplyTo = rs.getInt(3); + msg.TimestampString = rs.getString(4); + msg.AttachmentType = rs.getString(5); + msg.Text = rs.getString(6); + return msg; + }); } public static User getMessageAuthor(JdbcTemplate sql, int mid) { -- cgit v1.2.3