์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- Absolute
- AGI
- ai
- AI agents
- AI engineer
- AI researcher
- ajax
- algorithm
- Algorithms
- aliases
- Array ๊ฐ์ฒด
- ASI
- bayes' theorem
- Bit
- Blur
- BOM
- bootstrap
- canva
- challenges
- ChatGPT
- Today
- In Total
A Joyful AI Research Journey๐ณ๐
[60] 230324 JSP: ์นดํ ๊ธ์ฐ๊ธฐ ๊ธฐ๋ฅ, ๊ฒ์ํ ๋ชฉ๋ก ๊ตฌํํ๊ธฐ [K-๋์งํธ ํธ๋ ์ด๋ 60์ผ] ๋ณธ๋ฌธ
[60] 230324 JSP: ์นดํ ๊ธ์ฐ๊ธฐ ๊ธฐ๋ฅ, ๊ฒ์ํ ๋ชฉ๋ก ๊ตฌํํ๊ธฐ [K-๋์งํธ ํธ๋ ์ด๋ 60์ผ]
yjyuwisely 2023. 3. 24. 11:52230324 60th class
์ง๋: ๊ต์ฌ๊ฐ ์๋ ์์ฒด ์์
(๊ต์ฌ: ์ต๋ฒ๊ท ์ JSP 2.3 ์น ํ๋ก๊ทธ๋๋ฐ: ๊ธฐ์ด๋ถํฐ ์ค๊ธ๊น์ง, ์ ์: ์ต๋ฒ๊ท )
์ฑ
์์ ์ฝ๋: https://github.com/madvirus/jsp23
์ ์ ๋ธ๋ก๊ทธ: https://javacan.tistory.com/
์ค๋ ๋ฐฐ์ด ๊ฒ ์ค ๊ธฐ์ตํ ๊ฒ์ ์ ๋ฆฌํ๋ค.
viewBorderList.jsp ViewBoardDetail.jsp
์ ์ฝ๋๋ ์๋ ๊ธ์์ ๋ณผ ์ ์๋ค.
์์ฝ
[1] viewBoardList.jsp
๊ฒ์ํ ๋ชฉ๋ก ๋ฆฌ์คํธ
select(์ฌ๋ฌ ๊ฑด) -> ResultSet(์ ์ ์ฅ) -> while(rs.next())
//select์ ๊ฒฐ๊ณผ๋ฅผ DBMS์์ java๋ก ์ ์ฅํ๊ธฐ ์ํ ๊ฐ์ฒด
ResultSet rs = null;
[2] viewBoardDetail.jsp
๊ฒ์ํ ๋ชฉ๋ก ๋ฆฌ์คํธ -> ์ ๋ชฉ ํด๋ฆญ -> ๊ฒ์ํ ์์ธ ๋ด์ฉ
select(ํ๊ฑด) -> ResultSet -> if(rs.nex())
=====================================================
[3] modifyBoard.jsp
๊ฒ์ํ ์์ธ ๋ด์ฉ -> ์์
(๊ฒ์ํ ์์ธ ๋ด์ฉ -> ์์ ๋ฒํผ -> ์์ ํ๋ฉด -> ์์ (ex. ๋ค์ด๋ฒ ์นดํ์ ๊ฒฝ์ฐ))
update -> update ๋จ -> ๊ฒ์ํ ์์ธ ๋ด์ฉ ํ๋ฉด์ผ๋ก(viewDetailList.jsp)
[4] deleteBoard.jsp
๊ฒ์ํ ์์ธ ๋ด์ฉ -> ์ญ์
delete -> delete -> ๊ฒ์ํ ๋ชฉ๋ก ๋ฆฌ์คํธ ํ๋ฉด์ผ๋ก(viewBoardList.jsp)
[5] insertBoard.jsp
๊ฒ์ํ ๊ธ์ฐ๊ธฐ ํ๋ฉด -> ๊ธ์ฐ๊ธฐ -> ์ ๋ชฉ, ๋ด์ฉ์ ์๋ฒ๋ก ์ ์ก
[6] insertBoardServer.jsp
DB์์
insert -> insert ๋จ -> ๊ฒ์ํ ๋ชฉ๋ก ๋ฆฌ์คํธ ํ๋ฉด์ผ๋ก(viewBoardList.jsp)
์นดํ ๊ธ์ฐ๊ธฐ jsp ์์
get ๋ฐฉ์์ผ ๊ฒฝ์ฐ ์๋์ ๊ฐ๋ค. ํ์ง๋ง ์ฝ๋์์ post๋ก ์ง์ ํ๋ค.
http://localhost:8081/insertBoardServer.jsp?title=์ ๋ชฉ์
๋๋ค.&content=๋ด์ฉ๋ฑ๋ฑ
<!-- ์ ๋ชฉ์
๋๋ค -> title -->
<!-- ๋ด์ฉ๋ฑ๋ฑ -> content -->
<!-- title, content -> insertBoardServer.jsp -->
MySQL์ ๋ฐ์ดํฐ๋ฅผ ๋ฃ๊ธฐ ์ํด ์ฐ๋ ์ฝ๋๋ ์๋์ ๊ฐ๋ค.
์ฐธ๊ณ : 2023.03.17 - [๐ณK-Digital Revision 2023โจ/SQL] - [54~55] 230316, 230317 SQL ๋ฌธ์ ํ์ด (member, board ํ
์ด๋ธ) [K-๋์งํธ ํธ๋ ์ด๋ 54~55์ผ]
SELECT * FROM sample.board;
ALTER TABLE `sample`.`board`
CHANGE COLUMN `count` `count` INT NULL DEFAULT 0 COMMENT '์กฐํ์' ; -- ์กฐํ์์ ๋ํดํธ๊ฐ์ 0์ผ๋ก ๋ง๋ ๋ค.
ALTER TABLE `sample`.`board`
DROP FOREIGN KEY `board_ibfk_1`;
ALTER TABLE `sample`.`board`
CHANGE COLUMN `title` `title` VARCHAR(50) NOT NULL COMMENT '์ ๋ชฉ' , -- ์
๋ ฅํ ์ ์๋ ๊ธธ์ด๋ฅผ ๋ฐ๊พผ๋ค.
CHANGE COLUMN `content` `content` VARCHAR(100) NULL DEFAULT NULL COMMENT '๋ด์ฉ' , -- ์
๋ ฅํ ์ ์๋ ๊ธธ์ด๋ฅผ ๋ฐ๊พผ๋ค.
CHANGE COLUMN `id` `id` VARCHAR(20) NULL DEFAULT NULL COMMENT '์์ฑ์' ; -- ์
๋ ฅํ ์ ์๋ ๊ธธ์ด๋ฅผ ๋ฐ๊พผ๋ค.
ALTER TABLE `sample`.`board`
ADD CONSTRAINT `board_ibfk_1`
FOREIGN KEY (`id`)
REFERENCES `sample`.`member` (`id`);
insert into board (title, content, id) values ('์๋
ํ์ธ์', '์ค๋ ์ ๋ชจ ์์ต๋๋ค.','abcd');
insert into board (title, content) values ('์๋
ํ์ธ์', '์ค๋ ์ ๋ชจ ์์ต๋๋ค.');
insert into board(title, content) values('์ ๋ชฉ', '๋ด์ฉ');
SELECT * FROM sample.board;
์ฝ๋) ๊ฒ์ํ ๊ธ์ฐ๊ธฐ ํ ์ด๋ธ insertBoard.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>์นดํ ๊ธ์ฐ๊ธฐ</title>
</head>
<body>
<form action = "insertBoardServer.jsp" method="post"> <!-- ๊ธ์ฐ๊ธฐ๋ฅผ ํ๊ธฐ ์ํ ์๋ฒ.jsp -->
<table border="1">
<tr>
<td>์ ๋ชฉ</td>
<td><input type="text"></td>
</tr>
<tr>
<td>๋ด์ฉ</td>
<td><textarea rows="10" cols="30"></textarea></td>
</tr>
<tr>
<td colspan="2"><!-- ์ข์ฐ์ ์
๋ค์ ๋ณํฉํ๋ค (๊ฐ๋ก๋ก ํฉ์นจ, ๊ฐ๋ก๊ฐ ๊ธด ์ง์ฌ๊ฐํ) -->
<input type="submit" value="๊ธ์ฐ๊ธฐ">
</td>
</tr>
</table>
</form>
</body>
</html>
๊ฒฐ๊ณผ)
์ฝ๋) ๊ธ์ฐ๊ธฐ ํ
์ด๋ธ insertBoard.jsp
์ฒ์์ ์ค๋ฅ๊ฐ ๋ฌ๋ ๋ฐ name๊ณผ ๊ทธ ๊ฐ์ ๋ฃ์ด์ผ ํ๋ค.
name = "title", name = "content"
<input type="text" name="title">
<textarea rows="10" cols="30" name="content"></textarea>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>์นดํ ๊ธ์ฐ๊ธฐ</title>
</head>
<body>
<form action = "insertBoardServer.jsp" method="post"> <!-- ๊ธ์ฐ๊ธฐ๋ฅผ ํ๊ธฐ ์ํ ์๋ฒ.jsp -->
<table border="1">
<tr>
<td>์ ๋ชฉ</td>
<td><input type="text" name="title"></td>
</tr>
<tr>
<td>๋ด์ฉ</td>
<td><textarea rows="10" cols="30" name="content"></textarea></td>
</tr>
<tr>
<td colspan="2"><!-- ์ข์ฐ์ ์
๋ค์ ๋ณํฉํ๋ค (๊ฐ๋ก๋ก ํฉ์นจ, ๊ฐ๋ก๊ฐ ๊ธด ์ง์ฌ๊ฐํ) -->
<input type="submit" value="๊ธ์ฐ๊ธฐ">
</td>
</tr>
</table>
</form>
</body>
</html>
์๋ฒ ์ฝ๋) ๊ฒ์ํ ๊ธ์ฐ๊ธฐ์ ์๋ฒ insertBoardServer.jsp
์ฒ์์ ์ค๋ฅ๊ฐ ๋ฌ๋๋ฐ ์๋ ์ฝ๋๋ฅผ ๋นผ๋จน์ด์๋ค.
pstmt = conn.prepareStatement(query);
๋ MySQL์์ ํ ์ด๋ธ ์ฒดํฌํ์ ๋ POST ๋ฐฉ์์ charset=UTF-8 ์ ์ฉ์ด ์ ๋๋ฏ๋ก MySQL์์์ ํ๊ธ ๊นจ์ง ํด๊ฒฐ์ ์ํด ์๋ ์ฝ๋๋ฅผ ๋ฃ๋๋ค.
<% request.setCharacterEncoding("utf-8"); %> <!-- ํ๊ธ๊นจ์ง ํด๊ฒฐ -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<% request.setCharacterEncoding("utf-8"); %> <!-- ํ๊ธ๊นจ์ง ํด๊ฒฐ -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>๊ฒ์ํ ๊ธ์ฐ๊ธฐ ์๋ฒ</title>
</head>
<body>
<%
String title = request.getParameter("title");
String content = request.getParameter("content");
// 1. JDBC ๋๋ผ์ด๋ฒ ๋ก๋ฉ(mysql)
Class.forName("com.mysql.jdbc.Driver");
//java api์ db์ ์ฐ๊ฒฐํ๊ธฐ ์ํ ๊ฐ์ฒด
Connection conn = null;
//workbench sql์ ์์ฑํ ํ ctrl+enter(๊ฒฐ๊ณผ๋์ถ)
//sql๋ฌธ์ฅ์ ์์ฑ์ ํ๊ณ , ๊ทธ sql๋ฌธ์ฅ์ ์คํํ๊ธฐ ์ํ ๊ฐ์ฒด
//Statement stmt = null;
PreparedStatement pstmt = null;
try{
String jdbcDriver = "jdbc:mysql://localhost:3306/sample?serverTimezone=Asia/Seoul";
String dbUser = "root";
String dbPass = "1234";
//2. ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ปค๋ฅ์
์์ฑ
conn = DriverManager.getConnection(jdbcDriver,dbUser,dbPass);
// ์ฌ๊ธฐ๊น์ง๊ฐ DB ์ฐ๊ฒฐ ์ฝ๋
//3. Statement ์์ฑ
//SQL ์ฝ๋ ๋ณต์ฌ
String query = "insert into board(title, content) values(?, ?)"; //๋ฌผ์ํ(?) ์ฒ๋ฆฌ๋ก ์ฌ์ฉ์๊ฐ ์
๋ ฅํ ๊ฐ์ ๋ฃ๊ฒ ๋๋ค.
pstmt = conn.prepareStatement(query);
pstmt.setString(1, title); //์ฒซ๋ฒ์งธ ๋ฌผ์ํ์๋ title๊ฐ์ ๋ฃ์ด๋ผ.
pstmt.setString(2, content); //๋๋ฒ์งธ ๋ฌผ์ํ์๋ content๊ฐ์ ๋ฃ์ด๋ผ.
//4. ์ฟผ๋ฆฌ ์คํ
//workbench์์ ctrl+enter(์ฟผ๋ฆฌ๋ฌธ์ฅ์ ์คํ)๋ฅผ ํตํด ๋์ถ๋ select๊ฒฐ๊ณผ๋ฅผ Resultset์ ์ ์ฅ
//rs = pstmt.executeQuery();
pstmt.executeUpdate(); //insert์์
๋ select์ผ ๋ executeUpdate()๋ฅผ ์ฌ์ฉํ๋ค. ๋๋จธ์ง๋ rs=pstmt.executeQuery();๋ฅผ ์ด๋ค.
}catch(Exception ex){
System.out.println(ex.getMessage()); //์ฝ์์ ์ค๋ฅ ๋ฉ์์ง๋ฅผ ๋์ด๋ค.
out.println("DB ์ฐ๊ฒฐ ์คํจ");
}finally{
if(conn!=null){conn.close();}
}
%>
</body>
</html>
๊ฒฐ๊ณผ) MySQL์ ํ๊ธ ๊นจ์ง ์์ด ๋ฐ์ดํฐ๊ฐ 4๋ฒ ๊ธ๋ก ๋ค์ด๊ฐ๋ค.
์ ์ฒด ์ฝ๋) ๊ฒ์ํ ๊ธ์ฐ๊ธฐ ํ
์ด๋ธ์ ์๋ฒ insertBoardServer.jsp
๋ด์ฉ ์
๋ ฅ ํ ๊ฒ์ํ ๋ชฉ๋ก ๋ฆฌ์คํธ๋ก ์ด๋ํ๋ค.
์ถ๊ฐ ํ ์ฝ๋๋ ์๋์ ๊ฐ๋ค.
//insert ํ ํ ๊ฒ์ํ ๋ชฉ๋ก ๋ฆฌ์คํธ ํ๋ฉด(viewBoardList.jsp)์ผ๋ก ์ด๋ํ๋ผ๊ณ ์๋ตํ๋ค.
response.sendRedirect("viewBoardList.jsp");
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<% request.setCharacterEncoding("utf-8"); %> <!-- ํ๊ธ๊นจ์ง ํด๊ฒฐ -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>๊ฒ์ํ ๊ธ์ฐ๊ธฐ ์๋ฒ</title>
</head>
<body>
<%
String title = request.getParameter("title");
String content = request.getParameter("content");
// 1. JDBC ๋๋ผ์ด๋ฒ ๋ก๋ฉ(mysql)
Class.forName("com.mysql.jdbc.Driver");
//java api์ db์ ์ฐ๊ฒฐํ๊ธฐ ์ํ ๊ฐ์ฒด
Connection conn = null;
//workbench sql์ ์์ฑํ ํ ctrl+enter(๊ฒฐ๊ณผ๋์ถ)
//sql๋ฌธ์ฅ์ ์์ฑ์ ํ๊ณ , ๊ทธ sql๋ฌธ์ฅ์ ์คํํ๊ธฐ ์ํ ๊ฐ์ฒด
//Statement stmt = null;
PreparedStatement pstmt = null;
try{
String jdbcDriver = "jdbc:mysql://localhost:3306/sample?serverTimezone=Asia/Seoul";
String dbUser = "root";
String dbPass = "1234";
//2. ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ปค๋ฅ์
์์ฑ
conn = DriverManager.getConnection(jdbcDriver,dbUser,dbPass);
// ์ฌ๊ธฐ๊น์ง๊ฐ DB ์ฐ๊ฒฐ ์ฝ๋
//3. Statement ์์ฑ
//SQL ์ฝ๋ ๋ณต์ฌ
String query = "insert into board(title, content) values(?, ?)"; //๋ฌผ์ํ(?) ์ฒ๋ฆฌ๋ก ์ฌ์ฉ์๊ฐ ์
๋ ฅํ ๊ฐ์ ๋ฃ๊ฒ ๋๋ค.
pstmt = conn.prepareStatement(query);
pstmt.setString(1, title); //์ฒซ๋ฒ์งธ ๋ฌผ์ํ์๋ title๊ฐ์ ๋ฃ์ด๋ผ.
pstmt.setString(2, content); //๋๋ฒ์งธ ๋ฌผ์ํ์๋ content๊ฐ์ ๋ฃ์ด๋ผ.
//4. ์ฟผ๋ฆฌ ์คํ
//workbench์์ ctrl+enter(์ฟผ๋ฆฌ๋ฌธ์ฅ์ ์คํ)๋ฅผ ํตํด ๋์ถ๋ select๊ฒฐ๊ณผ๋ฅผ Resultset์ ์ ์ฅ
//rs = pstmt.executeQuery();
pstmt.executeUpdate(); //insert๊ฐ ์ ์์ ์ผ๋ก ์ฒ๋ฆฌ. insert์์
๋ select์ผ ๋ executeUpdate()๋ฅผ ์ฌ์ฉํ๋ค. ๋๋จธ์ง๋ rs=pstmt.executeQuery();๋ฅผ ์ด๋ค.
//insert ํ ํ ๊ฒ์ํ ๋ชฉ๋ก ๋ฆฌ์คํธ ํ๋ฉด(viewBoardList.jsp)์ผ๋ก ์ด๋ํ๋ผ๊ณ ์๋ตํ๋ค.
response.sendRedirect("viewBoardList.jsp");
}catch(Exception ex){
System.out.println(ex.getMessage()); //์ฝ์์ ์ค๋ฅ ๋ฉ์์ง๋ฅผ ๋์ด๋ค.
out.println("DB ์ฐ๊ฒฐ ์คํจ");
}finally{
if(conn!=null){conn.close();}
}
%>
</body>
</html>
๊ฒฐ๊ณผ)
<์์ , ์ญ์ ๋ฒํผ ์๋ํ๊ธฐ ํ๊ธฐ>
์ ์ ๋ฐ ํน์ง
<input> ํ๊ทธ์ formaction ์์ฑ์ ํผ ๋ฐ์ดํฐ(form data)๊ฐ ์๋ฒ๋ก ์ ์ถ๋ ๋ ์ ๋ ฅ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ ํ์ผ์ URL์ ๋ช ์ํจ.
์ด ์์ฑ์ <input> ์์์ type ์์ฑ๊ฐ์ด “submit” ๋๋ “image”์ธ ๊ฒฝ์ฐ์๋ง ์ฌ์ฉํ ์ ์์ต๋๋ค.
formaction ์์ฑ์ <form> ์์์ action ์์ฑ๊ฐ์ ์ฌ์ ์(overriding)ํฉ๋๋ค.
๋ฌธ๋ฒ
<input type="submit|image" formaction="URL">
์ฐธ๊ณ : http://www.tcpschool.com/html-tag-attrs/input-formaction
์์ , ์ญ์ ๋ฒํผ์ ๋ง๋๋ ์๋ ์ฝ๋๋ฅผ ์ถ๊ฐํ๋ค.
<form method="post">๋ฅผ ์ด๋ค.
<form method="post">
<table>
<!-- (์ค๋ต) -->
</table>
<input type="submit" value="์์ " formaction="modifyBoard.jsp">
<input type="submit" value="์ญ์ " formaction="removeBoard.jsp">
</form>
์ค๋ฅ๊ฐ ๋ฌ์๋๋ฐ ์๋ ์ฝ๋์์ <%=rs.getString("content") %>์ ์์น๋ฅผ ์๋ชป ๋ฃ์ด์์ด๋ค.
<textarea> </textarea> ์ฌ์ด์ <%=rs.getString("content") %>์ ๋ฃ๋๋ค.
<textarea name="content" rows="10" cols="30"><%=rs.getString("content") %></textarea>
๋ <form>์ ์๋ ์ฝ๋๋ก ์์ ํด์ผ ์์ ๋ฒํผ์ ๋๋ ์ ๋ ์๋ no๋ฅผ ํฌํจํ URL์ด ์ ์ง๋๋ค.
<form method="post">
http://localhost:8081/JSPExample/ch14/viewBoardDetail.jsp?no=8
<form>์ผ๋ก ๋๋ฉด URL์ ๊ฒฝ๋ก๊ฐ ์์ฒด jsp ํ์ผ์ธ http://localhost:8081/JSPExample/ch14/viewBoardDetail๊ฐ ๋๋ค.
๋ด๊ฐ ํท๊ฐ๋ ธ๋ ๊ฒ (ํ์ดํ ํํ๊ฐ ์๋ ์ฝ๋ ํํ๋ก ๋ง๋ค๋ ค๋ฉด readonly๋ type="hidden" ์ฌ์ฉํ๊ธฐ)
<!-- ์ฒซ๋ฒ์งธ ๋ฐฉ๋ฒ readonly ์ด๋ค. -->
๋ฒํธ: <input type="text" name="no" value="<%=rs.getString("no") %>" readonly>
<!-- ๋๋ฒ์งธ ๋ฐฉ๋ฒ: hidden ์ด๋ค. -->
๋ฒํธ: <%=rs.getInt("no") %> <input type="hidden" name="no" value="<%=rs.getString("no") %>">
์ ์ฒด ์ฝ๋) ์์ , ์ญ์ ๋ฒํผ์ ์ถ๊ฐํ viewBoardDetail.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head><title>๊ฒ์ํ ๋ด์ฉ</title></head>
<body>
<%
//method๋ฐฉ์์ด post์ผ ๋ ํ๊ธ๊นจ์ง ๋ฐฉ์ง
request.setCharacterEncoding("utf-8");
int no = Integer.parseInt(request.getParameter("no"));
// 1. JDBC ๋๋ผ์ด๋ฒ ๋ก๋ฉ(mysql)
Class.forName("com.mysql.jdbc.Driver");
//java api์ db์ ์ฐ๊ฒฐํ๊ธฐ ์ํ ๊ฐ์ฒด
Connection conn = null;
//workbench sql์ ์์ฑํ ํ ctrl+enter(๊ฒฐ๊ณผ๋์ถ)
//sql๋ฌธ์ฅ์ ์์ฑ์ ํ๊ณ , ๊ทธ sql๋ฌธ์ฅ์ ์คํํ๊ธฐ ์ํ ๊ฐ์ฒด
//Statement stmt = null;
PreparedStatement pstmt = null;
//select์ ๊ฒฐ๊ณผ๋ฅผ DBMS์์ java๋ก ์ ์ฅํ๊ธฐ ์ํ ๊ฐ์ฒด
ResultSet rs = null;
//์ฐ๊ฒฐํ๊ธฐ
//๋๋ผ์ด๋ธ ๋งค๋์ ์๊ฒ Connection๊ฐ์ฒด๋ฅผ ๋ฌ๋ผ๊ณ ์์ฒญํ๋ค.
//Connection์ ์ป๊ธฐ ์ํด ํ์ํ ๋ฐ์ดํฐ(url,id,๋น๋ฐ๋ฒํธ)
try{
String jdbcDriver = "jdbc:mysql://localhost:3306/sample?serverTimezone=Asia/Seoul";
String dbUser = "root";
String dbPass = "1234";
//2. ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ปค๋ฅ์
์์ฑ
conn = DriverManager.getConnection(jdbcDriver,dbUser,dbPass);
String query = "select * from board where no =?"; //executeQuery
//3. Statement ์์ฑ
pstmt = conn.prepareStatement(query);
pstmt.setInt(1, no); //#####์ฒซ๋ฒ์งธ ๋ฌผ์ํ์๋ no๊ฐ์ ๋ฃ์ด๋ผ.#####
//4. ์ฟผ๋ฆฌ ์คํ
//workbench์์ ctrl+enter(์ฟผ๋ฆฌ๋ฌธ์ฅ์ ์คํ)๋ฅผ ํตํด ๋์ถ๋ select๊ฒฐ๊ณผ๋ฅผ Resultset์ ์ ์ฅ
rs = pstmt.executeQuery();
//pstmt.executeUpdate();
//5. ์ฟผ๋ฆฌ ์คํ ๊ฒฐ๊ณผ ์ถ๋ ฅ
//rs.next: ํ
์ด๋ธ์์ ๋ฐ ํ์ผ๋ก ๋ด๋ ค์จ๋ค. ๋ฐ์ดํฐ๊ฐ ์์ผ๋ฉด ๊ฒฐ๊ณผ๊ฐ true (while๋ฌธ์ด true)์ด๋ฏ๋ก ๋ฐ๋ณตํ๋ค.
//while๋ฌธ์ด false์ด๋ฉด ์ข
๋ฃ
if(rs.next()){
%>
<!-- ์ฌ๊ธฐ์ ๊ฒ์ํ ๊ธ ๋์ค๊ฒ ๋ง๋ ๋ค. -->
<form method="post">
<table border="1">
<tr>
<td colspan=2>
<!-- ๋ฒํธ: <input type="text" name="no" value="<%=rs.getString("no") %>" readonly> --> <!-- ์ฒซ๋ฒ์งธ ๋ฐฉ๋ฒ -->
๋ฒํธ: <%=rs.getInt("no") %> <input type="hidden" name="no" value="<%=rs.getString("no") %>"> <!-- ๋๋ฒ์งธ ๋ฐฉ๋ฒ: hidden ์ด๋ค. -->
์กฐํ์: <%=rs.getInt("count") %>
์ข์์: <%=rs.getInt("good") %>
</td>
</tr>
<tr>
<td>์ ๋ชฉ</td>
<td><input type="text" name="title" value="<%=rs.getString("title") %>"></td> <!-- input ํ๊ทธ๋ฅผ ๋ฃ์ด์ title ํธ์ง ๊ฐ๋ฅํ๋ค. -->
</tr>
<tr>
<td>๋ด์ฉ</td>
<td><textarea name="content" rows="10" cols="30"><%=rs.getString("content") %></textarea></td> <!-- input ํ๊ทธ๋ฅผ ๋ฃ์ด์ content ํธ์ง ๊ฐ๋ฅํ๋ค. -->
</tr>
<tr>
<td colspan="2">
<input type="submit" value="์์ " formaction="modifyBoard.jsp">
<input type="submit" value="์ญ์ " formaction="removeBoard.jsp">
</td>
</tr>
</table>
</form>
<%
} // if end
//out.println("DB ์ฐ๊ฒฐ ์ฑ๊ณต");
}catch(SQLException ex){
out.println("DB ์ฐ๊ฒฐ ์คํจ");
}finally{
if(conn!=null){conn.close();}
}
%>
</body>
</html>
์ ์ฒด ์ฝ๋) ๊ฒ์๊ธ ์์ ๋ฒํผ์ด ์๋ํ๋ค. modifyBoard.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>๊ฒ์ํ ์์ ๋ฒํผ ์๋</title>
</head>
<body>
<%
//method๋ฐฉ์์ด post์ผ ๋ ํ๊ธ๊นจ์ง ๋ฐฉ์ง
request.setCharacterEncoding("utf-8");
//๊ฒ์ํ ๋ฒํธ
int no = Integer.parseInt(request.getParameter("no"));
//์ ๋ชฉ
String title = request.getParameter("title");
//๋ด์ฉ
String content = request.getParameter("content");
System.out.println("no=>"+no);
System.out.println("title=>"+title);
System.out.println("content=>"+content);
// 1. JDBC ๋๋ผ์ด๋ฒ ๋ก๋ฉ(mysql)
Class.forName("com.mysql.jdbc.Driver");
//java api์ db์ ์ฐ๊ฒฐํ๊ธฐ ์ํ ๊ฐ์ฒด
Connection conn = null;
//workbench sql์ ์์ฑํ ํ ctrl+enter(๊ฒฐ๊ณผ๋์ถ)
//sql๋ฌธ์ฅ์ ์์ฑ์ ํ๊ณ , ๊ทธ sql๋ฌธ์ฅ์ ์คํํ๊ธฐ ์ํ ๊ฐ์ฒด
//Statement stmt = null;
PreparedStatement pstmt = null;
try{
String jdbcDriver = "jdbc:mysql://localhost:3306/sample?serverTimezone=Asia/Seoul";
String dbUser = "root";
String dbPass = "1234";
//2. ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ปค๋ฅ์
์์ฑ
conn = DriverManager.getConnection(jdbcDriver,dbUser,dbPass);
String query = "update board set title=?, content=? where no=?"; //executeQuery
//3. Statement ์์ฑ
pstmt = conn.prepareStatement(query);
pstmt.setString(1, title); //1๋ฒ์งธ ๋ฌผ์ํ์๋ title๊ฐ์ ๋ฃ์ด๋ผ.
pstmt.setString(2, content); //2๋ฒ์งธ ๋ฌผ์ํ์๋ content๊ฐ์ ๋ฃ์ด๋ผ.
pstmt.setInt(3, no); //3๋ฒ์งธ ๋ฌผ์ํ์๋ no๊ฐ์ ๋ฃ์ด๋ผ.
//4. ์ฟผ๋ฆฌ ์คํ
//workbench์์ ctrl+enter(์ฟผ๋ฆฌ๋ฌธ์ฅ์ ์คํ)๋ฅผ ํตํด ๋์ถ๋ select๊ฒฐ๊ณผ๋ฅผ Resultset์ ์ ์ฅ
//rs = pstmt.executeQuery();
pstmt.executeUpdate();
response.sendRedirect("viewBoardDetail.jsp?no="+no); //๊ฒ์ํ ๊ธ ์์ฒด์ viewBoardDetail.jsp๊ฐ ์ ์ง๋๋ค.
}catch(Exception ex){
out.println("DB ์ฐ๊ฒฐ ์คํจ");
}finally{
if(conn!=null){conn.close();}
}
%>
</body>
</html>
์์ ์ ) ์ ๋ชฉ ๋ฐ ๋ด์ฉ ์์ ์ ์ด๋ค.
์์ ํ ๊ฒฐ๊ณผ) ์ ๋ชฉ ๋ฐ ๋ด์ฉ ์์ ํ๋ค.
๊ฒฐ๊ณผ)
๊ฒ์ํ ๊ธ ์ญ์ ํ๊ธฐ
์ฝ๋) removeBoard.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>๊ฒ์ํ ์ญ์ ๋ฒํผ ์๋</title>
</head>
<body>
<%
//method๋ฐฉ์์ด post์ผ ๋ ํ๊ธ๊นจ์ง ๋ฐฉ์ง
request.setCharacterEncoding("utf-8");
//๊ฒ์ํ ๋ฒํธ
int no = Integer.parseInt(request.getParameter("no"));
//์ ๋ชฉ
String title = request.getParameter("title");
//๋ด์ฉ
String content = request.getParameter("content");
System.out.println("no=>"+no);
System.out.println("title=>"+title);
System.out.println("content=>"+content);
// 1. JDBC ๋๋ผ์ด๋ฒ ๋ก๋ฉ(mysql)
Class.forName("com.mysql.jdbc.Driver");
//java api์ db์ ์ฐ๊ฒฐํ๊ธฐ ์ํ ๊ฐ์ฒด
Connection conn = null;
//workbench sql์ ์์ฑํ ํ ctrl+enter(๊ฒฐ๊ณผ๋์ถ)
//sql๋ฌธ์ฅ์ ์์ฑ์ ํ๊ณ , ๊ทธ sql๋ฌธ์ฅ์ ์คํํ๊ธฐ ์ํ ๊ฐ์ฒด
//Statement stmt = null;
PreparedStatement pstmt = null;
try{
String jdbcDriver = "jdbc:mysql://localhost:3306/sample?serverTimezone=Asia/Seoul";
String dbUser = "root";
String dbPass = "1234";
//2. ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ปค๋ฅ์
์์ฑ
conn = DriverManager.getConnection(jdbcDriver,dbUser,dbPass);
String query = "DELETE FROM board WHERE no=?;"; //executeQuery
//String query = "update board set title=?, content=? where no=?"; //executeQuery
//3. Statement ์์ฑ
pstmt = conn.prepareStatement(query);
pstmt.setInt(1, no); //์ฒซ๋ฒ์งธ ๋ฌผ์ํ์๋ no๊ฐ์ ๋ฃ์ด๋ผ.
//4. ์ฟผ๋ฆฌ ์คํ
//workbench์์ ctrl+enter(์ฟผ๋ฆฌ๋ฌธ์ฅ์ ์คํ)๋ฅผ ํตํด ๋์ถ๋ select๊ฒฐ๊ณผ๋ฅผ Resultset์ ์ ์ฅ
//rs = pstmt.executeQuery();
pstmt.executeUpdate();
response.sendRedirect("viewBoardList.jsp"); //๊ฒ์ํ ๋ชฉ๋ก์ธ viewBoardList.jsp๋ก ๊ฐ๋ค.
}catch(Exception ex){
out.println("DB ์ฐ๊ฒฐ ์คํจ");
}finally{
if(conn!=null){conn.close();}
}
%>
</body>
</html>
์ญ์ ์ )
์ญ์ ํ)
๋ฒ์ธ) viewBoardList.jsp์ ์๋ ์ฝ๋๋ฅผ ์ถ๊ฐํด์ ๊ธ์ฐ๊ธฐ ๋ฒํผ์ ์ถ๊ฐํ๋ค.
<form><input type="submit" value="๊ธ์ฐ๊ธฐ1" formaction="insertBoard.jsp"></form>
<a href="insertBoard.jsp">๊ธ์ฐ๊ธฐ2</a>
๊ฒฐ๊ณผ)