basic01_java 프로젝트의 oop0919 패키지 Test05_File 참고
https://ewok.tistory.com/94?category=1087760
[37일] Java (49) - File 클래스
File 클래스는 파일 및 폴더 정보를 제공해주는 역할을 한다. 파일명, 파일크기, 확장명, 파일 타입 등을 알 수 있다. File 객체를 생성하려면 문자열 경로를 제공해야 한다. String pathname = "I:/java20220
ewok.tistory.com
삭제를 위한 delete 메서드를 먼저 만들자
PdsDAO.java
public int delete(int pdsno, String passwd, String saveDir) {
int cnt = 0;
try {
con = dbopen.getConection();
//테이블의 행 삭제하기 전에, 삭제하고자 하는 파일명 가져온다
String filename = "";
PdsDTO oldDTO = read(pdsno);
if(oldDTO != null) {
filename = oldDTO.getFilename();
}//if end
sql = new StringBuilder();
sql.append(" DELETE FROM tb_pds ");
sql.append(" WHERE pdsno=? AND passwd=? ");
pstmt = con.prepareStatement(sql.toString());
pstmt.setInt(1, pdsno);
pstmt.setString(2, passwd);
cnt = pstmt.executeUpdate();
if(cnt==1) { //테이블에서 행 삭제가 성공했으므로, 첨부했던 파일도 삭제
Utility.deleteFile(saveDir, filename);
}//if end
} catch (Exception e) {
System.out.println("삭제 실패 : " + e);
} finally {
DBClose.close(con, pstmt);
}//end
return cnt;
}//delete() end
처음 위 코드로 실행한 결과 올바른 비밀번호를 입력했음에도 비밀번호가 일치하지 않다는 문구가 출력되었다. 이는 try 바로 아래에서 db 연결을 했는데 read()에서 다시 한번 연결하였기 때문이다. 아래와 같이 수정하였더니 정상적으로 실행됐다.
public int delete(int pdsno, String passwd, String saveDir) {
int cnt = 0;
try {
//테이블의 행 삭제하기 전에, 삭제하고자 하는 파일명 가져온다
String filename = "";
PdsDTO oldDTO = read(pdsno);
if(oldDTO != null) {
filename = oldDTO.getFilename();
}//if end
con = dbopen.getConection();
sql = new StringBuilder();
sql.append(" DELETE FROM tb_pds ");
sql.append(" WHERE pdsno=? AND passwd=? ");
pstmt = con.prepareStatement(sql.toString());
pstmt.setInt(1, pdsno);
pstmt.setString(2, passwd);
cnt = pstmt.executeUpdate();
if(cnt==1) { //테이블에서 행 삭제가 성공했으므로, 첨부했던 파일도 삭제
Utility.deleteFile(saveDir, filename);
}//if end
} catch (Exception e) {
System.out.println("삭제 실패 : " + e);
} finally {
DBClose.close(con, pstmt);
}//end
return cnt;
}//delete() end
read()에는 db를 열고 마지막에 닫아주는 작업이 포함되었기 때문에 read() 아래로 다시 db를 여는 코드를 작성하여 정상적으로 작동되게 하였다.
pdsDel.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="../header.jsp"%>
<!-- 본문 시작 pdsDel.jsp-->
<!-- 글번호(pdsno)와 비밀번호(passwd)가 일치하면 행 삭제 및 첨부파일 삭제하기 -->
<%
int pdsno = Integer.parseInt(request.getParameter("pdsno"));
%>
<h3>* 사진 삭제 *</h3>
<p><a href="pdsList.jsp">[포토갤러리]</a></p>
<form method="post" action="pdsDelProc.jsp" onsubmit="return pwCheck2()"><!-- myscript.js -->
<input type="hidden" name="pdsno" value="<%=pdsno%>">
<table class="table">
<tr>
<th class="success">비밀번호</th>
<td><input type="password" name="passwd" id="passwd" required></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="삭제" class="btn btn-danger">
</td>
</tr>
</table>
</form>
<!-- 본문 끝 -->
<%@ include file="../footer.jsp"%>
myscript.js
function pwCheck2() {
let passwd = document.getElementById("passwd").value;
passwd = passwd.trim();
if(!(passwd.length>=4 && passwd.length<=15)) {
alert("비밀번호 4~15글자 이내로 입력해 주세요");
document.getElementById("passwd").focus();
return false;
}//if end
let massage = "첨부 파일도 삭제됩니다\n계속 진행할까요?";
if(confirm(message)) { //확인true, 취소false
return true; //서버로 전송
} else {
return false;
}//if end
}//pwCheck2() end
pdsDelProc.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="ssi.jsp"%>
<%@ include file="../header.jsp"%>
<!-- 본문 시작 pdsDelProc.jsp-->
<h3>* 사진 삭제 결과 *</h3>
<%
int pdsno = Integer.parseInt(request.getParameter("pdsno"));
String passwd = request.getParameter("passwd").trim();
String saveDir = application.getRealPath("/storage"); //첨부된 파일을 삭제하기 위해서(파일 저장 실제 경로)
int cnt = dao.delete(pdsno, passwd, saveDir);
if(cnt==0) {
out.println("<p>비밀번호가 일치하지 않습니다</p>");
out.println("<p><a href='javascript:history.back()>[다시시도]</a></p>'");
} else {
out.println("<script>");
out.println(" alert('게시글과 사진이 삭제되었습니다')");
out.println(" location.href='pdsList.jsp';"); //목록페이지 이동
out.println("</script>");
}//if end
%>
<!-- 본문 끝 -->
<%@ include file="../footer.jsp"%>
'웹개발 교육 > jsp' 카테고리의 다른 글
[61일] jsp (39) - myweb 프로젝트(회원 탈퇴 페이지) (0) | 2022.10.24 |
---|---|
[61일] jsp (38) - myweb 프로젝트(과제) (0) | 2022.10.24 |
[60일] jsp (36) - myweb 프로젝트(첨부 게시판-목록, 상세보기) (0) | 2022.10.21 |
[60일] jsp (35) - myweb 프로젝트(첨부 게시판) (0) | 2022.10.21 |
[59일] jsp (34) - myweb 프로젝트(첨부 게시판 기초) (0) | 2022.10.20 |