Jsp - Delete 게시글

2020. 7. 30. 21:30Jsp

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<%@page import="board.BoardDAO"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>jsp5/deleteForm.jsp</h1>
<%
request.setCharacterEncoding("utf-8");
 
int num=Integer.parseInt(request.getParameter("num"));
String pass=request.getParameter("pass");
 
BoardDAO bdao=new BoardDAO();
ResultSet rs=bdao.getResultSet(num);
if(rs.next()){
    
%>
<form action="deletePro.jsp" method="post">
<input type="hidden" name="num" value="<%=rs.getInt("num")%>">
<table border="1">
<tr><td>비밀번호</td><td><input type="password" name="pass"></td></tr>
<tr><td><input type="submit" value="글삭제"></td>
<td><input type="button" value="글목록" onclick="location.href='list.jsp'"> </td>
</tr>
</table>
</form>
<%
}
%>
</body>
</html>
\
<%@page import="board.BoardDTO"%>
<%@page import="board.BoardDAO"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>jsp5/deletePro.jsp</h1>
 
<%
request.setCharacterEncoding("utf-8");
//파라미터 받아오기
int num=Integer.parseInt(request.getParameter("num"));
String pass=request.getParameter("pass");
//num 저장
BoardDTO bb=new BoardDTO();
bb.setNum(num);
bb.setPass(pass);
//저장된 num  검색결과 저장
BoardDAO bdao=new BoardDAO();
ResultSet rs=bdao.getResultSet(bb.getNum());
//검색결과가 있으면 비밀번호 확인후 게시글 삭제
if(rs.next()){
    if(pass.equals(rs.getString("pass"))){
        bdao.DeleteBoard(bb);
        response.sendRedirect("list.jsp");
        
    }else{%>
        <script type="text/javascript">
        alert("비밀번호 틀림");
        history.back();
        </script>        <%
    }
    
    
}else{%>
    <script type="text/javascript">
        alert("게시물 없음");
        history.back();
        </script>
    <%
    
}
 
%>
</body>
</html>
cs

 

DAO와 bean(DTO)를 활용하여 게시글 삭제를 구현해보았다. DB에 연결하여 화면에서 받아온 파라미터를 DAO에 적용하여 해당하는 DB를 지우는 기능인데 아주 간단하니 헷갈리지만 말자!

'Jsp' 카테고리의 다른 글

Jsp - 게시글내용  (0) 2020.07.30
Jsp - Update 게시글  (0) 2020.07.30
Jsp - 게시글 쓰기  (0) 2020.07.30
Jsp - DAO,Paging 을 활용한 게시판 글목록 불러오기  (1) 2020.07.30
Jsp - file upload  (0) 2020.07.26