[๊ตฌ๋””์•„์นด๋ฐ๋ฏธ IT๊ตญ๋น„์ง€์›] home.jsp๋ฅผ ์™„์„ฑํ•˜๊ณ , ๊ฒŒ์‹œ๊ธ€ ์ƒ์„ธํŽ˜์ด์ง€์™€ ๋Œ“๊ธ€ ์ž‘์„ฑ ๋ฐ ๋ฆฌ์ŠคํŠธ ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ–ˆ๋‹ค.


๐Ÿ’ก home.jsp

home.jsp์— ํŽ˜์ด์ง• ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•˜๊ณ , ์ „๋ฐ˜์ ์œผ๋กœ ๊ฐ€๋…์„ฑ์„ ๋†’์ด๊ธฐ ์œ„ํ•œ ์ˆ˜์ •์„ ํ–ˆ๋‹ค.

๐Ÿ“ ์ž‘์„ฑ

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import = "java.sql.*" %>
<%@ page import = "java.util.*" %> <!-- HashMap ์‚ฌ์šฉ -->
<%@ page import = "vo.*" %>
<%
	// ์ฝ˜์†”์ฐฝ ์ถœ๋ ฅ ์ƒ‰์ƒ ์ง€์ •
	final String RED = "\u001B[31m";
	final String BG_RED = "\u001B[41m";
	final String GREEN = "\u001B[32m";
	final String BG_GREEN = "\u001B[42m";
	final String RESET = "\u001B[0m";

	// 1) ์š”์ฒญ๊ฐ’ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ (๋ฆฌ๋‹ค์ด๋ ‰์…˜)
	// ์„ธ์…˜๊ฐ’์ด ์žˆ๋Š”์ง€ ๋˜๋Š” ํŽ˜์ด์ง• ์‹œ rowPerPage๋‚˜ currentPage ๋“ฑ..
	// 1-1) session JSP ๋‚ด์žฅ(๊ธฐ๋ณธ)๊ฐ์ฒด
	// ์ผ๋‹จ ์ง€๊ธˆ์€ ์—†์Œ
	// 1-2) request / response JSP ๋‚ด์žฅ(๊ธฐ๋ณธ)๊ฐ์ฒด
	// currentPage, rowPerPage, localName
	System.out.println(RED + request.getParameter("currentPage") + " <- home param currentPage");
	System.out.println(request.getParameter("rowPerPage") + " <- home param rowPerPage");
	System.out.println(request.getParameter("localName") + " <- home param localName" + RESET);
	
	int currentPage = 1;
	if(request.getParameter("currentPage") != null) {
		currentPage = Integer.parseInt(request.getParameter("currentPage"));
	}
	int rowPerPage = 10;
	if(request.getParameter("rowPerPage") != null) {
		rowPerPage = Integer.parseInt(request.getParameter("rowPerPage"));
	}
	String localName = "์ „์ฒด";
	if(request.getParameter("localName") != null) {
		localName = request.getParameter("localName");
	}
	
	System.out.println(GREEN + currentPage + " <- home currentPage");
	System.out.println(rowPerPage + " <- home rowPerPage");
	System.out.println(localName + " <- home localName" + RESET);
	
	// 2) ๋ชจ๋ธ(=๊ฒฐ๊ณผ์…‹) ๊ณ„์ธต (๊ผญ db์—์„œ๋งŒ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค)
	// ๋“œ๋ผ์ด๋ฒ„ ๋กœ๋”ฉ ๋ฐ db ์ ‘์†
	String driver = "org.mariadb.jdbc.Driver";
	String dburl = "jdbc:mariadb://127.0.0.1:3306/userboard";
	String dbuser = "****";
	String dbpw = "****";
	Class.forName(driver);
	Connection conn = DriverManager.getConnection(dburl, dbuser, dbpw);
	
	// 2-1) ์„œ๋ธŒ๋ฉ”๋‰ด๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•œ ์ฟผ๋ฆฌ ์ž‘์„ฑ (์„œ๋ธŒ๋ฉ”๋‰ด ๊ฒฐ๊ณผ์…‹)
	// ์ „์ฒด์™€ ๊ฐ localName์˜ count๋ฅผ ์กฐํšŒํ•˜๊ธฐ ์œ„ํ•ด ์ฟผ๋ฆฌ๋ฅผ ํ•ฉ์นจ(UNION ALL ์‚ฌ์šฉ)
	/*
		SELECT '์ „์ฒด' localName, COUNT(local_name) cnt FROM board
		UNION ALL 
		SELECT local_name, COUNT(local_name) FROM board GROUP BY local_name
	*/
	String subMenuSql = "SELECT '์ „์ฒด' localName, COUNT(local_name) cnt FROM board UNION ALL SELECT local_name, COUNT(local_name) FROM board GROUP BY local_name";
	PreparedStatement subMenuStmt = conn.prepareStatement(subMenuSql);
	ResultSet subMenuRs = subMenuStmt.executeQuery();
	System.out.println(BG_GREEN + subMenuStmt + " <- home subMenuStmt");
	
	// HashMap์„ ArrayList์— ๋„ฃ๊ธฐ
	ArrayList<HashMap<String, Object>> subMenuList = new ArrayList<HashMap<String, Object>>();
	while(subMenuRs.next()) {
		HashMap<String, Object> m = new HashMap<String, Object>();
		m.put("localName", subMenuRs.getString("localName"));
		m.put("cnt", subMenuRs.getInt("cnt"));
		subMenuList.add(m);
	}
	
	// 2-2) ์นดํ…Œ๊ณ ๋ฆฌ ๋ณ„ ๊ฒŒ์‹œ๊ธ€ 10๊ฐœ์”ฉ ์ถœ๋ ฅ (๊ฒŒ์‹œํŒ ๋ชฉ๋ก ๊ฒฐ๊ณผ์…‹)
	// ๋ชจ๋ธ๊ฐ’์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜ ์ถ”๊ฐ€
	int startRow = (currentPage - 1) * rowPerPage;
	
	// ์ฟผ๋ฆฌ ์ž‘์„ฑ
	PreparedStatement boardStmt = null;
	ResultSet boardRs = null;
	// ๊ธฐ๋ณธ ์ฟผ๋ฆฌ
	String boardSql = "SELECT board_no boardNo, board_title boardTitle, local_name localName, createdate createdate FROM board";
	// localName ์„ ํƒ ์‹œ ์ค‘๊ฐ„์— ์ถ”๊ฐ€๋˜๋Š” ์ฟผ๋ฆฌ
	String localNameSql = " WHERE local_name = ?";
	// ๋งˆ์ง€๋ง‰์— ๋“ค์–ด๊ฐ€๋Š” ์ฟผ๋ฆฌ // ์ž‘์„ฑ์ผ์ž ์ˆœ์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
	String lastSql = " ORDER BY createdate DESC LIMIT ?, ?";
	
	if(localName.equals("์ „์ฒด") || localName.equals("")) {
		boardSql = boardSql + lastSql;
		boardStmt = conn.prepareStatement(boardSql);
		boardStmt.setInt(1, startRow);
		boardStmt.setInt(2, rowPerPage);
	} else {
		boardSql = boardSql + localNameSql + lastSql;
		boardStmt = conn.prepareStatement(boardSql);
		boardStmt.setString(1, localName);
		boardStmt.setInt(2, startRow);
		boardStmt.setInt(3, rowPerPage);
	}
	System.out.println(boardStmt + " <- home boardStmt" + RESET);
	boardRs = boardStmt.executeQuery(); // DB์ฟผ๋ฆฌ ๊ฒฐ๊ณผ์…‹ ๋ชจ๋ธ
	
	// ์ผ๋ฐ˜์ ์ธ ์ž๋ฃŒ๊ตฌ์กฐ๋กœ ๋ณ€๊ฒฝ ArrayList
	ArrayList<Board> boardList = new ArrayList<Board>(); // ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์‚ฌ์šฉํ•  ๋ชจ๋ธ (์‚ฌ์ด์ฆˆ 0)
	// boardRs -> boardList
	while(boardRs.next()) {
		Board b = new Board(); // ์–˜๊ฐ€ while๋ฌธ ๋ฐ–์— ์žˆ์œผ๋ฉด..! b๋Š” ๊ณ„์† ๋ฐ”๋€Œ์–ด์•ผ๋˜๋Š”๋ฐ ๋ฐ”๋€Œ์ง€ ์•Š์Œ
		b.boardNo = boardRs.getInt("boardNo");
		b.localName = boardRs.getString("localName");
		b.boardTitle = boardRs.getString("boardTitle");
		b.createdate = boardRs.getString("createdate");
		boardList.add(b); // ๋งˆ์ง€๋ง‰์— ์—ฌ๊ธฐ์„œ b๋ฅผ list์— ์ถ”๊ฐ€ํ•˜๊ณ  ์ƒˆ๋กœ์šด b(new)๋ฅผ ๋งŒ๋“ค์–ด์•ผ ๊ณ„์†ํ•ด์„œ ๋‹ค๋ฅธ b์˜ ๊ฐ’๋“ค์ด list์— ์ถ”๊ฐ€๋  ๊ฒƒ
	}
	// ๋””๋ฒ„๊น…
	System.out.println(BG_RED + boardList + " <- home boardList");
	System.out.println(boardList.size() + " <- home boardList.size" + RESET);
	
	// 2-3) ํŽ˜์ด์ง•์„ ์œ„ํ•œ ๋ชจ๋ธ๊ฐ’ ๊ตฌํ•˜๊ธฐ
	// ๋ชจ๋ธ๊ฐ’์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜ ์ถ”๊ฐ€
	int totalCount = 0;
	int lastPage = 0;
	
	// ์ฟผ๋ฆฌ ์ž‘์„ฑ
	PreparedStatement pageStmt = null;
	ResultSet pageRs = null;
	// ๊ธฐ๋ณธ ์ฟผ๋ฆฌ
	String pageSql = "SELECT count(*) FROM board";
	// localName์— ๋”ฐ๋ผ where์ ˆ ์ถ”๊ฐ€
	if(localName.equals("์ „์ฒด") || localName.equals("")) {
		pageStmt = conn.prepareStatement(pageSql);
	} else {
		pageSql = pageSql + localNameSql;
		pageStmt = conn.prepareStatement(pageSql);
		pageStmt.setString(1, localName);
	}
	System.out.println(BG_GREEN + pageStmt + " <- home pageStmt" + RESET);
	
	// totalCount // ์ฟผ๋ฆฌ ์‹คํ–‰ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ณ€์ˆ˜์— ์ €์žฅ
	pageRs = pageStmt.executeQuery();
	if(pageRs.next()) {
		totalCount = pageRs.getInt("count(*)");
	}
	System.out.println(GREEN + totalCount + " <- home totalCount");
	
	// lastPage
	lastPage = totalCount / rowPerPage;
	if(totalCount % rowPerPage != 0) {
		lastPage = lastPage + 1;
	}
	System.out.println(lastPage + " <- home lastPage" + RESET);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>home.jsp</title>
	<!-- ๋ถ€ํŠธ์ŠคํŠธ๋žฉ5 ์‚ฌ์šฉ -->
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
	<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<!-- include ํŽ˜์ด์ง€ : ๋ฉ”์ธ๋ฉ”๋‰ด(๊ฐ€๋กœ) -->	
<div>
	<!-- ์•ก์…˜ํƒœ๊ทธ -->
	<jsp:include page="/inc/mainmenu.jsp"></jsp:include>
</div>
<!-------- include ํŽ˜์ด์ง€ ๋ ------->	
	
<div class="container mt-5">
	<!----------------------------------------------------- ๋กœ๊ทธ์ธํผ (์„ธ์…˜์— id๊ฐ’์ด null์ผ๋•Œ๋งŒ ์ถœ๋ ฅ) ---------------------------------------------------->
	<div>
		<%
			if(session.getAttribute("loginMemberId") == null) {
		%>
				<h3 class="mt-4">๋กœ๊ทธ์ธ</h3>
				<div class="text-danger">
					<%	// msg ๋ฐœ์ƒ์‹œ ์ถœ๋ ฅ
						if(request.getParameter("msg") != null) {
					%>
							<%=request.getParameter("msg")%>
					<%
						}
					%>
				</div>
				<form action="<%=request.getContextPath()%>/member/loginAction.jsp" method="post">
					<table>
						<tr>
							<td>์•„์ด๋””</td>
							<td>
								<input type="text" name="memberId">
							</td>
						</tr>
						<tr>
							<td>ํŒจ์Šค์›Œ๋“œ</td>
							<td>
								<input type="password" name="memberPw">
							</td>
						</tr>
					</table>
					<button type="submit" class="btn btn-success">๋กœ๊ทธ์ธ</button>
				</form>
		<%
			}
		%>
	</div>
	<!----------------------------------------------------------- ๋กœ๊ทธ์ธํผ ๋ ------------------------------------------------------------------>
	
	<br>
	
	<div class="row">
	<!------------------------------------------------ ์„œ๋ธŒ๋ฉ”๋‰ด(์„ธ๋กœ) subMenuList๋ชจ๋ธ ์ถœ๋ ฅ --------------------------------------------------------->
		<div class="col-sm-4">
			<h3 class="mt-4">์นดํ…Œ๊ณ ๋ฆฌ</h3>
			<ul class="nav nav-pills flex-column">
				<%
					for(HashMap<String, Object> m : subMenuList) {
				%>
						<li class="nav-item">
							<a href="<%=request.getContextPath()%>/home.jsp?localName=<%=(String)m.get("localName")%>" class="nav-link">
								<%=(String)m.get("localName")%>(<%=(Integer)m.get("cnt")%>)
								<!-- ๊ฐ’ํƒ€์ž…์ด Objectํƒ€์ž…์ด๋ฏ€๋กœ ํ˜•๋ณ€ํ™˜ -->
							</a>
						</li>
				<%		
					}
				%>
			</ul>
		</div>
	<!--------------------------------------------------------- subMenuList ๋ ----------------------------------------------------------------->
	
	<!-------------------------------------------------- ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„ ๊ฒŒ์‹œ๊ธ€ 10๊ฐœ์”ฉ boardList๋ชจ๋ธ ์ถœ๋ ฅ------------------------------------------------------>
		<div class="col-sm-8">
			<h3 class="mt-4">๊ฒŒ์‹œํŒ</h3>
			<table class="table container">
				<thead class="table-success">
					<tr>	
						<th>์นดํ…Œ๊ณ ๋ฆฌ</th>
						<th>์ œ๋ชฉ</th>
						<th>์ž‘์„ฑ์ผ์ž</th>
					</tr>
				</thead>
				<tbody>
					<%
						for(Board b : boardList) {
					%>
							<tr>
								<td><%=b.localName%></td>
								<td>
									<a href="<%=request.getContextPath()%>/board/boardOne.jsp?boardNo=<%=b.boardNo%>">
										<%=b.boardTitle%>
									</a>
								</td>
								<td><%=b.createdate.substring(0, 10)%></td>
							</tr>
					<%
						}
					%>
				</tbody>
			</table>
			<!------- ํŽ˜์ด์ง• ์‹œ์ž‘ ------->
			<div class="text-center">
				<%
					if(currentPage > 1) {
				%>
						<a href="<%=request.getContextPath()%>/home.jsp?currentPage=<%=currentPage - 1%>&rowPerPage=<%=rowPerPage%>&localName=<%=localName%>" class="btn btn-success btn-sm">
							์ด์ „
						</a>
				<%
					}
				%>
					<%=currentPage%>ํŽ˜์ด์ง€
				<%
					if(lastPage > currentPage) {
				%>
					<a href="<%=request.getContextPath()%>/home.jsp?currentPage=<%=currentPage + 1%>&rowPerPage=<%=rowPerPage%>&localName=<%=localName%>" class="btn btn-success btn-sm">
						๋‹ค์Œ
					</a>
				<%
					}
				%>
			</div>
			<!------- ํŽ˜์ด์ง• ๋ ------->
		</div>
	<!--------------------------------------------------------- boardList ๋ ------------------------------------------------------------------>
	</div>
</div>

	<br>
	
<!-- include ํŽ˜์ด์ง€ : Copyright -->
<div>
	<%
		// request.getRequestDispatcher(request.getContextPath()+"./inc/copyright.jsp").include(request, response);
		// ๋„ˆ๋ฌด ๊ธธ๋‹ค. ๊ทธ๋ž˜์„œ ์•ก์…˜ํƒœ๊ทธ๋ฅผ ํ™œ์šฉํ•œ๋‹ค!
	%> 
	<!-- ์•ก์…˜ํƒœ๊ทธ -->
	<jsp:include page="/inc/copyright.jsp"></jsp:include>
</div>
<!-------- include ํŽ˜์ด์ง€ ๋ ------->
	
</body>
</html>
  • ๊ฐ€๋…์„ฑ์„ ๋†’์ด๊ธฐ์œ„ํ•ด ์ฃผ์„์œผ๋กœ ์„น์…˜ ๋‚˜๋ˆ„๊ธฐ

  • ํ•œ ํŽ˜์ด์ง€์—์„œ ์—ฌ๋Ÿฌ๊ฐœ์˜ ์ฟผ๋ฆฌ ์ž‘์„ฑ ์‹œ ์ˆซ์ž๋กœ ๊ตฌ๋ถ„ํ•˜์ง€ ๋ง๊ณ (ex. sql1 , sql2 ... ) ์–ด๋–ค sql๋ฌธ์ธ์ง€ ์ด๋ฆ„ ์ง“๋Š” ๊ฒƒ์ด ๊ฐ€๋…์„ฑ๋„ ์ข‹๊ณ , ๋””๋ฒ„๊น… ์‹œ ํ™•์ธํ•˜๊ธฐ ์šฉ์ดํ•จ!

  • ๊ถ๊ธˆํ•œ ์ 

    1. home.jsp๋Š” ํšŒ์›/๋น„ํšŒ์› ๋ชจ๋‘ ๋“ค์–ด์˜ฌ ์ˆ˜ ์žˆ๋Š” ํŽ˜์ด์ง€์ธ๋ฐ๋„ ์„ธ์…˜๊ฐ’ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ํ•ด์•ผํ•˜๋Š”์ง€?
    2. ํŽ˜์ด์ง• ์‹œ rowPerPage๋„ ๋งค๋ฒˆ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ํ•ด์•ผํ•˜๋Š”์ง€ / ๊ฐ’์„ ๋„˜๊ฒจ์ค˜์•ผํ•˜๋Š”์ง€
    3. ์ง€๊ธˆ์€ ์ƒ˜ํ”Œ๋ฐ์ดํ„ฐ๋กœ createdate๋ฅผ 2022.5.4~2023.5.4 ์ค‘ ๋žœ๋ค๋ถ€์—ฌ๋ฅผ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ORDER BY์‹œ boardNo๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜์ง€๋งŒ, ์ƒ˜ํ”Œ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ๋ฉด(์ˆœ์ฐจ์ ์œผ๋กœ ๊ฒŒ์‹œ๊ธ€ ์ž‘์„ฑ์ด ์ด๋ฃจ์–ด์ง„๋‹ค๋ฉด) ์ƒˆ๋กœ์šด ๊ฒŒ์‹œ๊ธ€ ์ƒ์„ฑ์‹œ ์ž๋™์œผ๋กœ ์ค‘๋ณต์—†๋Š” ์ˆซ์ž๊ฐ€ ๋ถ€์—ฌ๋˜๋Š” ๊ธฐ๋ณธํ‚ค์ธ boardNo๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•ด๋„ ๋˜๋Š” ๊ฑธ๊นŒ? ์•„๋‹ˆ๋ฉด ๊ทธ๋ ‡๋‹ค ํ•ด๋„ createdate๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•˜๋Š” ๊ฒƒ์ด ๋งž๋Š”๊ฑด๊ฐ€?
    4. ์ž๋ฐ”์—์„œ substring ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ , db์—์„œ ๊ฐ€์ ธ์˜ฌ ๋•Œ๋ถ€ํ„ฐ ์ผ๋ถ€๊ธ€์ž๋งŒ ์ถ”์ถœํ•ด์„œ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด substr(createdate,1,10)์œผ๋กœ ์ž‘์„ฑํ–ˆ๋”๋‹ˆ ORDER BY๋„ ์‹œ๊ฐ„์ด ์ž˜๋ฆฐ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ๋˜์–ด๋ฒ„๋ ธ๋‹ค. ORDER BY์ ˆ์—๋Š” substr์„ ์ž‘์„ฑํ•˜์ง€ ์•Š์•˜๋Š”๋ฐ๋„ ๊ฐ™์ด ์ ์šฉ๋˜๋Š” ๊ฒƒ์ธ์ง€?

๐Ÿ“ ๊ฒฐ๊ณผ

ํŽ˜์ด์ง•์„ ์ถ”๊ฐ€ํ–ˆ๋‹ค. ์นดํ…Œ๊ณ ๋ฆฌ ๋ณ€๊ฒฝ์‹œ(localName ๊ฐ’์— ๋”ฐ๋ผ) ์ด์ „/๋‹ค์Œ ๋ฒ„ํŠผ์„ ๋‹ค๋ฅด๊ฒŒ ์ถœ๋ ฅ.


๐Ÿ’ก boardOne.jsp

home.jsp์—์„œ ๊ฒŒ์‹œ๊ธ€ ์ œ๋ชฉ(boardTitle) ํด๋ฆญ ์‹œ ๊ฒŒ์‹œ๊ธ€ ์ƒ์„ธ ํŽ˜์ด์ง€(boardOne.jsp)๋กœ ์ด๋™ํ•˜๋„๋ก ํ–ˆ๋‹ค. ๊ฒŒ์‹œ๊ธ€ ์ƒ์„ธ ํŽ˜์ด์ง€์—๋Š” ๊ฒŒ์‹œ๊ธ€ ์ƒ์„ธ ๋ณด๊ธฐ/ ๋Œ“๊ธ€ ์ž…๋ ฅ ํผ/ ๋Œ“๊ธ€ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•˜๋„๋ก ํ–ˆ๋‹ค. ๋˜ํ•œ ๋Œ“๊ธ€ ์ž…๋ ฅ ํผ์€ ํšŒ์› ์ „์šฉ์œผ๋กœ ๊ตฌํ˜„ํ•˜๊ณ , ๋Œ“๊ธ€ ๋ฆฌ์ŠคํŠธ์€ ํŽ˜์ด์ง•์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ํ–ˆ๋‹ค.


๐Ÿ“Œ comment ํ…Œ์ด๋ธ” ์ถ”๊ฐ€


๋Œ“๊ธ€ ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ ํ…Œ์ด๋ธ”์„ ์ถ”๊ฐ€ํ–ˆ๋‹ค. ๊ธฐ๋ณธํ‚ค๋Š” comment_no๋กœ ๋‘์—ˆ๊ณ  ์–ด๋–ค ๊ฒŒ์‹œ๊ธ€(board_no)์—, ์–ด๋–ค ์‚ฌ์šฉ์ž(member_id)๊ฐ€ ์ž‘์„ฑํ–ˆ๋Š”์ง€ ์•Œ์•„์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ๊ฐ ์™ธ๋ž˜ํ‚ค๋กœ ๋‘์—ˆ๋‹ค.


๐Ÿ“Œ ์ƒ˜ํ”Œ๋ฐ์ดํ„ฐ ์ž‘์„ฑ


๋Œ“๊ธ€ ์ƒ˜ํ”Œ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ–ˆ๋‹ค. board_no๋Š” ํŽ˜์ด์ง• ์ž‘์—… ํ…Œ์ŠคํŠธํ‹€ ์œ„ํ•ด 1000,998,996,994,992 ์ด 5๊ฐœ์˜ ๊ฒŒ์‹œ๊ธ€์—๋งŒ ์ง€์ •ํ–ˆ๋‹ค.


๐Ÿ“Œ JOIN๋ฌธ ๋ฏธ๋ฆฌ๋ณด๊ธฐ

10์ผ์ฐจ ์ˆ˜์—…๋•Œ join์ด ์–ด๋–ค ๊ฒƒ์ธ์ง€์— ๋Œ€ํ•ด์„œ๋งŒ ๊ฐ„๋žตํ•˜๊ฒŒ ๋ฐฐ์› ์—ˆ๋‹ค.

์ค‘๋ณต๋œ ๋ฐ์ดํ„ฐ ์ž‘์„ฑ์„ ์ค„์—ฌ์„œ ์˜ค๋ฅ˜๋ฅผ ์ตœ์†Œํ™”ํ•˜๊ธฐ ์œ„ํ•ด ํ…Œ์ด๋ธ”์„ ๋ถ„๋ฆฌํ•˜๋Š”๋ฐ ์ด๊ฒƒ์„ ์ •๊ทœํ™”๋ผ๊ณ  ํ•œ๋‹ค. ์ •๊ทœํ™”ํ•œ 2๊ฐœ ์ด์ƒ์˜ ๋ฐ์ดํ„ฐ ํ…Œ์ด๋ธ”์„ ์—ฐ๊ฒฐํ•ด์„œ ์ตœ์ข… ๊ฒฐ๊ณผ๋ฅผ ๋‚ด๋Š” ๊ฒƒ์„ join์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

/* boardOne.jsp์˜ ๊ฒฐ๊ณผ์…‹ ๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ• */

/* 1) ๋‘๋ฒˆ์˜ ์ฟผ๋ฆฌ๋กœ ๋”ฐ๋กœ๋”ฐ๋กœ (userboardํ”„๋กœ์ ํŠธ์—์„œ ์‚ฌ์šฉ) */
SELECT board_no, board_title
FROM board
WHERE board_no = 1000;

SELECT comment_no, board_no, comment_content
FROM COMMENT
WHERE board_no = 1000
LIMIT 0 , 10;

/* 2) ํ•œ๋ฒˆ์— ์ฟผ๋ฆฌ๋กœ (์กฐ์ธ ๋ฌธ๋ฒ• ์‚ฌ์šฉ, ์กฐ์ธ ๋ฌธ๋ฒ•์€ SQL๋ฌธ๋ฒ• ์ˆ˜์—…์—์„œ ๋‹ค์‹œ...) */
SELECT b.board_title, b.board_no, c.board_no, c.comment_content
FROM board b INNER JOIN comment c
ON b.board_no = c.board_no
WHERE b.board_no = 1000
ORDER BY c.createdate DESC;

๊ฒŒ์‹œ๊ธ€์˜ ์ •๋ณด(board ํ…Œ์ด๋ธ”)์™€ ๋Œ“๊ธ€ ๋ฆฌ์ŠคํŠธ(comment ํ…Œ์ด๋ธ”)์„ ๊ฐ™์ด ์ถœ๋ ฅํ•˜๋ ค๋ฉด ๋‘๋ฒˆ์˜ ์ฟผ๋ฆฌ ์ž‘์„ฑ์„ ํ†ตํ•ด ๊ฐ๊ฐ ์ถœ๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์ง€๋งŒ, board ํ…Œ์ด๋ธ”๊ณผ comment ํ…Œ์ด๋ธ”์„ joinํ•œ๋‹ค๋ฉด ํ•œ๋ฒˆ์˜ ์ฟผ๋ฆฌ ์ž‘์„ฑ์œผ๋กœ๋„ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•  ๊ฒƒ์ด๋‹ค. ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ์šฐ์„  ๋‘๋ฒˆ์˜ ์ฟผ๋ฆฌ ์ž‘์„ฑ์„ ํ†ตํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ๋กœ ํ–ˆ๊ณ , ๋‹ค์Œ์‹œ๊ฐ„์— ๋ฐฐ์šธ join๋ฌธ์„ ๋ฏธ๋ฆฌ ๋ณด์—ฌ์ฃผ์…จ๋‹ค.

join๋ฌธ ์ž‘์„ฑ ์‹œ์—๋Š” ํ…Œ์ด๋ธ”์— ๋ณ„๋ช…(AS)์„ ์ง€์ •ํ•ด์ฃผ๊ณ , ํ…Œ์ด๋ธ”๋ช…+๋งˆ์นจํ‘œ(.)+์ปฌ๋Ÿผ๋ช…์œผ๋กœ ์ž‘์„ฑํ•œ๋‹ค.

INNER JOIN ๋ฅผ ์ƒ๋žตํ•˜๊ณ  ์‰ผํ‘œ(,)๋กœ ์ž‘์„ฑ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ํ‘œ์ค€ ๋ฐฉ๋ฒ•์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— DB๋งˆ๋‹ค ์‚ฌ์šฉ์ด ์•ˆ๋  ์ˆ˜๋„ ์žˆ๋‹ค.
์‹คํ–‰ ์ˆœ์„œ๋Š” from์ ˆ์ด ์ œ์ผ ๋จผ์ € ์‹คํ–‰๋œ๋‹ค. ์ฆ‰, from์ ˆ์—์„œ ์ž‘์„ฑ๋œ ๋ณ„๋ช…(AS)๋Š” ๋‹ค๋ฅธ ์ ˆ์—์„œ๋„ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•œ ๊ฒƒ์ด๋‹ค.
join๋ฌธ ์ž‘์„ฑ์‹œ ON ๋’ค์— ์ผ์น˜ํ•˜๋Š” ์™ธ๋ž˜ํ‚ค์˜ ์กฐ๊ฑด์„ ๋„ฃ๋Š”๋‹ค.


๐Ÿ“Œ Comment ํด๋ž˜์Šค ์ƒ์„ฑ


Voํƒ€์ž…์˜ Comment ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์˜€๋‹ค.


๐Ÿ“Œ ์ž‘์„ฑ

โœ” ๊ตฌํ˜„ํ•ด์•ผํ•  ์ 

  • ๊ฒŒ์‹œ๊ธ€ ์ƒ์„ธ ์ •๋ณด ์ถœ๋ ฅ (board ํ…Œ์ด๋ธ”)
  • ๋Œ“๊ธ€ ์ž…๋ ฅ ํผ ์ƒ์„ฑ (ํšŒ์› ์ „์šฉ)
  • ๋Œ“๊ธ€ ๋ฆฌ์ŠคํŠธ ์ถœ๋ ฅ (comment ํ…Œ์ด๋ธ”), ์ตœ๊ทผ์ˆœ์œผ๋กœ ์ •๋ ฌ
  • ๋Œ“๊ธ€ ๋ฆฌ์ŠคํŠธ ํŽ˜์ด์ง•ํ•˜๊ธฐ

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import = "java.sql.*" %>
<%@ page import = "java.util.*" %>
<%@ page import = "vo.*" %>
<%
	//์ฝ˜์†”์ฐฝ ์ถœ๋ ฅ ์ƒ‰์ƒ ์ง€์ •
	final String RED = "\u001B[31m";
	final String BG_RED = "\u001B[41m";
	final String GREEN = "\u001B[32m";
	final String BG_GREEN = "\u001B[42m";
	final String RESET = "\u001B[0m";
	
	// 1) ์š”์ฒญ๊ฐ’ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ
	// currentPage, rowPerPage, boardNo
	System.out.println(RED + request.getParameter("currentPage") + " <- boardOne param currentPage");
	System.out.println(request.getParameter("rowPerPage") + " <- boardOne param rowPerPage");
	System.out.println(request.getParameter("boardNo") + " <- boardOne param boardNo" + RESET);

	// boardNo๊ฐ€ null์ด๊ฑฐ๋‚˜ ๊ณต๋ฐฑ์ด๋ฉด home์œผ๋กœ ๋ฆฌ๋‹ค์ด๋ ‰์…˜
	if(request.getParameter("boardNo") == null
			|| request.getParameter("boardNo").equals("")) {
		response.sendRedirect(request.getContextPath() + "/home.jsp");
		return;
	}
	int boardNo = Integer.parseInt(request.getParameter("boardNo"));
	// currentPage
	int currentPage = 1;
	if(request.getParameter("currentPage") != null) {
		currentPage = Integer.parseInt(request.getParameter("currentPage"));
	}
	// rowPerPage
	int rowPerPage = 10;
	if(request.getParameter("rowPerPage") != null) {
		rowPerPage = Integer.parseInt(request.getParameter("rowPerPage"));
	}
	// ๋””๋ฒ„๊น…
	System.out.println(GREEN + currentPage + " <- boardOne currentPage");
	System.out.println(rowPerPage + " <- boardOne rowPerPage");
	System.out.println(boardNo + " <- boardOne boardNo" + RESET);
	
	// 2) ๋ชจ๋ธ๊ฐ’ ๊ตฌํ•˜๊ธฐ
	// ๋“œ๋ผ์ด๋ฒ„ ๋กœ๋”ฉ ๋ฐ db ์ ‘์†
	String driver = "org.mariadb.jdbc.Driver";
	String dburl = "jdbc:mariadb://127.0.0.1:3306/userboard";
	String dbuser = "****";
	String dbpw = "****";
	Class.forName(driver);
	Connection conn = DriverManager.getConnection(dburl, dbuser, dbpw);
	
	// 2-1) ํ•ด๋‹น boardNo์˜ ๊ฒŒ์‹œ๊ธ€ ์ƒ์„ธ๋ณด๊ธฐ - boardOne
	// SELECT * FROM board WHERE board_no = ?
	PreparedStatement boardOneStmt = null;
	ResultSet boardOneRs = null;
	String boardOneSql = "SELECT board_no boardNo, local_name localName, board_title boardTitle, board_content boardContent, member_id memberId, createdate createdate, updatedate updatedate FROM board WHERE board_no = ?";
	boardOneStmt = conn.prepareStatement(boardOneSql);
	boardOneStmt.setInt(1, boardNo);
	System.out.println(BG_GREEN + boardOneStmt + " <- boardOne boardOneStmt");
	
	// Voํƒ€์ž…์œผ๋กœ ๋ฐ”๊พธ๊ธฐ
	boardOneRs = boardOneStmt.executeQuery();
	Board board = null;
	if(boardOneRs.next()) {
		board = new Board();
		board.boardNo = boardOneRs.getInt("boardNo");
		board.localName = boardOneRs.getString("localName");
		board.boardTitle = boardOneRs.getString("boardTitle");
		board.boardContent = boardOneRs.getString("boardContent");
		board.memberId = boardOneRs.getString("memberId");
		board.createdate = boardOneRs.getString("createdate");
		board.updatedate = boardOneRs.getString("updatedate");
	}
	
	// 2-2) ํ•ด๋‹น boardNo์˜ ๋Œ“๊ธ€ ๋ฆฌ์ŠคํŠธ ์ถœ๋ ฅ - commentList
	// ๋ชจ๋ธ๊ฐ’์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜ ์ถ”๊ฐ€
	int startRow = (currentPage - 1) * rowPerPage;
	
	// ์ฟผ๋ฆฌ ์ž‘์„ฑ // ์ž‘์„ฑ์ผ์ž ์ˆœ์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
	// SELECT comment_content, member_id, createdate, updatedate FROM comment WHERE board_no = ? ORDER BY createdate DESC LIMIT ?, ?
	PreparedStatement commentListStmt = null;
	ResultSet commentListRs = null;
	String commentListSql = "SELECT comment_content commentContent, member_id memberId, createdate createdate, updatedate updatedate FROM comment WHERE board_no = ? ORDER BY createdate DESC LIMIT ?, ?";
	commentListStmt = conn.prepareStatement(commentListSql);
	commentListStmt.setInt(1, boardNo);
	commentListStmt.setInt(2, startRow);
	commentListStmt.setInt(3, rowPerPage);
	System.out.println(commentListStmt + " <- boardOne commentListStmt");
	
	// Voํƒ€์ž…์˜ ArrayList๋กœ ๋ฐ”๊พธ๊ธฐ
	commentListRs = commentListStmt.executeQuery();
	ArrayList<Comment> commentList = new ArrayList<Comment>();
	while(commentListRs.next()) {
		Comment c = new Comment();
		c.commentContent = commentListRs.getString("commentContent");
		c.memberId = commentListRs.getString("memberId");
		c.createdate = commentListRs.getString("createdate");
		c.updatedate = commentListRs.getString("updatedate");
		commentList.add(c);
	}
	// ๋””๋ฒ„๊น…
	System.out.println(BG_RED + commentList + " <- boardOne commentList");
	System.out.println(commentList.size() + " <- boardOne commentList.size" + RESET);
	
	// 2-3) ํŽ˜์ด์ง•์„ ์œ„ํ•œ ๋ชจ๋ธ๊ฐ’ ๊ตฌํ•˜๊ธฐ
	// ๋ชจ๋ธ๊ฐ’์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜ ์ถ”๊ฐ€
	int totalCount = 0;
	int lastPage = 0;
	
	// ํ•ด๋‹น boardNo์˜ ๋Œ“๊ธ€์˜ ์ด ๊ฐฏ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ์ฟผ๋ฆฌ ์ž‘์„ฑ
	PreparedStatement commentPageStmt = null;
	ResultSet commentPageRs = null;
	String commentPageSql = "SELECT count(*) FROM comment WHERE board_no = ?";
	commentPageStmt = conn.prepareStatement(commentPageSql);
	commentPageStmt.setInt(1, boardNo);
	System.out.println(BG_GREEN + commentPageStmt + " <- boardOne commentPageStmt" + RESET);
	
	// totalCount
	commentPageRs = commentPageStmt.executeQuery();
	if(commentPageRs.next()) {
		// ๊ฒฐ๊ณผ๊ฐ’์„ ๋ณ€์ˆ˜์— ์ €์žฅ
		totalCount = commentPageRs.getInt("count(*)");
	}
	System.out.println(GREEN + totalCount + " <- boardOne totalCount");
	
	// lastPage
	lastPage = totalCount / rowPerPage;
	if(totalCount % rowPerPage != 0) {
		lastPage = lastPage + 1;
	}
	System.out.println(lastPage + " <- boardOne lastPage" + RESET);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>boardOne.jsp</title>
	<!-- ๋ถ€ํŠธ์ŠคํŠธ๋žฉ5 ์‚ฌ์šฉ -->
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
	<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<!-- include ํŽ˜์ด์ง€ : ๋ฉ”์ธ๋ฉ”๋‰ด(๊ฐ€๋กœ) -->	
<div>
	<!-- ์•ก์…˜ํƒœ๊ทธ -->
	<jsp:include page="/inc/mainmenu.jsp"></jsp:include>
</div>
<!-------- include ํŽ˜์ด์ง€ ๋ ------->	

<div class="container mt-5">
	<!-------------------------------------------------- ๊ฒŒ์‹œ๊ธ€ ์ƒ์„ธ๋ณด๊ธฐ boardOne๋ชจ๋ธ ์ถœ๋ ฅ------------------------------------------------------>
	<h3 class="mt-4">๊ฒŒ์‹œ๊ธ€ ์ƒ์„ธ๋ณด๊ธฐ</h3>
	<table class="table container">
		<tr>
			<th class="table-success">๊ธ€๋ฒˆํ˜ธ</th>
			<td>No.<%=board.boardNo%></td>
		</tr>
		<tr>
			<th class="table-success">์นดํ…Œ๊ณ ๋ฆฌ</th>
			<td><%=board.localName%></td>
		</tr>
		<tr>
			<th class="table-success">์ž‘์„ฑ์ž</th>
			<td><%=board.memberId%></td>
		</tr>
		<tr>
			<th class="table-success">์ œ๋ชฉ</th>
			<td><%=board.boardTitle%></td>
		</tr>
		<tr>
			<th class="table-success">๋‚ด์šฉ</th>
			<td><%=board.boardContent%></td>
		</tr>
		<tr>
			<th class="table-success">์ž‘์„ฑ์ผ์ž</th>
			<td><%=board.createdate%></td>
		</tr>
		<tr>
			<th class="table-success">์ˆ˜์ •์ผ์ž</th>
			<td><%=board.updatedate%></td>
		</tr>
	</table>
	<div class="text-center">
		<a href="" class="btn btn-success">์ˆ˜์ •</a>
		<a href="" class="btn btn-success">์‚ญ์ œ</a>
	</div>
	<!--------------------------------------------------------- boardOne ๋ ------------------------------------------------------------------>
		
	<br>
	
	<!----------------------------------------------------- ๋Œ“๊ธ€์ž…๋ ฅํผ (์„ธ์…˜์— id๊ฐ’์ด ์žˆ์„๋•Œ๋งŒ ์ถœ๋ ฅ) --------------------------------------------------->
	<div>
		<%
			// ๋กœ๊ทธ์ธ ์‚ฌ์šฉ์ž๋งŒ ๋Œ“๊ธ€ ์ž…๋ ฅ์„ ํ—ˆ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋ถ„๊ธฐ
			if(session.getAttribute("loginMemberId") != null) {
				// ํ˜„์žฌ ๋กœ๊ทธ์ธ ์‚ฌ์šฉ์ž์˜ ์•„์ด๋””๋ฅผ ๋ณ€์ˆ˜์— ๋„ฃ๊ธฐ
				String loginMemberId = (String)session.getAttribute("loginMemberId"); // ํ˜•๋ณ€ํ™˜ํ•ด์ฃผ๊ธฐ
		%>
				<h5 class="mt-4">๋Œ“๊ธ€์ž…๋ ฅ</h5>
				<div class="text-danger">
					<%
						// msg ๋ฐœ์ƒ์‹œ ์ถœ๋ ฅ
						if(request.getParameter("msg") != null) {
					%>
							<%=request.getParameter("msg") %>
					<%
						}
					%>
				</div>
				<form action="<%=request.getContextPath()%>/board/insertCommentAction.jsp" method="post">
					<!-- boardNo์™€ memberId๋Š” ์ž…๋ ฅ๊ฐ’์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— hidden์œผ๋กœ ๋„˜๊น€ -->
					<input type="hidden" name="boardNo" value="<%=board.boardNo%>">
					<input type="hidden" name="memberId" value="<%=loginMemberId%>">
					<table>
						<tr>
							<td>๋Œ“๊ธ€</td>
							<td>
								<textarea rows="3" cols="80" name="commentContent"></textarea>
							</td>
						</tr>
					</table>
					<button type="submit" class="btn btn-success">์ž…๋ ฅ</button>
				</form>
		<%
			}
		%>
	</div>
	<!----------------------------------------------------------- ๋Œ“๊ธ€์ž…๋ ฅํผ ๋ ----------------------------------------------------------------->

	<br>
	
	<!------------------------------------------------------- ๋Œ“๊ธ€๋ฆฌ์ŠคํŠธ commentList๋ชจ๋ธ ์ถœ๋ ฅ  ----------------------------------------------------->
	<table class="table table-hover">
		<thead>
			<tr>
				<th>๋Œ“๊ธ€ ๋‚ด์šฉ</th>
				<th>์ž‘์„ฑ์ž</th>
				<th>์ž‘์„ฑ์ผ์ž</th>
				<th>์ˆ˜์ •์ผ์ž</th>
				<th>์ˆ˜์ •</th>
				<th>์‚ญ์ œ</th>
			</tr>
		</thead>
		<tbody>			
			<%
				for(Comment c : commentList) {
			%>
					<tr>
						<td><%=c.commentContent%></td>
						<td><%=c.memberId%></td>
						<td><%=c.createdate%></td>
						<td><%=c.updatedate%></td>
						<td>
							<a href="" class="btn btn-outline-secondary btn-sm">์ˆ˜์ •</a>
						</td>
						<td>
							<a href="" class="btn btn-outline-secondary btn-sm">์‚ญ์ œ</a>
						</td>
					</tr>
			<%
				}
			%>
		</tbody>
	</table>
	<%
		// ๋Œ“๊ธ€์ด ์—†์œผ๋ฉด ์ถœ๋ ฅ
		if(totalCount == 0) {
	%>
			<h5>๋Œ“๊ธ€์ด ์—†์Šต๋‹ˆ๋‹ค ใ… ใ… </h5>
	<%
		}
	%>
	
	<!------- ํŽ˜์ด์ง• ์‹œ์ž‘ ------->
	<div class="text-center">
		<%
			if(currentPage > 1) {
		%>
				<a href="<%=request.getContextPath()%>/board/boardOne.jsp?currentPage=<%=currentPage - 1 %>&rowPerPage=<%=rowPerPage%>&boardNo=<%=boardNo%>" class="btn btn-success btn-sm">
					์ด์ „
				</a>
		<%
			}
		%>
			<%=currentPage%>ํŽ˜์ด์ง€
		<%
			if(lastPage > currentPage) {
		%>
			<a href="<%=request.getContextPath()%>/board/boardOne.jsp?currentPage=<%=currentPage + 1%>&rowPerPage=<%=rowPerPage%>&boardNo=<%=boardNo%>" class="btn btn-success btn-sm">
				๋‹ค์Œ
			</a>
		<%
			}
		%>
	</div>
	<!------- ํŽ˜์ด์ง• ๋ ------->
	<!--------------------------------------------------------------- commentList ๋ -------------------------------------------------------------->
</div>

	<br>
	
<!-- include ํŽ˜์ด์ง€ : Copyright -->
<div>
	<!-- ์•ก์…˜ํƒœ๊ทธ -->
	<jsp:include page="/inc/copyright.jsp"></jsp:include>
</div>
<!-------- include ํŽ˜์ด์ง€ ๋ ------->

</body>
</html>
  • ์ €๋ฒˆ ๋‹ค์ด์–ด๋ฆฌ ํ”„๋กœ์ ํŠธ๋Š” ์„ธ์…˜๊ธฐ๋Šฅ์„ ์ด์šฉํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฒŒ์‹œ๊ธ€ ์ž‘์„ฑ ์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜๋„๋ก ํ•˜๊ณ , ์ˆ˜์ •/์‚ญ์ œ ์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ ํผ์„ ๋งŒ๋“ค์–ด ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์œผ๋ฉด ์ˆ˜์ •/์‚ญ์ œ๊ฐ€ ๋˜์ง€ ์•Š๋„๋ก ํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋ฒˆ ํ”„๋กœ์ ํŠธ๋Š” ์„ธ์…˜๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ ํผ์„ ๋งŒ๋“ค ํ•„์š”๊ฐ€ ์—†๋‹ค. ๋˜ํ•œ, action ํŽ˜์ด์ง€ ์ž‘์„ฑ ์‹œ์—๋„ ์ฟผ๋ฆฌ์— where์ ˆ์„ ์ž‘์„ฑํ•  ํ•„์š”๊ฐ€ ์—†์„ ๊ฒƒ์ด๋‹ค.
  • ๋”ฐ๋ผ์„œ ๋Œ“๊ธ€ ์ž…๋ ฅ ์‹œ ํ˜„์žฌ ๋กœ๊ทธ์ธ ์‚ฌ์šฉ์ž์˜ ์•„์ด๋””(session.getAttribute("loginMemberId"))์˜ ๊ฐ’์„ ๋„˜๊ธด๋‹ค.



๐Ÿ“Œ ๊ฒฐ๊ณผ


๋กœ๊ทธ์ธ ์•ˆํ–ˆ์„ ๋•Œ (์„ธ์…˜๊ฐ’ X) ํ•ด๋‹น boardNo์˜ ๋Œ“๊ธ€ ๋ฆฌ์ŠคํŠธ ์ถœ๋ ฅ (๊ฐ€์žฅ ์ตœ๊ทผ์— ์ž‘์„ฑ๋œ ๋Œ“๊ธ€์ด ์ƒ๋‹จ์œผ๋กœ ์˜ฌ๋ผ์˜ค๋„๋ก ์ •๋ ฌ), ๋Œ“๊ธ€ 10๊ฐœ ์ดํ•˜์‹œ์—๋Š” ํŽ˜์ด์ง• ๋ฐœ์ƒX, ๋Œ“๊ธ€ ๋ฆฌ์ŠคํŠธ ์ฒซํŽ˜์ด์ง€์—์„  ์ด์ „์ด ์ถœ๋ ฅ๋˜์ง€ ์•Š์Œ


๋กœ๊ทธ์ธ ์‹œ (์„ธ์…˜๊ฐ’ O) ๋Œ“๊ธ€ ์ž…๋ ฅ ํผ ์ถœ๋ ฅ


๋Œ“๊ธ€์ด ์กด์žฌํ•˜์ง€ ์•Š์„ ์‹œ ๋ฉ”์„ธ์ง€ ์ถœ๋ ฅ


๋Œ“๊ธ€ 10๊ฐœ ์ดˆ๊ณผ ์ถœ๋ ฅ ์‹œ ํŽ˜์ด์ง• ๋ฐœ์ƒO, ํ•ด๋‹น boardNo์˜ ๋Œ“๊ธ€ ๋ฆฌ์ŠคํŠธ ๋งˆ์ง€๋ง‰ ํŽ˜์ด์ง€์—์„  ๋‹ค์Œ์ด ์ถœ๋ ฅ๋˜์ง€ ์•Š์Œ


๐Ÿ’ก insertCommentAction.jsp

๋Œ“๊ธ€ ์ž…๋ ฅ(insert) ์‹œ actionํŽ˜์ด์ง€๋ฅผ ์ง์ ‘ ์ž‘์„ฑํ•ด๋ณด๋Š” ๊ณผ์ œ ์‹œ๊ฐ„์„ ๊ฐ€์กŒ๋‹ค.

๐Ÿ“ ์ž‘์„ฑ

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import = "java.net.*"%>
<%@ page import = "vo.*" %>
<%@ page import = "java.sql.*" %>
<%
	//์ฝ˜์†”์ฐฝ ์ถœ๋ ฅ ์ƒ‰์ƒ ์ง€์ •
	final String RED = "\u001B[31m";
	final String BG_RED = "\u001B[41m";
	final String GREEN = "\u001B[32m";
	final String BG_GREEN = "\u001B[42m";
	final String RESET = "\u001B[0m";
	
	// ์ธ์ฝ”๋”ฉ
	request.setCharacterEncoding("utf-8");

	// 1) ์š”์ฒญ๊ฐ’ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ
	// boardNo, memberId, commentContent
	System.out.println(RED + request.getParameter("boardNo") + " <- insertCommentAction param boardNo");
	System.out.println(request.getParameter("memberId") + " <- insertCommentAction param memberId");
	System.out.println(request.getParameter("commentContent") + " <- insertCommentAction param commentContent" + RESET);
	
	// boardNo, memberId
	if(request.getParameter("boardNo") == null
			|| request.getParameter("boardNo").equals("")
			|| request.getParameter("memberId") == null
			|| request.getParameter("memberId").equals("")) {
		response.sendRedirect(request.getContextPath() + "/home.jsp");
		return;
	}
	// null์ด๊ฑฐ๋‚˜ ๊ณต๋ฐฑ์ด ์•„๋‹ˆ๋ฉด ๊ฐ’ ๋ณ€์ˆ˜์— ๋ฐ›๊ธฐ
	int boardNo = Integer.parseInt(request.getParameter("boardNo"));
	String memberId = request.getParameter("memberId");
	
	// commentContent
	if(request.getParameter("commentContent") == null
			|| request.getParameter("commentContent").equals("")) {
		String msg = URLEncoder.encode("๋Œ“๊ธ€ ๋‚ด์šฉ์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”!", "utf-8");
		response.sendRedirect(request.getContextPath() + "/board/boardOne.jsp?boardNo=" + boardNo + "&msg=" + msg);
		return;
	}
	// null์ด๊ฑฐ๋‚˜ ๊ณต๋ฐฑ์ด ์•„๋‹ˆ๋ฉด ๊ฐ’ ๋ณ€์ˆ˜์— ๋ฐ›๊ธฐ
	String commentContent = request.getParameter("commentContent");
	
	// ๋””๋ฒ„๊น…
	System.out.println(GREEN + boardNo + " <- insertCommentAction boardNo");
	System.out.println(memberId + " <- insertCommentAction memberId");
	System.out.println(commentContent + " <- insertCommentAction commentContent" + RESET);
	
	// ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ’ ํด๋ž˜์Šค์— ์ €์žฅ
	Comment paramComment = new Comment();
	paramComment.boardNo = boardNo;
	paramComment.memberId = memberId; 
	paramComment.commentContent = commentContent; 
	
	// 2) insert
	// ๋“œ๋ผ์ด๋ฒ„ ๋กœ๋”ฉ ๋ฐ db ์ ‘์†
	String driver = "org.mariadb.jdbc.Driver";
	String dburl = "jdbc:mariadb://127.0.0.1:3306/userboard";
	String dbuser = "****";
	String dbpw = "****";
	Class.forName(driver);
	Connection conn = DriverManager.getConnection(dburl, dbuser, dbpw);
	
	// ์ฟผ๋ฆฌ ์ž‘์„ฑ
	// INSERT INTO comment(board_no, comment_content, member_id, createdate, updatedate) VALUES(?, ?, ?, NOW(), NOW())
	String sql = "INSERT INTO comment(board_no, comment_content, member_id, createdate, updatedate) VALUES(?, ?, ?, NOW(), NOW())";
	PreparedStatement stmt = conn.prepareStatement(sql);
	stmt.setInt(1, paramComment.boardNo);
	stmt.setString(2, paramComment.commentContent);
	stmt.setString(3, paramComment.memberId);
	System.out.println(BG_GREEN + stmt + " <- insertCommentAction stmt");
	
	// ์ฟผ๋ฆฌ๊ฐ€ ์ž˜ ์‹คํ–‰๋˜์—ˆ๋Š”์ง€ ํ™•์ธ
	// ํ•ด๋‹น boardNo์˜ boardOne ํŽ˜์ด์ง€๋กœ ๋ฆฌ๋‹ค์ด๋ ‰์…˜
	int row = stmt.executeUpdate(); // 1์ด๋ฉด 1ํ–‰ ์„ฑ๊ณต
	if(row == 1) {
		System.out.println("insertCommentAction ๋Œ“๊ธ€ ์ž…๋ ฅ ์„ฑ๊ณต" + RESET);
		response.sendRedirect(request.getContextPath() + "/board/boardOne.jsp?boardNo=" + boardNo);
		return;
	} else {
		System.out.println(BG_RED + "insertCommentAction ๋Œ“๊ธ€ ์ž…๋ ฅ ์‹คํŒจ" + RESET);
		String msg = URLEncoder.encode("๋Œ“๊ธ€์ด ๋“ฑ๋ก๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค", "utf-8");
		response.sendRedirect(request.getContextPath() + "/board/boardOne.jsp?boardNo=" + boardNo + "&msg=" + msg);
		return;
	}
%>
  • ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ์˜ ๊ฐœ๋…์€ ์•„์ง๋„ ์กฐ๊ธˆ ์•„๋ฆฌ์†กํ•œ ๊ฒƒ ๊ฐ™๋‹ค. ์–ด๋–ค ๊ฐ’์„ ๊ฒ€์‚ฌํ•ด์•ผํ•˜๊ณ , ์–ด๋””๋กœ ๋ฆฌ๋‹ค์ด๋ ‰์…˜ ํ• ์ง€ ๊ตฌ์ƒ์„ ์ž˜ ํ•ด์•ผํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋„˜์–ด์˜ค๋Š” ๊ฐ’์ด ๋ฌด์—‡์ด๊ณ  ๋„˜์–ด๊ฐ€์•ผ๋  ๊ฐ’์ด ๋ฌด์—‡์ธ์ง€ ์ž˜ ํŒŒ์•…ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•  ๊ฒƒ์ด๋‹ค.

  • ๊ถ๊ธˆํ•œ ์ 

    • insert์‹œ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ’์„ ๊ฐ์ฒด(class)์— ๋ฐ˜๋“œ์‹œ ์ €์žฅํ•ด์•ผ ํ•˜๋Š”์ง€?



๐Ÿ“ ๊ฒฐ๊ณผ

๋Œ“๊ธ€(commentContent๊ฐ’) null ๋˜๋Š” ๊ณต๋ฐฑ์ธ ์ƒํƒœ๋กœ ๋Œ“๊ธ€์ž…๋ ฅ์„ ์‹œ๋„ํ•˜๋ฉด msg ์ถœ๋ ฅ



profile
ํ•˜๋ฃจ ํ•œ๊ฑธ์Œ์”ฉ๐Ÿ’ช ์ดˆ๋ณด ๊ฐœ๋ฐœ์ž ๋„์ „๊ธฐ ๐ŸŒฑ๐Ÿ’ป

0๊ฐœ์˜ ๋Œ“๊ธ€