Skip to content

Commit

Permalink
added updateMessageById
Browse files Browse the repository at this point in the history
  • Loading branch information
jmsundin committed Jan 31, 2024
1 parent 0e3058e commit 59b4ee8
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/main/java/DAO/MessageDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public Message insertMessage(int posted_by, String message_text, long time_poste
try {
Connection conn = ConnectionUtil.getConnection();
String sql = "INSERT INTO message (posted_by, message_text, time_posted_epoch) "
+ "VALUES (?, ?, ?)";
+ "VALUES (?, ?, ?)";

PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
ps.setInt(1, posted_by);
ps.setString(2, message_text);
Expand Down Expand Up @@ -96,18 +96,37 @@ public Message getMessageById(int message_id) {
return null;
}

public Message updateMessageById(int message_id, String updated_message_text) {
try {
Connection conn = ConnectionUtil.getConnection();
String sql = "UPDATE message SET message_text = ? WHERE message_id = ?;";
PreparedStatement ps = conn.prepareStatement(sql);

ps.setString(1, updated_message_text);
ps.setInt(2, message_id);
int updatedRows = ps.executeUpdate();

while (updatedRows > 0) {
return getMessageById(message_id);
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}

public Message deleteMessageById(int message_id) {
Message message = getMessageById(message_id);
if (message != null) {
try {
Connection conn = ConnectionUtil.getConnection();
String sql = "DELETE FROM message WHERE message_id = ?";
PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);

ps.setInt(1, message_id);

int deletedEntity = ps.executeUpdate();

if (deletedEntity > 0) {
return message;
}
Expand All @@ -117,5 +136,5 @@ public Message deleteMessageById(int message_id) {
}
return null;
}

}

0 comments on commit 59b4ee8

Please sign in to comment.