Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Tags
more
Archives
Today
Total
관리 메뉴

개발 일기

JAVA 게시판 만들기 3 탄 (feat. DAO 클래스 만들기 2 (DB관련 필요 서비스 내용 + 댓글(reply) 추가하기) ) 본문

Project 1. 게시판 만들기

JAVA 게시판 만들기 3 탄 (feat. DAO 클래스 만들기 2 (DB관련 필요 서비스 내용 + 댓글(reply) 추가하기) )

개발 일기 2021. 8. 22. 22:18

안녕하세요!

 

지난번에 이어 게시판 관련 DAO 클래스를 추가로 작성하도록 하겠습니다.

 

 

1. Reply Create (댓글 생성)

public void addReply(Reply reply) throws Exception {
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
			conn = dataSource.getConnection();
			pstmt = conn.prepareStatement(ReplySql.REPLY_CREATE);
			pstmt.setString(1, reply.getReplyContent());
			pstmt.setInt(2, reply.getQnaNo());
			pstmt.setInt(3, reply.getMemberNo());

			pstmt.executeUpdate();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (pstmt != null) pstmt.close();
			if (conn != null) conn.close();
		}

	}
    
    //사용한 SQL 문
    //insert into REPLY(REPLY_NO, REPLY_CONTENT, QNA_NO, MEMBER_NO) values (REPLY_SEQ.nextval, ?, ?, ?)

 - Reply 객체를 인자로 받아서 insert문에 추가 (Reply DTO 클래스 : https://soonggi.tistory.com/68)

 

 

 

2. Reply Update (댓글 수정)

public void modifyReply(Reply reply) throws Exception {
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
			conn = dataSource.getConnection();
			pstmt = conn.prepareStatement(ReplySql.REPLY_UPDATE);
			pstmt.setString(1, reply.getReplyContent());
			pstmt.setInt(2, reply.getReplyNo());
			pstmt.setInt(3, reply.getMemberNo());
			pstmt.executeUpdate();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (pstmt != null) pstmt.close();
			if (conn != null) conn.close();
		}
		
	}
    
    //사용한 SQL문
    //update REPLY set REPLY_CONTENT=? where REPLY_NO=? and MEMBER_NO =?

 - Reply 객체를 인자로 받아 update문에 추가

 

 

 

3. Reply Delete (댓글 삭제)

public void removeReply(int replyNo) throws Exception {
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
			conn = dataSource.getConnection();
			pstmt = conn.prepareStatement(ReplySql.REPLY_DELETE);
			pstmt.setInt(1, replyNo);
			pstmt.executeUpdate();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (pstmt != null) pstmt.close();
			if (conn != null) conn.close();
		}
	}
    
    //사용한 SQL문
    //DELETE FROM REPLY WHERE REPLY_NO=?

 

 

 

4. Qna List(전체 게시물 조회)

public ArrayList<Qna> getQnaList(int start, int last) throws Exception{
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		ArrayList<Qna> qnas = new ArrayList<Qna>();

		try {
			conn = dataSource.getConnection();
			StringBuffer sql = new StringBuffer(500);
			
			sql.append("select * from");
			sql.append("(");
			sql.append("select rownum idx, s.*");
			sql.append("from ");
			
			sql.append("( ");
			sql.append("select ");
			sql.append("qna_no, member_no, qna_title, qna_reg_date, ");
			sql.append("qna_visible, qna_file, qna_views, ");
			sql.append("(select count(*) from reply where qna_no = a.qna_no) ");
			sql.append("from ");
			sql.append("qna a ");
			sql.append("order by qna_no desc");
			
			sql.append(") s ");
			sql.append(")");
			
			sql.append("where idx >= ? and idx <= ? ");

			pstmt = conn.prepareStatement(sql.toString());
			pstmt.setInt(1, start);
			pstmt.setInt(2, last);
			rs = pstmt.executeQuery();
			
			while (rs.next()) {
				Qna qna = new Qna();
				qna.setQnaNo(rs.getInt(2));
				qna.setMemberNo(rs.getInt(3));
				qna.setQnaTitle(rs.getString(4));
				qna.setQnaRegDate(rs.getDate(5));
				qna.setQnaVisible(rs.getInt(6));
				qna.setQnaFile(rs.getString(7));
				qna.setQnaViews(rs.getInt(8));
				qna.setQna_reply_count(rs.getInt(9));
				qnas.add(qna);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (rs != null) rs.close();
			if (pstmt != null) pstmt.close();
			if (conn != null) conn.close();
		}
		return qnas;
	}

 - SQL문으로 qna와 reply 테이블 조인 후 셀렉트 한 뒤 rownum을 설정해서 정렬함

 - rownum idx 기준으로 조회되는 게시글의 시작과 끝을 설정

 

 

 

5. QNA 조회수 1건 상승

public void increaseQnaReadCount(int qnaNo) throws Exception {
		Connection conn = null;
		PreparedStatement pstmt = null;

		try {
			conn = dataSource.getConnection();
			pstmt = conn.prepareStatement(QnaSql.QNA_READ_COUNT);
			pstmt.setInt(1, qnaNo);
			pstmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (pstmt != null) pstmt.close();
			if (conn != null) conn.close();
		}
	}
    
    //사용한 SQL문
    //UPDATE QNA SET QNA_VIEWS = QNA_VIEWS + 1 where QNA_NO = ?

 - 게시물에 들어올 경우 QNA_VIEWS 테이블을 1씩 증가하도록 설정

 

 

 

6. QNA 총 갯수 반환

public int getQnaCount() throws Exception{
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		int count = 0;
		try {
			conn = dataSource.getConnection();
			pstmt = conn.prepareStatement(QnaSql.QNA_COUNT);
			rs = pstmt.executeQuery();
			if (rs.next())
				count = rs.getInt(1);

		} finally {
			if (rs != null)
				rs.close();
			if (pstmt != null)
				pstmt.close();
			if (conn != null)
				conn.close();
		}
		return count;
	}
    
    //사용한 SQL문
    //SELECT COUNT(*) FROM QNA

 - QNA 게시물의 총 갯수를 반환받아 리스트 페이지 내 현재 총 게시물 수에 보여주는 용도로 사용

 

 

댓글 Reply의 DTO클래스와 관련된 DAO를 QNA페이지 한곳에 몰아 정리해보았습니다.

감사합니다.

Comments