어제 만든거에서 수정 클릭시 내용 변경되서 DB에 저장 되도록 만들어 봄
Post방식으로 만든거라 만들었을때 한글만 깨져서 웹사이트에서 출력되고 DB에서도 한글만 깨져서
저장 됫다. 강사님 한태 물어보니
<%request.setCharacterEncoding("utf-8");%> <= 해당 인코딩 부분을 넣어주니 해결 됫다.
write.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%
request.setCharacterEncoding("utf-8");
%>
<%
// 지정된 글 번호 얻기
int num = Integer.parseInt(request.getParameter("num"));
// 게시글 데이터를 담을 변수 정의
String writer = "";
String title = "";
String regtime = "";
int hits = 0;
String content = "";
// 지정된 글 번호를 가진 레코드 읽기
Class.forName("org.mariadb.jdbc.Driver");
try (
Connection conn = DriverManager.getConnection(
"jdbc:mariadb://localhost:3307/jspdb", "root", "maria");
Statement stmt = conn.createStatement();
// 쿼리 실행
ResultSet rs = stmt.executeQuery(
"select * from board where num=" + num);
) {
if (rs.next()) {
// 글 데이터를 변수에 저장
writer = rs.getString("writer");
title = rs.getString("title");
content = rs.getString("content");
regtime = rs.getString("regtime");
hits = rs.getInt("hits");
// 글 제목과 내용이 웹 페이지에 올바르게 출력되도록
// 공백과 줄 바꿈 처리
title = title.replace(" ", " ");
content = content.replace(" ", " ").replace("\n", "<br>");
// 이 글의 조회수를 1 증가
stmt.executeUpdate("update board set hits=hits+1 where num=" + num);
}
// 수정된 내용을 데이터베이스에 업데이트
PreparedStatement pstmt = conn.prepareStatement("UPDATE board SET content = ? WHERE num = ?");
pstmt.setString(1, content);
pstmt.setInt(2, num);
pstmt.executeUpdate();// 업데이트된 레코드 수를 반환
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
table {
width: 680px;
text-align: center;
}
th {
width: 100px;
background-color: cyan;
}
td {
text-align: left;
border: 1px solid gray;
}
</style>
</head>
<body>
<table>
<tr>
<th>제목</th>
<td><%=title %></td>
</tr>
<tr>
<th>작성자</th>
<td><%=writer %></td>
</tr>
<tr>
<th>작성일시</th>
<td><%=regtime %></td>
</tr>
<tr>
<th>조회수</th>
<td><%=hits %></td>
</tr>
<tr>
<th>내용</th>
<td>
<form action="update.jsp" method="post">
<input type="hidden" name="num" value="<%=num%>">
<textarea name="content" rows="5" cols="50"><%=content%></textarea>
<br>
<input type="submit" value="수정 완료">
</form>
</td>
</tr>
</table>
</body>
</html>
update.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%
request.setCharacterEncoding("utf-8");// post방식 추가안하면 한글깨져서 나옴
%>
<%
String content = request.getParameter("content");
int num = Integer.parseInt(request.getParameter("num"));
Class.forName("org.mariadb.jdbc.Driver");
try (
Connection conn = DriverManager.getConnection(
"jdbc:mariadb://localhost:3307/jspdb", "root", "maria" );
PreparedStatement pstmt = conn.prepareStatement("UPDATE board SET content = ? WHERE num = ?");
) {
// 준비된 문에 content와 num 매개변수를 설정합니다.
pstmt.setString(1, content);
pstmt.setInt(2, num);
// 업데이트 문을 실행합니다.
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
// 수정된 내용을 처리한 후 해당 게시물로 이동
response.sendRedirect("view.jsp?num=" + num);
%>
굳이.. jsp파일을 2개 만들어서 해본거 같다.
오늘 만든 새로 만든 게시판에서 공부해보면서 만들어 봐야겠다.
'프로젝트 기반 자바(JAVA) 응용 SW개발자 취업과정 > JSP 활용 연습' 카테고리의 다른 글
Dao, Dto, ServiceImp ( java) 활용 jsp 정리 (0) | 2023.06.23 |
---|---|
게시판 만들기_2 ( write.jsp / update.jsp )MariaDB (0) | 2023.06.19 |
게시판 만들기_2 ( list.jsp / view.jsp )MariaDB (0) | 2023.06.16 |
게시판 만들기_1 ( list.jsp / view.jsp )MariaDB (0) | 2023.06.15 |
List 회원 클릭 시 회원 정보 조회 하기 (0) | 2023.06.13 |