728x90
작성된 게시글을 수정(UPDATE)할 수 있는 기능을 만들었다.
pw가 맞아야 수정이 가능하도록 쿼리를 작성했다.
updateBoardForm.jsp
<%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
int no = Integer.parseInt(request.getParameter("no"));
System.out.println("updateBoardForm - no = " + no); // 값 체크
/*
SQL 쿼리문
"SELECT title, writer, content
FROM board
WHERE no = ?";
*/
String sql = "SELECT title, writer, pw, content FROM board WHERE no = ?";
// DB 연결 및 초기화
Class.forName("org.mariadb.jdbc.Driver");
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
conn = DriverManager.getConnection("jdbc:mariadb://127.0.0.1:3307/board","root","****");
stmt = conn.prepareStatement(sql);
stmt.setInt(1, no);
rs = stmt.executeQuery();
if(rs.next()) {
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>updateBoardForm</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Orbit&display=swap" rel="stylesheet">
<style>
body {
font-family: "Orbit", sans-serif;
font-weight: 400;
font-style: normal;
}
</style>
</head>
<body style="background-size : cover; background-image: url('./img/bg.jpg'); height: 100vh; width: 100vw;">
<div class="container" style="opacity: 0.9;">
<div class="row">
<div class="col"></div>
<div class="mt-5 col-9 border border-light-subtle shadow p-5 mb-5 bg-body-tertiary rounded">
<!-- start main-->
<div class="row">
<div class="col">
<h1>게시글 수정</h1>
</div>
<div class="col"></div>
<div class="col">
<form action="./boardList.jsp">
<button type="submit" class="btn-close" aria-label="Close" style="float: right; width:25px; height:25px; background-size: 25px 25px;"></button>
</form>
</div>
</div>
<form action="./updateBoardAction.jsp?no=<%=no %>" method="post">
<table class="table table-hover table-light" style="width: 100%;">
<tr>
<th style="width:20%;">제목</th>
<td><input type="text" style="width: 300px;" name="title" value='<%=rs.getString("title")%>'></td>
</tr>
<tr>
<th style="width:20%;">글쓴이</th>
<td><input type="text" style="width: 300px;" name="writer" value='<%=rs.getString("writer")%>' readonly="readonly"></td>
</tr>
<tr>
<th style="width:20%;">비밀번호</th>
<td><input type="password" style="width: 300px;" name="pw"></td>
</tr>
<tr>
<th style="width:20%;">내용</th>
<td><textarea rows="10" cols="60" name="content"><%=rs.getString("content")%></textarea></td>
</tr>
</table>
<div class="row">
<div class="col"></div>
<div class="col"></div>
<div class="col" style="text-align: center; margin: auto 0;">
<button type="submit" class="btn btn-light shadow-sm bg-body-#117C74 rounded" style="border:1px solid #117C74; color: white; background-color: #0C7971; float: right; margin-left: 10px;">
완료
</button>
<button type="reset" class="btn btn-light shadow-sm bg-body-#117C74 rounded" style="border:1px solid #117C74; color: white; background-color: #0C7971; float: right;">
초기화
</button>
</div>
</div>
</form>
<!-- end -->
</div>
<div class="col"></div>
</div>
</div>
</body>
<%
}
//DB 자원 반납
rs.close();
stmt.close();
conn.close();
%>
</html>
updateBoardAction.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%
// 1. 요청 확인
int no = Integer.parseInt(request.getParameter("no"));
String title = request.getParameter("title");
String writer = request.getParameter("writer");
String pw = request.getParameter("pw");
String content = request.getParameter("content");
// 디버깅(값 체크)
System.out.println("updateBoardAction - no = " + no);
System.out.println("updateBoardAction - title = " + title);
System.out.println("updateBoardAction - writer = " + writer);
System.out.println("updateBoardAction - pw = " + pw);
System.out.println("updateBoardAction - content = " + content);
// 2. DB 데이터 수정
/*
SQL 쿼리 문
UPDATE board
SET title = ?, content = ?
WHERE no = ? AND pw = ?
*/
String sql = "UPDATE board SET title = ?, content = ? WHERE no = ? AND pw = ?";
Connection conn = null;
PreparedStatement stmt = null;
int row = 0;
conn = DriverManager.getConnection("jdbc:mariadb://127.0.0.1:3307/board","root","****");
stmt = conn.prepareStatement(sql);
stmt.setString(1, title);
stmt.setString(2, content);
stmt.setInt(3, no);
stmt.setString(4, pw);
stmt.executeQuery();
System.out.println(stmt); // ?값 체크
row = stmt.executeUpdate();
// 3. 결과 분기
// 성공 -> cityboardOne.jsp
// 실패 -> updateCityboardForm.jsp
if(row == 1) {
response.sendRedirect("./boardOne.jsp?no=" + no);
} else {
response.sendRedirect("./updateBoardForm.jsp?no=" + no);
}
// DB 반납
stmt.close();
conn.close();
%>
728x90
'웹 개발' 카테고리의 다른 글
파이널 프로젝트를 마치며 (0) | 2024.08.19 |
---|---|
css,bootstrap사용해서 게시판만들기(4) (0) | 2024.03.22 |
css,bootstrap사용해서 게시판만들기(3) (0) | 2024.03.22 |
css,bootstrap사용해서 게시판만들기(2) (0) | 2024.03.21 |
css,bootstrap사용해서 게시판만들기(1) (0) | 2024.03.21 |