40: Servlet delete&modify

jk·2024년 2월 28일
0

kdt 풀스택

목록 보기
77/127



6. 게시판 삭제를 구현하시오.

//code1
//BFrontController.java
			case "/delete.do": {
				System.out.println("Here is " + com);
				System.out.println("--------------");
				command = new BDeleteCommand();
				command.execute(request, response);
				viewPage = "list.do";
				break;
			}
//code2
//BDeleteCommand.java
public class BDeleteCommand implements BCommand {
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) {
		BDao dao = new BDao();
		dao.delete(request.getParameter("bId"));
	}
}
//code3
//BDao.java
	public void delete(String bIdStr) {
		Connection conn = null;
		PreparedStatement psmt = null;
		try {
			String query = "delete from mvc_board where bid = ?";
			conn = dataSource.getConnection();
			psmt = conn.prepareStatement(query);
			System.out.println(query);
			int bId = Integer.parseInt(bIdStr);
			psmt.setInt(1, bId);
			int rn = psmt.executeUpdate();
			System.out.println("rn: " + rn);
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (conn != null) {
					conn.close();
				};
				if (psmt != null) {
					psmt.close();
				};
			} catch(Exception e2) {
				e2.printStackTrace();
			};
		};		
	}
<!-- code4 -->
<!-- content_view.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>content_view.jsp</title>
</head>
<body>
	<h1>글보기</h1>
	<table width="500" border="1">
		<form action="modify.do" method="post">
			<input type="hidden" name="bId" value="${content_view.bId}">
			<tr>
				<td>번호</td>
				<td>${content_view.bId}</td>
			</tr>
			<tr>
				<td>히트</td>
				<td>${content_view.bHit}</td>
			</tr>
			<tr>
				<td>이름</td>
				<td><input type="text" name="bName" value="${content_view.bName}"></td>
			</tr>
			<tr>
				<td>제목</td>
				<td><input type="text" name="bTitle" value="${content_view.bTitle}"></td>
			</tr>
			<tr>
				<td>내용</td>
				<td><textarea name="bContent" rows="10">${content_view.bContent}</textarea></td>
			</tr>
			<tr>
				<td colspan="2">
					<input type="submit" value="수정">
						&nbsp&nbsp<a href="list.do">목록보기</a>
						&nbsp&nbsp<a href="delete.do?bId=${content_view.bId}">삭제</a>
				</td>
			</tr>
		</form>
	</table>
</body>
</html>



7. 게시판 수정을 구현하시오.

//code1
//BFrontController.java
			case "/modify.do": {
				System.out.println("Here is " + com);
				System.out.println("--------------");
				command = new BModifyCommand();
				command.execute(request, response);
				viewPage = "list.do";
				break;
			}
//code2
//BModifyCommand.java
public class BModifyCommand implements BCommand {
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) {
		BDao dao = new BDao();
		dao.modify(request.getParameter("bId"), request.getParameter("bName"), request.getParameter("bTitle"), request.getParameter("bContent"));
	}
}
//code3
//BDao.java
	public void modify(String bIdStr, String bName, String bTitle, String bContent) {
		Connection conn = null;
		PreparedStatement psmt = null;
		try {
			String query = "update mvc_board set bname = ?, btitle = ?, bcontent = ? where bid = ?";
			conn = dataSource.getConnection();
			psmt = conn.prepareStatement(query);
			System.out.println(query);
			int bId = Integer.parseInt(bIdStr);
			psmt.setString(1, bName);
			psmt.setString(2, bTitle);
			psmt.setString(3, bContent);
			psmt.setInt(4, bId);
			int rn = psmt.executeUpdate();
			System.out.println("rn: " + rn);
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (conn != null) {
					conn.close();
				};
				if (psmt != null) {
					psmt.close();
				};
			} catch(Exception e2) {
				e2.printStackTrace();
			};
		};
	}
<!-- code4 -->
<!-- content_view.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>content_view.jsp</title>
</head>
<body>
	<h1>글보기</h1>
	<table width="500" border="1">
		<form action="modify.do" method="post">
			<input type="hidden" name="bId" value="${content_view.bId}">
			<tr>
				<td>번호</td>
				<td>${content_view.bId}</td>
			</tr>
			<tr>
				<td>히트</td>
				<td>${content_view.bHit}</td>
			</tr>
			<tr>
				<td>이름</td>
				<td><input type="text" name="bName" value="${content_view.bName}"></td>
			</tr>
			<tr>
				<td>제목</td>
				<td><input type="text" name="bTitle" value="${content_view.bTitle}"></td>
			</tr>
			<tr>
				<td>내용</td>
				<td><textarea name="bContent" rows="10">${content_view.bContent}</textarea></td>
			</tr>
			<tr>
				<td colspan="2">
					<input type="submit" value="수정">
						&nbsp&nbsp<a href="list.do">목록보기</a>
						&nbsp&nbsp<a href="delete.do?bId=${content_view.bId}">삭제</a>
				</td>
			</tr>
		</form>
	</table>
</body>
</html>
profile
Brave but clumsy

0개의 댓글