From b74a520a989ef520d30cb15d4c1c19719093ccac Mon Sep 17 00:00:00 2001 From: Jon Sundin Date: Wed, 31 Jan 2024 02:43:11 +0000 Subject: [PATCH] get all messages by account id DAO method --- src/main/java/DAO/MessageDAO.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/main/java/DAO/MessageDAO.java b/src/main/java/DAO/MessageDAO.java index ad0ceea..9b0ac2a 100644 --- a/src/main/java/DAO/MessageDAO.java +++ b/src/main/java/DAO/MessageDAO.java @@ -137,4 +137,28 @@ public Message deleteMessageById(int message_id) { return null; } + public List getAllMessagesByAccountId(int account_id) { + List messages = new ArrayList<>(); + try { + Connection conn = ConnectionUtil.getConnection(); + String sql = "SELECT * FROM message WHERE posted_by = ?"; + PreparedStatement ps = conn.prepareStatement(sql); + ps.setInt(1, account_id); + + ResultSet rs = ps.executeQuery(); + while (rs.next()) { + Message message = new Message(); + message.setMessage_id(rs.getInt(1)); + message.setPosted_by(rs.getInt(2)); + message.setMessage_text(rs.getString(3)); + message.setTime_posted_epoch(rs.getLong(4)); + messages.add(message); + } + + } catch (SQLException e) { + e.printStackTrace(); + } + return messages; + } + }