조회수..

달수·2022년 9월 21일
1

MariaDBBoardDao.java
+ viewCount 메서드 정의

BoardDetailServlet.java
+ 상세보기 페이지에서 viewCount 메서드 호출

MariaDBBoardDao.java
+ 조회수 순 정렬 쿼리 변경

결과물


-> 결과
그냥 findByNO 메서드에서 쿼리문 추가만 하면 됐다...

@Override
  public Board findByNo(int no) throws Exception {
    try (PreparedStatement pstmt = con.prepareStatement(
        "select bno,title,cont,mno,cdt,vw_cnt from app_board where bno=" +no);
        ResultSet rs = pstmt.executeQuery();
        PreparedStatement pstmt2 = con.prepareStatement(
            "update app_board set vw_cnt = vw_cnt + 1 where bno = " + no );
        ResultSet rs2 = pstmt2.executeQuery();) {

      if (!rs.next()) {
        return null;
      }

      Board board = new Board();
      board.no = rs.getInt("bno");
      board.title = rs.getString("title");
      board.content = rs.getString("cont");
      board.memberNo = rs.getInt("mno");
      board.createdDate = rs.getDate("cdt");
      board.viewCount = rs.getInt("vw_cnt");
      return board;
    }
  }

0개의 댓글