Spring with @ 게시글 변경 삭제 조회수- day63

stan·2023년 8월 7일
0

Spring

목록 보기
5/22

board.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Board</title>
</head>
<body>	
	<script type = "text/javascript">
	function delBoard(bid){
		var ans = confirm("정말 삭제하시겠습니까?");
		if(ans) {
			location.href="deleteBoard.do?bid="+bid;
		}
	}
	</script>

<form action="updateBoard.do" method="post">
	<input type = "text" value = "${data.bid}" readonly/>
	<input type = "text" value = "${data.title}" readonly/>
	<input type = "text" value = "${data.content}"/>
	<input type = "text" value = "${data.writer}"/>

</form>
	<c:if test="${data.writer eq member}">
	<input type="submit" value = "글 수정">
	<input type="button" value = "글 삭제" onClick ="delBoard(${data.bid})">
	</c:if>
	
</body>
</html>

※disable 쓰면 안됨

BoardDAO

package com.spring.biz.board;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.spring.biz.common.JDBCUtil;

@Repository("boardDAO") // @Component를 상속받은 @
public class BoardDAO {
	private Connection conn;
	private PreparedStatement pstmt;
	private ResultSet rs;
	
	private final String insert="INSERT INTO BOARD (BID,TITLE,CONTENT,WRITER) VALUES((SELECT NVL(MAX(BID),0)+1 FROM BOARD),?,?,?)";
	private final String selectOne="SELECT * FROM BOARD WHERE BID=?"; // getOne
	private final String selectAll="SELECT * FROM BOARD ORDER BY BID DESC"; // getAll
	private final String selectAll_Writer = "SELECT * FROM BOARD WHERE WRITER LIKE '%' || ? || '%'";
	private final String selectAll_Title = "Select * FROM BOARD WHERE TITLE LIKE '%' || ?|| '%'";
	private final String update="UPDATE BOARD SET TITLE=?,CONTENT=? WHERE BID=?";
	private final String update_Cnt = "UPDATE BOARD SET CNT=CNT+1 WHERE BID=?";
	private final String delete="DELETE FROM BOARD WHERE BID=?";
	
	public BoardVO selectOne(BoardVO bVO) {
		BoardVO data=null;
		System.out.println("BoardDAO 로그 selectOne() 메서드");
		conn=JDBCUtil.getConnection();
		try {
			pstmt=conn.prepareStatement(selectOne);
			pstmt.setInt(1, bVO.getBid());
			ResultSet rs=pstmt.executeQuery();
			if(rs.next()) {
				data=new BoardVO();
				data.setBid(rs.getInt("BID"));
				data.setCnt(rs.getInt("CNT"));
				data.setContent(rs.getString("CONTENT"));
				data.setTitle(rs.getString("TITLE"));
				data.setWriter(rs.getString("WRITER"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, pstmt);
		}
		return data;
	}
	public List<BoardVO> selectAll(BoardVO bVO) {
		List<BoardVO> datas=new ArrayList<BoardVO>();
		System.out.println("BoardDAO 로그 selectAll() 메서드");
		conn=JDBCUtil.getConnection();
		try {
			if(bVO.getSearchCondition()==null) {
				pstmt = conn.prepareStatement(selectAll);
			}
			else if (bVO.getSearchCondition().equals("WRITER")) {
				pstmt = conn.prepareStatement(selectAll_Writer);
				pstmt.setString(1, bVO.getSearchContent());
			}
			else if(bVO.getSearchCondition().equals("TITLE")) {
				pstmt = conn.prepareStatement(selectAll_Title);
				pstmt.setString(1, bVO.getSearchContent());
			}
			rs=pstmt.executeQuery();
			//pstmt=conn.prepareStatement(selectAll);
			//ResultSet rs=pstmt.executeQuery();
			while(rs.next()) {
				BoardVO data=new BoardVO();
				data.setBid(rs.getInt("BID"));
				data.setCnt(rs.getInt("CNT"));
				data.setContent(rs.getString("CONTENT"));
				data.setTitle(rs.getString("TITLE"));
				data.setWriter(rs.getString("WRITER"));
				datas.add(data);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, pstmt);
		}
		return datas;
	}
	
	public boolean insert(BoardVO bVO) {
		System.out.println("BoardDAO 로그 insert() 메서드");
		conn=JDBCUtil.getConnection();
		try {
			pstmt=conn.prepareStatement(insert);
			pstmt.setString(1, bVO.getTitle());
			pstmt.setString(2, bVO.getContent());
			pstmt.setString(3, bVO.getWriter());
			int rs=pstmt.executeUpdate();
			if(rs<=0) {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			JDBCUtil.close(conn, pstmt);
		}
		return true;
	}
	public boolean update(BoardVO bVO) {
		System.out.println("BoardDAO 로그 update() 메서드");
		conn=JDBCUtil.getConnection();
		try {//nullpointerexception뜸 방지
			//searchCondition set 안했으면 NPE나옴
			///bVO는 스프링컨테이너에 의해 100% new되기때문에 bVO자체가 널 일순없습니당
			if(bVO.getSearchCondition()!=null && bVO.getSearchCondition().equals("cnt")) {
				pstmt=conn.prepareStatement(update_Cnt);
				pstmt.setInt(1, bVO.getBid());
			}
			else {
				pstmt=conn.prepareStatement(update);
				pstmt.setString(1, bVO.getTitle());
				pstmt.setString(2, bVO.getContent());
				pstmt.setInt(3, bVO.getBid());	
			}
			
			int rs=pstmt.executeUpdate();
			if(rs<=0) {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			JDBCUtil.close(conn, pstmt);
		}
		return true;
	}
	public boolean delete(BoardVO bVO) {
		System.out.println("BoardDAO 로그 delete() 메서드");
		conn=JDBCUtil.getConnection();
		try {
			pstmt=conn.prepareStatement(delete);
			pstmt.setInt(1, bVO.getBid());
			int rs=pstmt.executeUpdate();
			if(rs<=0) {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			JDBCUtil.close(conn, pstmt);
		}
		return true;
	}
}

cnt 하려면
Update 쿼리문 1개 더 필요

BoardControl

package com.spring.view.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.spring.biz.board.BoardDAO;
import com.spring.biz.board.BoardVO;

@Controller
public class BoardController {

	@RequestMapping(value="/board.do")
	public String selectBoard(BoardVO bVO, BoardDAO bDAO, Model model) {
		System.out.println("BoardController로그");
		bVO.setSearchCondition("cnt");
		
		bDAO.update(bVO);
		
		//bVO.setBid(Integer.parseInt(request.getParameter("bid"))); bVO가 이미 setter를 이미 불러줌 command객체로
		//HttpServletRequest request를 인자로 쓸 필요가 없음
		model.addAttribute("data",bDAO.selectOne(bVO));//모델의 구조는 Map
		
		
		return "board.jsp";
	}
	
}
profile
이진 입니다

0개의 댓글