Skip to content

Commit

Permalink
MessageDAO getAllMessages
Browse files Browse the repository at this point in the history
  • Loading branch information
jmsundin committed Jan 30, 2024
1 parent f980e5e commit 0c76bbb
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 @@ -41,5 +41,32 @@ public Message insertMessage(int posted_by, String message_text, long time_poste
}
return null;
}

public List<Message> getAllMessages() {
List<Message> messages = new ArrayList<>();
try {
Connection conn = ConnectionUtil.getConnection();
String sql = "SELECT * FROM message;";
Statement s = conn.createStatement();
ResultSet rs = s.executeQuery(sql);
while (rs.next()) {
Message message = new Message();

int message_id = rs.getInt(1);
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);
messages.add(message);
}
} catch (SQLException e) {
e.printStackTrace();
}
return messages;
}

}

0 comments on commit 0c76bbb

Please sign in to comment.