1. 현재 댓글 기능 문제점
- 현재 작성한 댓글을 리스트로 보여지는 과정에서 해당 글에 해당하는 댓글뿐만 아니라 다른 글의 댓글까지 함께 떠서 수정이 필요함
- 댓글이 작성되면 댓글수도 함께 업데이트되도록 기능을 추가해야함
2. 문제 해결
2.1 현재 작성한 댓글을 리스트로 보여지는 과정에서 해당 글에 해당하는 댓글뿐만 아니라 다른 글의 댓글까지 함께 떠서 수정이 필요함(해결)
<c:forEach items="${commentRowList}" var="row">
<c:if test="${row.id == boardData.id}">
<tr>
<td>${row.commentAuthor}</td>
<td>${row.commentContent}</td>
<td>${row.writeDate}</td>
<td>${row.writeTime}</td>
</tr>
</c:if>
</c:forEach>
- 해당하는 글을 클릭했을 때 댓글의 id와 글의 id와 같을 시 표시하도록 수정
2.2 댓글이 작성되면 댓글수도 함께 업데이트되도록 기능을 추가해야함(완전한 해결 x 글 리스트에서는 댓글 수가 업데이트되지 않음)
public int commentNum(int id) throws ClassNotFoundException, SQLException {
// db에 접속해서
Class.forName("org.mariadb.jdbc.Driver");
Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PW);
PreparedStatement pstmt = null;
ResultSet rs = null;
pstmt = conn.prepareStatement("select count(*) as count\n" +
"from comment \n" +
"where id = ?\n" +
"group by id ");
pstmt.setInt(1, id);
rs = pstmt.executeQuery();
int commentNum1 = 0;
if (rs.next()) {
commentNum1 = rs.getInt("count");
System.out.println("commentNum1 : " + commentNum1);
return commentNum1;
}
return commentNum1;
}
- 첫번째 방법 : 댓글 수를 count해서 댓글 수를 보여주도록 수정
- (문제점) boardRead.bbs에서는 댓글 수가 제대로 출력되지만 BoardListCmd에서의 null 에러가 발생 그래서 board 테이블 자체의 commentCount를 업데이트하기로 함
2.3 댓글이 작성되면 댓글수도 함께 업데이트되도록 기능을 추가해야함(해결)
public void updateCommentCount(int id) throws ClassNotFoundException, SQLException{
// db에 접속해서
Class.forName("org.mariadb.jdbc.Driver");
Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PW);
PreparedStatement pstmt = null;
ResultSet rs = null;
pstmt = conn.prepareStatement("update board b \n" +
"set b.commentCount =(select count(*) as count\n" +
"\t\t\t\t\tfrom comment \n" +
"\t\t\t\t\twhere id = ?\n" +
"\t\t\t\t\tgroup by id)\n" +
"where b.id = ?");
pstmt.setInt(1, id);
pstmt.setInt(2, id);
rs = pstmt.executeQuery();
if(rs.next()){
rs.getInt("commentcount");
}
}
- 두번째 방법 : 서브쿼리를 사용해서 comment 테이블에서 댓글 수를 count하고 board 테이블과 comment 테이블의 id 값이 같을 시 commentCount를 업데이트로 하도록 추가
- 위 방법으로 board 테이블의 commentCount가 업데이트되어 boardList.bbs에서도 댓글 수가 잘 출력됨
'JSP' 카테고리의 다른 글
2021-09-06(jspmvc 시스템 구성도) (0) | 2021.09.08 |
---|---|
2021-09-03(게시판-답글 기능) (0) | 2021.09.04 |
2021-08-30(게시판 - 댓글 기능) (0) | 2021.08.30 |
2021-08-26(게시판 프로젝트 구조) (0) | 2021.08.26 |
2021-08-25(JSP-JSTL) (0) | 2021.08.25 |