From dc22c4a67d72eb2fa5905d2e8bd7424749fad12b Mon Sep 17 00:00:00 2001 From: Jon Sundin Date: Tue, 30 Jan 2024 01:22:14 +0000 Subject: [PATCH] get message by id DAO method --- src/main/java/DAO/MessageDAO.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/main/java/DAO/MessageDAO.java b/src/main/java/DAO/MessageDAO.java index 4050213..6d4a3e8 100644 --- a/src/main/java/DAO/MessageDAO.java +++ b/src/main/java/DAO/MessageDAO.java @@ -68,5 +68,32 @@ public List getAllMessages() { } return messages; } + + public Message getMessageById(int message_id) { + try { + Connection conn = ConnectionUtil.getConnection(); + String sql = "SELECT * FROM message WHERE message_id = ?;"; + PreparedStatement ps = conn.prepareStatement(sql); + + ps.setInt(1, message_id); + ResultSet rs = ps.executeQuery(); + + while (rs.next()) { + Message message = new Message(); + int posted_by = rs.getInt(2); + String message_text = rs.getString(3); + long time_posted_epoch = rs.getLong(4); + + message.setMessage_id(message_id); + message.setPosted_by(posted_by); + message.setMessage_text(message_text); + message.setTime_posted_epoch(time_posted_epoch); + return message; + } + } catch (SQLException e) { + e.printStackTrace(); + } + return null; + } }