Skip to content

Commit

Permalink
get all messages by account id DAO method
Browse files Browse the repository at this point in the history
  • Loading branch information
jmsundin committed Jan 31, 2024
1 parent b588116 commit b74a520
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/main/java/DAO/MessageDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,28 @@ public Message deleteMessageById(int message_id) {
return null;
}

public List<Message> getAllMessagesByAccountId(int account_id) {
List<Message> 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;
}

}

0 comments on commit b74a520

Please sign in to comment.