write.jsp
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
int num = Integer.parseInt(request.getParameter("num"));
String writer = "";
String title = "";
String content = "";
String url = "jdbc:mariadb://localhost:3307/jspdb";
String user = "root";
String pass = "maria";
int hits = 0;
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
table { width:680px;
background-color: antiquewhite;
text-align:center;}
td { background-color: white;
width:600px; text-align:left;
}
th { background-color: antiquewhite; }
.content { height:500px;}
textarea {width:99%; height: 99%;
border: 0; resize: none; }
textarea:focus { outline: none; }
</style>
</head>
<body>
<%
Class.forName("org.mariadb.jdbc.Driver");
try {
con = DriverManager.getConnection(url, user, pass);
stmt = con.createStatement();
rs = stmt.executeQuery("select * from board where num=" + num);
if(rs.next()){
writer = rs.getString("writer");
title = rs.getString("title");
content = rs.getString("content");
hits = rs.getInt("hits");
//조회수
stmt.executeUpdate(
"update board set hits=hits+1 where num=" + num);
}
%>
<form action="update.jsp">
<input type="hidden" name="num" value="<%=num%>">
<table border="1">
<tr>
<th>조회수</th>
<td><%=hits %></td>
</tr>
<tr>
<th>작성자</th>
<td><%=writer %></td>
</tr>
<tr>
<th>제목</th>
<td><textarea name="title"><%= title %></textarea></td>
</tr>
<tr>
<th>내용</th>
<td class="content"><textarea name="content"><%= content %></textarea></td>
</tr>
</table>
<input type="submit" value="수정">
</form>
<%
}catch (Exception e){
e.printStackTrace();
}
%>
</body>
</html>
update.jsp
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
Connection con = null;
PreparedStatement pstmt = null;
String title = request.getParameter("title");
String content = request.getParameter("content");
int num = Integer.parseInt(request.getParameter("num"));
String url = "jdbc:mariadb://localhost:3307/jspdb";
String user = "root";
String pass = "maria";
Class.forName("org.mariadb.jdbc.Driver");
try {
con = DriverManager.getConnection(url, user, pass);
String updateQuery = "UPDATE board SET title = ?, content = ? WHERE num = ?";
pstmt = con.prepareStatement(updateQuery);
pstmt.setString(1, title);
pstmt.setString(2, content);
pstmt.setInt(3, num);
pstmt.executeUpdate();
response.sendRedirect("list.jsp?num=" + num);
} catch (Exception e) {
e.printStackTrace();
}
%>
</body>
</html>
view.jsp에서 수정 버튼 클릭 시 write.jsp로 이동
write.jsp에서 글 제목, 내용 수정 후 수정 버튼 클릭하면 update.jsp로 이동해서
String updateQuery = "UPDATE board SET title = ?, content = ? WHERE num = ?";
pstmt = con.prepareStatement(updateQuery);
pstmt.setString(1, title);
pstmt.setString(2, content);
pstmt.setInt(3, num);
pstmt.executeUpdate();
response.sendRedirect("list.jsp?num=" + num);
해당 코드 부분 처리 후 db에 업데이트 후 list.jsp로 돌아감
DB에서도 값 변경된 거 확인
'프로젝트 기반 자바(JAVA) 응용 SW개발자 취업과정 > JSP 활용 연습' 카테고리의 다른 글
JSTL (0) | 2023.07.12 |
---|---|
Dao, Dto, ServiceImp ( java) 활용 jsp 정리 (0) | 2023.06.23 |
게시판 만들기_1_1 ( update.jsp / write.jsp )MariaDB (0) | 2023.06.16 |
게시판 만들기_2 ( list.jsp / view.jsp )MariaDB (0) | 2023.06.16 |
게시판 만들기_1 ( list.jsp / view.jsp )MariaDB (0) | 2023.06.15 |