푸들푸들

[Error] 댓글 삭제 본문

구디아카데미/Error

[Error] 댓글 삭제

COCO_develop 2024. 10. 23. 17:41

2024/10/23

p1

댓글 삭제

BoardCommentDao.java

public class BoardCommentDao {
	public int deleteComment(Connection conn, int commentNo,int articleNo) throws Exception {
		int row=0;
		String sql = "delete from board_comment where comment_no=? and article_no=?";
		PreparedStatement stmt = conn.prepareStatement(sql);
		stmt.setInt(1, commentNo);
		stmt.setInt(2, articleNo);
		row = stmt.executeUpdate();
		return row;
	}
   }

 

DeleteCommentServlet.java

@WebServlet("/board/deleteComment")
public class DeleteCommentServlet extends HttpServlet{
	// -> C: /board/boardOne
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		String loginId = (String)session.getAttribute("loginId");
		if(loginId == null) { // 로그인이 안됐다면
			response.sendRedirect(request.getContextPath()+"login");
			return;
		}
		
		int commentNo = Integer.parseInt(request.getParameter("commentNo"));
        int articleNo = Integer.parseInt(request.getParameter("articleNo"));
		System.out.println("삭제 댓글 번호: "+commentNo);
		System.out.println("삭제 댓글의 글 번호: "+articleNo);
        
		Connection conn = null;
		try {
			conn = DBUtil.getConnection();
			conn.setAutoCommit(false); // 트랙잭션
			BoardCommentDao boardCommentDao = new BoardCommentDao();
			int row = boardCommentDao.deleteComment(conn, commentNo, articleNo);
			if(row == 1) {
				System.out.println("댓글 삭제 성공");
				conn.commit();  // 성공 시 커밋
			}
			 response.sendRedirect(request.getContextPath()+"/board/boardOne?articleNo="+articleNo);
		} catch (Exception e) {
			System.out.println("댓글 삭제 실패");
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
}
}

 

boardOne.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>BoardOne</title>
</head>
<body>
	<div>
		<c:import url="/WEB-INF/view/inc/header.jsp"></c:import>
	</div>
	<h1>BoardOne</h1>
	
	<div>
		<table border="1">
			<tr>
				<td>No</td>
				<td>${boardArticleOne.articleNo}</td>
			</tr>
			<tr>
				<td>MemberId</td>
				<td>${boardArticleOne.memberId}</td>
			</tr>
			<tr>
				<td>Title</td>
				<td>${boardArticleOne.articleTitle}</td>
			</tr>
			<tr>
				<td>Content</td>
				<td>${boardArticleOne.articleContent}</td>
			</tr>
			<tr>
				<td>Create date</td>
				<td>${boardArticleOne.createdate}</td>
			</tr>
		</table>
	</div>
	
	<!-- File List -->
	<div>
		<c:if test="${boardFileList.size() == 0}">
			첨부된 파일이 없습니다<br>
		</c:if>
		<c:if test="${boardFileList.size() != 0}">
			<table border="1">
				<tr>
					<td>첨부파일</td>
					<td>삭제</td>
				</tr>
				<c:forEach items="${boardFileList}" var="bf">
					<tr>
						<td>
							<a href="${pageContext.request.contextPath}/upload/${bf.filename}.${bf.fileExt}"
										download="${bf.originFilename}.${bf.fileExt}">
								${bf.originFilename}.${bf.fileExt}
							</a>
						</td>
						<td><a href="${pageContext.request.contextPath}/board/deleteFile">삭제</a></td>
					</tr>
				</c:forEach>
			</table>
		</c:if>
		<a href="${pageContext.request.contextPath}/board/insertFile">파일 추가</a>
	</div>
	
	<br>
	<!-- Comment List -->
	<div>
		<form action="${pageContext.request.contextPath}/board/insertComment" method="post">
			<input type="hidden" name="articleNo" value="${boardArticleOne.articleNo}">
        	<input type="hidden" name="memberId" value="${boardArticleOne.memberId}">
			<textarea rows="2" cols="40" name="comment"></textarea>
			<br>
			<button type="submit">댓글입력</button>
		</form>
		<table border="1">
			<c:forEach var="bc" items="${boardCommentList}">
				<tr>
					<td>${bc.comment}</td>
					<td>${bc.memberId}</td>
					<td>${bc.createdate}</td>
					<td>
	                    <c:if test="${boardArticleOne.memberId == bc.memberId}">
	                        <a href="${pageContext.request.contextPath}/board/deleteComment?commentNo=${bc.commentNo}&articleNo=${boardArticleOne.articleNo}">삭제</a>
	                    </c:if>
                	</td>
				</tr>
			</c:forEach>
		</table>
	</div>
	
	<a href="${pageContext.request.contextPath}/board/deleteBoard">글 삭제</a>
	<br><br>
	<a href="${pageContext.request.contextPath}/board/boardList">목록으로</a>
	<div>
		<c:import url="/WEB-INF/view/inc/footer.jsp"></c:import>
	</div>
</body>
</html>

 

 

댓글 삭제 성공 / 실패 메세지가 디버깅도 되지않은 상태였는데

갑자기 삭제 성공?!

띠용..

너무나 갑자기 되어서 원인을 못 찾겠다..

 

'구디아카데미 > Error' 카테고리의 다른 글

[Error] 압축해제 오류 0x80010135  (0) 2024.11.02
[Error] 오류메세지X, 404  (4) 2024.10.24
[Error] Sql 쿼리 INSERT문  (0) 2024.10.23
[Error] 빈 화면  (0) 2024.10.23
Error  (1) 2024.10.23