댓글 삭제
📍 ajax는 비동기 방식이라서 수행 후 함수 호출해야 화면에 바로 적용됨
<button onclick="deleteReply(${reply.replyNo})">삭제</button>
// 댓글 삭제
function deleteReply(replyNo){
if(confirm("정말 삭제하시겠습니까?")){
// 요청 주소 : /community/reply/delete
// 파라미터 : key : "replyNo", value : 매개변수 replyNo
// 전달방식 : "GET"
// success : 삭제 성공 시 -> " 삭제되었습니다." alert로 출력 후
// 댓글 목록 비동기 조회 함수 호출
// 삭제 실패 시 -> "삭제 실패" alert로 출력
// error : 앞 error 코드 참고
// DB에서 댓글 삭제 ==> REPLY_ST = 'Y' 변경
$.ajax({
url : contextPath + "/reply/delete",
data : {"replyNo" : replyNo},
success : function(result){
if(result>0){
alert("삭제되었습니다.");
selectReplyList();
} else {
alert("삭제 실패");
}
},
error : function(req, status, error){
console.log("댓글 삭제 실패");
console.log(req.responseText);
}
})
}
}
// 댓글 삭제
if(command.equals("delete")) {
int replyNo = Integer.parseInt(req.getParameter("replyNo"));
int result = service.deleteReply(replyNo);
resp.getWriter().print(result);
}
/** 댓글 삭제 service
* @param replyNo
* @return result
* @throws Exception
*/
public int deleteReply(int replyNo) throws Exception {
Connection conn = getConnection();
int result = dao.deleteReply(conn, replyNo);
if(result>0) commit(conn);
else rollback(conn);
close(conn);
return result;
}
/** 댓글 삭제 DAO
* @param conn
* @param replyNo
* @return result
* @throws Exception
*/
public int deleteReply(Connection conn, int replyNo) throws Exception{
int result = 0;
try {
String sql = prop.getProperty("deleteReply");
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, replyNo);
result = pstmt.executeUpdate();
} finally {
close(pstmt);
}
return result;
}
<!-- 댓글 삭제 -->
<entry key="deleteReply">
UPDATE REPLY
SET REPLY_ST='Y'
WHERE REPLY_NO=?
</entry>