일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 반다이몰
- java
- 일본어
- C로 시작하는 컴퓨터 프로그래밍4판
- ruby
- 日本語
- 자바
- Web
- Python
- 인프런
- vscode
- Flutter
- rails
- CSS
- springboot
- rails7
- nico
- jsp
- javascript
- 연습문제
- 디지몬
- Spring
- 単語
- 건담베이스
- html
- DART
- 一日一つメソッド
- 건담
- 비즈니스일본어
- メソッド
Archives
- Today
- Total
AR삽질러
JSP 개인프로젝트(10) - 게시판만들기(게시글수정, 삭제) 본문
728x90
https://arang95.tistory.com/58
지난시간에는 게시판만들기중 작성된 게시글의 내용을 볼수 있는 페이지와 모델을 구현하였다.
이번시간에는 게시판글 수정, 삭제를 구현해보겠다
1. 게시글 수정DAO
db에서 해당 내용을 꺼내온다.
게시글 삭제는 문제없이 동작하지만 수정부분에서 db가 에러가나서 또 삽질했다...ORA-12514
에러도 떠서 db가 망가진줄 알았지만 결국 null값이 들어가서 에러가 발생한거였고 데이터를 모두 지운후 다시 작동하니 동작하더라..삽질의 결과는 역시 결국 또...!!! 오타였지...
public BoardVO updateGetArticle(int num) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
BoardVO article = null;
try {
conn = ConnUtil.getcConnection();
pstmt = conn.prepareStatement("select * from board where num = ?");
pstmt.setInt(1, num);
rs = pstmt.executeQuery();
if(rs.next()) {
article = new BoardVO();
article.setNum(rs.getInt("num"));
article.setWriter(rs.getString("writer"));
article.setEmail(rs.getString("email"));
article.setSubject(rs.getString("subject"));
article.setPass(rs.getString("pass"));
article.setRegdate(rs.getTimestamp("regdate"));
article.setReadcount(rs.getInt("readcount"));
article.setRef(rs.getInt("ref"));
article.setStep(rs.getInt("step"));
article.setDepth(rs.getInt("depth"));
article.setContent(rs.getString("content"));
article.setIp(rs.getString("ip"));
}
}catch(Exception e) {
e.printStackTrace();
}finally {
if(rs != null)try {rs.close();}catch(SQLException e) {}
if(pstmt != null)try {pstmt.close();}catch(SQLException e) {}
if(conn != null)try {conn.close();}catch(SQLException e) {}
}
return article;
}
// 수정
public int updateArticle(BoardVO article) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String dbpasswd = "";
String sql = "";
int result = -1;
try {
conn = ConnUtil.getcConnection();
pstmt = conn.prepareStatement("select pass from board where num = ?");
pstmt.setInt(1, article.getNum());
rs = pstmt.executeQuery();
if(rs.next()) {
dbpasswd = rs.getString("pass");
if(dbpasswd.equals(article.getPass())) {
sql = "update board set writer=?, email=?, subject=?, content=? where num=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, article.getWriter());
pstmt.setString(2, article.getEmail());
pstmt.setString(3, article.getSubject());
pstmt.setString(4, article.getContent());
pstmt.setInt(5, article.getNum());
pstmt.executeUpdate();
result = 1;
}else {
result = 0;
}
}
}catch(Exception e) {
e.printStackTrace();
}finally {
if(rs != null)try {rs.close();}catch(SQLException e) {}
if(pstmt != null)try {pstmt.close();}catch(SQLException e) {}
if(conn != null)try {conn.close();}catch(SQLException e) {}
}
return result;
}
2. 게시글수정화면
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="boardone.BoardDAO" %>
<%@ page import="boardone.BoardVO" %>
<%@ page import="java.util.List" %>
<%@ page import="java.text.SimpleDateFormat" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="../css/bootstrap.css">
<link rel="stylesheet" href="../css/custom.css">
<title>글내용보기</title>
<script type="text/javascript" src="script.js"></script>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="../js/bootstrap.js"></script>
</head>
<%
int num = Integer.parseInt(request.getParameter("num"));
String pageNum = request.getParameter("pageNum");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
try{
BoardDAO dbPro = BoardDAO.getInstance();
BoardVO article = dbPro.getArticle(num);
int ref = article.getRef();
int step = article.getStep();
int depth = article.getDepth();
%>
<body>
<%
String id = (String)session.getAttribute("id");
%>
<nav class="navbar navbar-default">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed"
data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"
aria-expanded="false">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="../memberone/main.jsp">AR 웹페이지</a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active"><a href="../memberone/main.jsp">메인</a>
<li class="active"><a href="../boardone/writeForm.jsp">게시판</a>
</ul>
<%
if(id == null){
%>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle"
data-toggle="dropdown" role="button" aria-haspopup="true"
aria-expanded="false">접속하기<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a href="../memberone/login.jsp">로그인</a></li>
<li><a href="../memberone/regForm.jsp">회원가입</a></li>
</ul>
</li>
</ul>
<%
}
%>
</div>
</nav>
<div class="container">
<table class="table table-bordered table-hover" style="text-align: center; border: 1px solid #ddddddd">
<thead>
<tr>
<th colspan="4"><h4>글내용</h4></th>
</tr>
<tbody>
<tr>
<td style="width:125px;" align="center"><h5>글번호</h5></td>
<td style="width:125px;" align="center"><h5><%=article.getNum() %></h5></td>
<td style="width:125px;" align="center"><h5>조회수</h5></td>
<td style="width:125px;" align="center"><h5><%=article.getReadcount() %></h5></td>
</tr>
<tr>
<td align="center" style="width:125px;" align="center"><h5>작성자</h5></td>
<td align="center" style="width:375px;"><h5><%=article.getWriter() %></h5></td>
<td align="center"style="width:125px;"><h5>작성일</h5></td>
<td align="center" style="width:125px;"><h5><%=article.getRegdate() %></h5></td>
</tr>
<tr>
<td align="center" style="width:125px;" align="center"><h5>글제목</h5></td>
<td align="center" style="width:375px;" colspan="3"><h5><%=article.getSubject() %><h5></td>
</tr>
<tr>
<td align="center" style="width:125px;"><h5>글내용</h5></td>
<td align="left" style="width:375px;" colspan="3"><h5><%=article.getContent() %></h5></td>
</tr>
<tr>
<td colspan="4" align="center">
<input class="btn btn-primary" type="button" value="게시글 수정" onClick="document.location.href='updateForm.jsp?num=<%=article.getNum()%>&pageNum=<%=pageNum%>'">
<input class="btn btn-primary" type="button" value="게시글 삭제" onClick="document.location.href='deleteForm.jsp?num=<%=article.getNum() %>&pageNum=<%=pageNum %>'">
<input class="btn btn-primary" type="button" value="게시글 목록" onClick="document.location.href='list.jsp?pageNum=<%=pageNum %>'">
</td>
</tr>
</tbody>
<%
}catch(Exception e){}
%>
</thead>
</table>
</div>
</body>
</html>
3. 게시글 수정처리
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="boardone.BoardDAO"%>
<%@page import="java.sql.Timestamp"%>
<%request.setCharacterEncoding("utf-8"); %>
<jsp:useBean id="article" scope="page" class="boardone.BoardVO">
<jsp:setProperty name="article" property="*"/>
</jsp:useBean>
<%
String pageNum = request.getParameter("pageNum");
BoardDAO dbPro = BoardDAO.getInstance();
int check = dbPro.updateArticle(article);
if(check == 1){
%>
<meta http-equiv="Refresh" content="0;url=list.jsp?pageNum=<%=pageNum %>">
<%}else{ %>
<script type="text/javascript">
<!--
alert("비밀본호가 틀렸습니다!");
history.go(-1);
-->
</script>
<%} %>
4. 게시글삭제DAO
// 삭제
public int deleteArticle(int num, String pass) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String dbpasswd = "";
int result = -1;
try {
conn = ConnUtil.getcConnection();
pstmt = conn.prepareStatement("select pass from board where num = ?");
pstmt.setInt(1, num);
rs = pstmt.executeQuery();
if(rs.next()) {
dbpasswd = rs.getString("pass");
if(dbpasswd.equals(pass)) {
pstmt = conn.prepareStatement("delete from board where num=?");
pstmt.setInt(1, num);
pstmt.executeUpdate();
result = 1;
}else
result = 0;
}
}catch (Exception e) {
e.printStackTrace();
}finally {
if(rs != null)try {rs.close();}catch(SQLException e) {}
if(pstmt != null)try {pstmt.close();}catch(SQLException e) {}
if(conn != null)try {conn.close();}catch(SQLException e) {}
}
return result;
}
5. 게시글 삭제 폼
<%
int num = Integer.parseInt(request.getParameter("num"));
String pageNum = request.getParameter("pageNum");
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="../css/bootstrap.css">
<link rel="stylesheet" href="../css/custom.css">
<title>글수정</title>
<script type="text/javascript" src="script.js"></script>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="../js/bootstrap.js"></script>
</head>
<script language="JavaScript">
<!--
function deleteSave(){
if(document.delForm.pass.value==""){
alert("비밀번호를 입력해주세요!");
document.delForm.pass.focus();
return false;
}
}
-->
</script>
<body>
<%
String id = (String)session.getAttribute("id");
%>
<nav class="navbar navbar-default">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed"
data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"
aria-expanded="false">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="../memberone/main.jsp">AR 웹페이지</a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active"><a href="../memberone/main.jsp">메인</a>
<li class="active"><a href="../boardone/writeForm.jsp">게시판</a>
</ul>
<%
if(id == null){
%>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle"
data-toggle="dropdown" role="button" aria-haspopup="true"
aria-expanded="false">접속하기<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a href="../memberone/login.jsp">로그인</a></li>
<li><a href="../memberone/regForm.jsp">회원가입</a></li>
</ul>
</li>
</ul>
<%
}
%>
</div>
</nav>
<div class="container">
<form method="post" name="delForm" action="deleteProc.jsp?pageNum=<%=pageNum %>" onsubmit="return deleteSave()">
<table class="table table-bordered table-hover" style="text-align: center; border: 1px solid #ddddddd">
<thead>
<tr height="30">
<th align="center">
<h4>비밀번호를 입력해 주세요</h4></th>
</tr>
</thead>
<tbody>
<tr height="30">
<td style="width:110px;">비밀번호 :
<input class="form-controlnum" type="password" name="pass" size="25" maxlength="12" placeholder="비밀번호를 입력하세요.">
<input type="hidden" name="num" value="<%=num %>"></td>
</tr>
<tr height="30">
<td style="width:110px;">
<input class="btn btn-primary" type="submit" value="글삭제"/>
<input class="btn btn-primary" type="button" value="글목록" onclick="document.location.href='list.jsp?pageNum=<%=pageNum%>'">
</td>
</tr>
</tbody>
</table>
</form>
</div>
</body>
</html>
6. 게시글 삭제
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="boardone.BoardDAO"%>
<%@page import="java.sql.Timestamp"%>
<%request.setCharacterEncoding("utf-8"); %>
<%
int num = Integer.parseInt(request.getParameter("num"));
String pageNum = request.getParameter("pageNum");
String pass = request.getParameter("pass");
BoardDAO dbPro = BoardDAO.getInstance();
int check = dbPro.deleteArticle(num, pass);
if(check==1){
%>
<meta http-equiv="Refresh" content="0;url=list.jsp?pageNum=<%=pageNum %>">
<%}else{ %>
<script type="text/javascript">
<!--
alert("비밀번호가 틀렸습니다!");
history.go(-1);
-->
</script>
<%} %>
게시글이 삭제되면 자동으로 게시글 목록화면으로 돌아간다.
7. 구현내용
메인
게시판
글쓰기
글내용
글내용 수정하기
게시글삭제
8. 구현영상
오늘도 화이팅!!
728x90
반응형
LIST
'JAVA > JSP' 카테고리의 다른 글
JSP 개인프로젝트(12) - 게시판만들기(댓글관리) (0) | 2023.03.09 |
---|---|
JSP 개인프로젝트(11) - 게시판만들기(페이징처리) (0) | 2023.03.07 |
JSP 개인프로젝트(9) - 게시판만들기(게시글내용보기) (0) | 2023.02.28 |
JSP 개인프로젝트(8) - 게시판만들기(게시글목록) (0) | 2023.02.28 |
JSP 개인프로젝트(7) - singleton적용, 게시판만들기 (0) | 2023.02.28 |