From 673c27bdf1af889084e96224481fe2c003b3820c Mon Sep 17 00:00:00 2001 From: Jon Sundin Date: Tue, 30 Jan 2024 01:51:09 +0000 Subject: [PATCH] delete message by id dao method --- src/main/java/DAO/MessageDAO.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/main/java/DAO/MessageDAO.java b/src/main/java/DAO/MessageDAO.java index 6d4a3e8..7412cd5 100644 --- a/src/main/java/DAO/MessageDAO.java +++ b/src/main/java/DAO/MessageDAO.java @@ -95,5 +95,27 @@ public Message getMessageById(int message_id) { } 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; + } + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } }