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 |
๋๊ธ