๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Data Base/MySQL

[MySQL] DB ์ •๊ทœํ™”

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 9.
๋ฐ˜์‘ํ˜•

Q) ๋‹ค์Œ ํ…Œ์ด๋ธ”์€ ์–ด๋–ค ์ •๊ทœํ™”๋ฅผ ์œ„๋ฐ˜ํ•˜์˜€๋Š”๊ฐ€ ?

์ฃผ๋ฌธ๋ฒˆํ˜ธ

์ด๋ฆ„

์ฃผ์†Œ

์ „ํ™”๋ฒˆํ˜ธ

์ƒํ’ˆ1

๊ฐฏ์ˆ˜

๊ฐ€๊ฒฉ

์ƒํ’ˆ2

๊ฐฏ์ˆ˜

๊ฐ€๊ฒฉ

M20200801

ํ™๊ธธ๋™

๋Œ€๊ตฌ

010-1111-1111

๋งˆ์Šคํฌ

10

500

๋ณผํŽœ

30

300

M20200802

์ด์ˆœ์‹ 

์„œ์šธ

010-2222-2222

๋…ธํŠธ๋ถ

1

2000000

๋งˆ์šฐ์Šค

1

15000

M20200803

ํ™๊ธธ๋™

๋Œ€๊ตฌ

010-1111-1111

์ฒญ๋ฐ”์ง€

2

100000

โ€‹

โ€‹

โ€‹

M20200804

๊น€์œ ์‹ 

๋ถ€์‚ฐ

010-3333-3333

ํ–ฅ์ˆ˜

2

50000

์‹œ๊ณ„

1

70000

โ€‹

โ€‹

A) ์ œ 1 ์ •๊ทœํ™” ์œ„๋ฐ˜

์ด๋ ‡๊ฒŒ ํ…Œ์ด๋ธ”์„ ๋‚˜๋ˆ ์•ผํ•œ๋‹ค

 

โ€‹

create table order_tb (order_no varchar(9), name varchar(10), addr varchar(45), phone varchar(13), primary key(order_no));

โ€‹

create table order_detail (no int, order_no varchar(9), product varchar(20), qty int, price int, primary key (no), constraint FK_order_no foreign key (order_no) references order_tb (order_no));

 

โ€‹

โ€‹

โ–ถ MySQL Workbench๋กœ ๋งŒ๋“ค์–ด๋ณด๊ธฐ

โ€‹

โ€‹โ€‹

โ€‹

โ–ถ JSP ํŒŒ์ผ๋กœ ์ž‘์„ฑํ•˜๊ธฐ

โ€‹

โ€‹

inputForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<script>
	var oTbl;

	function insRow() {  // Row ์ถ”๊ฐ€
		oTbl = document.getElementById("addTable");
		var oRow = oTbl.insertRow();
		oRow.onmouseover = function() {
			oTbl.clickedRowIndex = this.rowIndex
		};  // clickedRowIndex - ํด๋ฆญํ•œ Row์˜ ์œ„์น˜๋ฅผ ํ™•์ธ;
		var oCell = oRow.insertCell();
		
		// ์‚ฝ์ž…๋  Form Tag
		var frmTag = "<input type='text' name='product'>";
		oCell.innerHTML = frmTag;
		oCell = oRow.insertCell();
		
		// ์‚ฝ์ž…๋  Form Tag
		frmTag = "<input type='text' name='qty'>";
		oCell.innerHTML = frmTag;
		oCell = oRow.insertCell();
		
		// ์‚ฝ์ž…๋  Form Tag
		frmTag = "<input type='text' name='price'>";
		frmTag += "<input type=button value='์‚ญ์ œ' onClick='removeRow()' style='cursor:hand'>";
		oCell.innerHTML = frmTag;
	}

	function removeRow() {  // Row ์‚ญ์ œ
		oTbl.deleteRow(oTbl.clickedRowIndex);
	}
</script>
<body>
	<form name="f" action="inputReg.jsp" method="post">
		์ƒํ’ˆ์ฃผ๋ฌธ ๊ธฐ๋ณธ
		<table border="1">
			<tr>
				<td>์ฃผ๋ฌธ๋ฒˆํ˜ธ</td>
				<td><input type="text" name="order_no" /></td>
			</tr>
			<tr>
				<td>์ด๋ฆ„</td>
				<td><input type="text" name="name" /></td>
			</tr>
			<tr>
				<td>์ฃผ์†Œ</td>
				<td><input type="text" name="addr" /></td>
			</tr>
			<tr>
				<td>์ „ํ™”๋ฒˆํ˜ธ</td>
				<td><input type="text" name="phone" /></td>
			</tr>
		</table>
		<br> ์ƒํ’ˆ์ฃผ๋ฌธ ์ƒ์„ธ
		<table border="1" id="addTable">
			<tr>
				<td>์ƒํ’ˆ</td>
				<td>๊ฐœ์ˆ˜</td>
				<td>๊ฐ€๊ฒฉ</td>
			</tr>
			<tr>
				<td colspan="3"><input name="addButton" type="button"
					style="cursor: hand" onClick="insRow()" value="์ž…๋ ฅ์ฐฝ ์ถ”๊ฐ€"></td>
			</tr>
			<tr>
				<td><input type="text" name="product"></td>
				<td><input type="text" name="qty"></td>
				<td><input type="text" name="price"></td>
			</tr>
		</table>
		<table>
			<tr>
				<td><input type="submit" value="๋“ฑ๋ก" />
				    <input type="button" value="์กฐํšŒ" onClick="location.href='output.jsp'"/></td>
			</tr>
		</table>
	</form>
</body>
</html>

 

 

inputReg.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="db.JdbcUtil"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
	request.setCharacterEncoding("UTF-8");
	String order_no = request.getParameter("order_no");
	String name = request.getParameter("name");
	String phone = request.getParameter("phone");
	String addr = request.getParameter("addr");
	String[] product = request.getParameterValues("product");
	String[] qty = request.getParameterValues("qty");
	String[] price = request.getParameterValues("price");
	
	Connection con = null;
	PreparedStatement pstmt = null;
	ResultSet rs = null;
	
	try {
		con = JdbcUtil.getConnection();
		con.setAutoCommit(false);
		String sql_m = "insert into order_tb values (?,?,?,?)";
		String sql_s = "insert into order_detail values (?,?,?,?,?)";
		pstmt = con.prepareStatement(sql_m);
		pstmt.setString(1, order_no);
		pstmt.setString(2, name);
		pstmt.setString(3, addr);
		pstmt.setString(4, phone);
		int result = pstmt.executeUpdate();
		
		if(result > 0) {
			for(int i = 0; i < product.length; i++) {
				int num = 1;
				pstmt = con.prepareStatement("select max(no) from order_detail");
				rs = pstmt.executeQuery();
		
				if(rs.next()) 
					num = rs.getInt(1) + 1;

					pstmt = con.prepareStatement(sql_s);
					pstmt.setInt(1, num);
					pstmt.setString(2, order_no);
					pstmt.setString(3, product[i]);
					pstmt.setString(4, qty[i]);
					pstmt.setString(5, price[i]);
		
					pstmt.executeUpdate();
				}
				con.commit();
				out.println("<script>");
				out.println("alert('๋ฐ์ดํ„ฐ ์ž…๋ ฅ ์™„๋ฃŒ');");
				out.println("location.href='inputForm.jsp'");
				out.println("</script>");
			} else {
				con.rollback();
				out.println("<script>");
				out.println("alert('๋ฐ์ดํ„ฐ๊ฐ€ ์ž…๋ ฅ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค');");
				out.println("history.back();");
				out.println("<script>");
		}
		
	} catch(Exception e) {
		if(con != null)
			con.rollback();
		out.println("<script>");
		out.println("alert('๋ฐ์ดํ„ฐ ์ž…๋ ฅ ์˜ค๋ฅ˜');");
		out.println("history.back();");
		out.println("</script>");
		
	} finally {
		if(rs != null)
		rs.close();
		if(pstmt != null)
		pstmt.close();
		if(con != null)
		con.close();
		
	}
%>
</body>
</html>

 

 

output.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="db.JdbcUtil"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
	Connection con = null;
	PreparedStatement pstmt = null;
	ResultSet rs = null;
	
	try {
		con = JdbcUtil.getConnection();
		String sql="select o.order_no, name, addr, phone, product, qty, price "
					+ "from order_tb o left join order_detail d on o.order_no=d.order_no "
					+ "order by order_no";
		pstmt=con.prepareStatement(sql);
		rs = pstmt.executeQuery();
		
		if(rs.next()) {
%>

	<table border="1">
		<tr>
			<td>์ฃผ๋ฌธ๋ฒˆํ˜ธ</td>
			<td>์ด๋ฆ„</td>
			<td>์ฃผ์†Œ</td>
			<td>์ „ํ™”๋ฒˆํ˜ธ</td>
			<td>์ƒํ’ˆ</td>
			<td>๊ฐฏ์ˆ˜</td>
			<td>๊ฐ€๊ฒฉ</td>
		</tr>

<%	String orderNoTmp="";  
	do {
		if(!orderNoTmp.equals(rs.getString(1))) { %>
		
		<tr>
			<td><%=rs.getString(1) %></td>
			<td><%=rs.getString(2) %></td>
			<td><%=rs.getString(3) %></td>
			<td><%=rs.getString(4) %></td>
			<td><%=rs.getString(5) %></td>
			<td><%=rs.getString(6) %></td>
			<td><%=rs.getString(7) %></td>
		</tr>
		
<%		orderNoTmp = rs.getString(1);

		} else { %>
		
		<tr>
			<td colspan="4"></td>
			<td><%=rs.getString(5) %></td>
			<td><%=rs.getString(6) %></td>
			<td><%=rs.getString(7) %></td>
		</tr>
		
<% 		}
		
	} while(rs.next());
%>
	
	</table>
			
<% 		} else {
			out.println("๋ถˆ๋Ÿฌ์˜ฌ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.");
		}
				
	} catch(Exception e) {
		e.printStackTrace();
		out.println("<script>");
		out.println("alert('๋ฐ์ดํ„ฐ ์ถœ๋ ฅ ์˜ค๋ฅ˜');");
		out.println("history.back();");
		out.println("</script>");
		
	} finally {
		if(rs != null)
			rs.close();
			if(pstmt != null)
			pstmt.close();
			if(con != null)
			con.close();
	}
%>
</body>
</html>

 

 

<์‹คํ–‰ ๊ฒฐ๊ณผ>

 

-'๋“ฑ๋ก' ๋ฒ„ํŠผ

โ€‹

 

-'์กฐํšŒ'๋ฒ„ํŠผ

 

โ˜…์ฃผ์˜โ˜…

DB์—์„œ ์‚ญ์ œํ•˜๊ณ ์‹ถ์œผ๋ฉด order_detail ํ…Œ์ด๋ธ”์˜ ๋‚ด์šฉ๋ถ€ํ„ฐ ์‚ญ์ œํ•œ ํ›„ order_tb ํ…Œ์ด๋ธ”์˜ ๋‚ด์šฉ์„ ์‚ญ์ œํ•ด์•ผํ•œ๋‹ค.

โ€‹

โ€‹

โ€‹

โ–ถ ํ”„๋กœ์‹œ์ € ์‚ฌ์šฉ

โ€‹โ€‹

1. employee ํ…Œ์ด๋ธ” ์ƒ์„ฑ

โ€‹โ€‹

โ€‹

2. ํ”„๋กœ์‹œ์ € ์ƒ์„ฑ

-delimiter ์„ ๊ผญ ๋„ฃ์–ด์ค˜์•ผํ•œ๋‹ค!

โ€‹โ€‹

โ€‹

3. ํ”„๋กœ์‹œ์ €๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์ „์— ํ˜„์žฌ employee ํ…Œ์ด๋ธ”์— ์‚ฝ์ž…๋˜์–ด์žˆ๋Š” ๋ ˆ์ฝ”๋“œ๋“ค์„ ํ™•์ธํ•œ๋‹ค.

โ€‹

โ€‹

4. ํ”„๋กœ์‹œ์ € ์‹คํ–‰

 

๋ฐ˜์‘ํ˜•

'Data Base > MySQL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

MySQL ๋ฌธ์ œ  (0) 2021.02.09
MySQL 25๋ฒˆ์งธ์ˆ˜์—…  (0) 2021.02.09
MySQL 24๋ฒˆ์งธ์ˆ˜์—…  (0) 2021.02.09
MySQL 23๋ฒˆ์งธ์ˆ˜์—…  (0) 2021.02.09
MySQL 22๋ฒˆ์งธ์ˆ˜์—…  (0) 2021.02.09

๋Œ“๊ธ€