Skip to content

Commit

Permalink
get message by id DAO method
Browse files Browse the repository at this point in the history
  • Loading branch information
jmsundin committed Jan 30, 2024
1 parent d6c2ae9 commit dc22c4a
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/main/java/DAO/MessageDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,32 @@ public List<Message> 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;
}

}

0 comments on commit dc22c4a

Please sign in to comment.