Notice
Recent Posts
Recent Comments
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
Archives
Today
In Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

A Joyful AI Research Journey๐ŸŒณ๐Ÿ˜Š

[60] 230324 JSP: ์นดํŽ˜ ๊ธ€์“ฐ๊ธฐ ๊ธฐ๋Šฅ, ๊ฒŒ์‹œํŒ ๋ชฉ๋ก ๊ตฌํ˜„ํ•˜๊ธฐ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 60์ผ] ๋ณธ๋ฌธ

๐ŸŒณBootcamp Revision 2023โœจ/HTML5, CSS3, JSP

[60] 230324 JSP: ์นดํŽ˜ ๊ธ€์“ฐ๊ธฐ ๊ธฐ๋Šฅ, ๊ฒŒ์‹œํŒ ๋ชฉ๋ก ๊ตฌํ˜„ํ•˜๊ธฐ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 60์ผ]

yjyuwisely 2023. 3. 24. 11:52

230324 60th class

์ง„๋„: ๊ต์žฌ๊ฐ€ ์•„๋‹Œ ์ž์ฒด ์ˆ˜์—… (๊ต์žฌ: ์ตœ๋ฒ”๊ท ์˜ JSP 2.3 ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ: ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์ค‘๊ธ‰๊นŒ์ง€, ์ €์ž: ์ตœ๋ฒ”๊ท )
์ฑ… ์˜ˆ์ œ ์ฝ”๋“œ: https://github.com/madvirus/jsp23
์ €์ž ๋ธ”๋กœ๊ทธ: https://javacan.tistory.com/

 

์ตœ๋ฒ”๊ท ์˜ JSP 2.3 ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ: ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์ค‘๊ธ‰๊นŒ์ง€ | ์ตœ๋ฒ”๊ท  - ๊ต๋ณด๋ฌธ๊ณ 

์ตœ๋ฒ”๊ท ์˜ JSP 2.3 ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ: ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์ค‘๊ธ‰๊นŒ์ง€ | [์ตœ๋ฒ”๊ท ์˜ JSP 2.3 ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์ค‘๊ธ‰๊นŒ์ง€]๋Š” JSP 2.3์˜ ์ƒˆ๋กœ์šด ํŠน์ง• ๋ฐ˜์˜๊ณผ JSP๋ฅผ ์ง€์›ํ•˜๋Š” ์š”์†Œ์ธ ์„œ๋ธ”๋ฆฟ๊ณผ ํ‘œํ˜„ ์–ธ์–ด ๋“ฑ์˜ ์ƒˆ๋กœ์šด

product.kyobobook.co.kr


์˜ค๋Š˜ ๋ฐฐ์šด ๊ฒƒ ์ค‘ ๊ธฐ์–ตํ•  ๊ฒƒ์„ ์ •๋ฆฌํ–ˆ๋‹ค.


viewBorderList.jsp ViewBoardDetail.jsp
์˜ ์ฝ”๋“œ๋Š” ์•„๋ž˜ ๊ธ€์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

2023.03.23 - [๐ŸŒณK-Digital Revision 2023โœจ/JSP] - [59] 230323 JSP Ch. 14 ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ์ดˆ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 59์ผ]


์š”์•ฝ

[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๋ฒˆ ๊ธ€๋กœ ๋“ค์–ด๊ฐ”๋‹ค.

MySQL์˜ ๊ฒฐ๊ณผ


์ „์ฒด ์ฝ”๋“œ) ๊ฒŒ์‹œํŒ ๊ธ€์“ฐ๊ธฐ ํ…Œ์ด๋ธ”์˜ ์„œ๋ฒ„ 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>

์ œ๋ชฉ๊ณผ ๋‚ด์šฉ์„ ์ž…๋ ฅํ•œ๋‹ค.

๊ฒฐ๊ณผ)

๊ธ€์“ฐ๊ธฐ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋ฐ”๋กœ ๊ฒŒ์‹œํŒ ๋ชฉ๋ก์œผ๋กœ ๊ฐ„๋‹ค.
์ œ๋ชฉ์ž…๋‹ˆ๋‹ค2๋ฅผ ํด๋ฆญํ–ˆ๋‹ค. (์ด ๊ทธ๋ฆผ์€ ๋ฐ‘ ์ค„๋ถ€ํ„ฐ ์•„๋ž˜ ์ฝ”๋“œ๋“ค์˜ ์ˆ˜์ • ์ „์ด๋‹ค.)


<์ˆ˜์ •, ์‚ญ์ œ ๋ฒ„ํŠผ ์ž‘๋™ํ•˜๊ธฐ ํ•˜๊ธฐ>

์ฐธ๊ณ 

์ •์˜ ๋ฐ ํŠน์ง•
<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") %>">

type="text"๋กœ ๋’€์„ ๋•Œ ์ž…๋ ฅ์นธ์ด ์ƒ๊ฒจ์„œ ์•„๋ฌด๊ฑฐ๋‚˜ ํƒ€์ดํ•‘ ๊ฐ€๋Šฅํ•˜๋‹ค.
type = "hidden"์œผ๋กœ ๋’€์„ ๋•Œ ์ž…๋ ฅ ์นธ์ด ์ˆจ๊ฒจ์ง„๋‹ค.

์ „์ฒด ์ฝ”๋“œ) ์ˆ˜์ •, ์‚ญ์ œ ๋ฒ„ํŠผ์„ ์ถ”๊ฐ€ํ•œ 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>

์ˆ˜์ • ์ „) ์ œ๋ชฉ ๋ฐ ๋‚ด์šฉ ์ˆ˜์ • ์ „์ด๋‹ค. 

์ˆ˜์ • ํ›„ ๊ฒฐ๊ณผ) ์ œ๋ชฉ ๋ฐ ๋‚ด์šฉ ์ˆ˜์ • ํ›„๋‹ค. 

๊ฒฐ๊ณผ)

8๋ฒˆ ๊ฒŒ์‹œ๊ธ€์„ ๋ณด๋ฉด ์ˆ˜์ •๋œ ์ œ๋ชฉ, ๋‚ด์šฉ์ด ๋“ค์–ด๊ฐ”๋‹ค.


๊ฒŒ์‹œํŒ ๊ธ€ ์‚ญ์ œํ•˜๊ธฐ
์ฝ”๋“œ) 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>

์‚ญ์ œ ์ „)

๊ฒŒ์‹œ๊ธ€ 7๋ฒˆ์„ ์‚ญ์ œํ•˜๊ธฐ ์ „

์‚ญ์ œ ํ›„)

๊ฒŒ์‹œ๊ธ€ 7๋ฒˆ์„ ์‚ญ์ œํ•œ ํ›„ ๋ฐ”๋กœ ๊ฒŒ์‹œํŒ ๋ชฉ๋ก์œผ๋กœ ๋„˜์–ด๊ฐ”๋‹ค.

๋ฒˆ์™ธ) viewBoardList.jsp์— ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด์„œ ๊ธ€์“ฐ๊ธฐ ๋ฒ„ํŠผ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

<form><input type="submit" value="๊ธ€์“ฐ๊ธฐ1" formaction="insertBoard.jsp"></form>
<a href="insertBoard.jsp">๊ธ€์“ฐ๊ธฐ2</a>

๊ฒฐ๊ณผ)

๊ธ€์“ฐ๊ธฐ1 ๋ฒ„ํŠผ๊ณผ ๊ธ€์“ฐ๊ธฐ2 ๋งํฌ๊ฐ€ ์ƒ๊ธด๋‹ค.
๊ฒŒ์‹œํŒ ๋ชฉ๋ก์—์„œ ๊ธ€์“ฐ๊ธฐ๊ฐ€ ๋œ๋‹ค.


728x90
๋ฐ˜์‘ํ˜•
Comments