๊ตฌํ ์์ ( ๋ฐ์ดํฐ๋ฒ ์ด์ค ๐๐ป ์๋ฒ ๐๐ป ํ๋ก ํธ ๐๐ป ๋ฐฑ์๋ )
1) [์ ๋ณด์ฒ๋ฆฌ์ฐ์ ๊ธฐ์ฌ ๊ณผ์ ํ๊ฐํ] ์ค๊ธฐ์ํ ๋ฌธ์ - ์ง์ญ๊ตฌ์์ํฌํ
2) [์ ๋ณด์ฒ๋ฆฌ์ฐ์ ๊ธฐ์ฌ ๊ณผ์ ํ๊ฐํ] ์ค๊ธฐ MVC ํจํด ๊ตฌ์กฐ / ํ๋ฆ
3) [์ ๋ณด์ฒ๋ฆฌ์ฐ์
๊ธฐ์ฌ ๊ณผ์ ํ๊ฐํ] ์ง์ญ๊ตฌ์์ํฌํ - DB ์์ฑํ๊ธฐ
4) [์ ๋ณด์ฒ๋ฆฌ์ฐ์
๊ธฐ์ฌ ๊ณผ์ ํ๊ฐํ] ์ง์ญ๊ตฌ์์ํฌํ - Java / Servlet ์์ฑ
5) [์ ๋ณด์ฒ๋ฆฌ์ฐ์
๊ธฐ์ฌ ๊ณผ์ ํ๊ฐํ] ์ง์ญ๊ตฌ์์ํฌํ - View ํ๋ฉด ๋ง๋ค๊ธฐ
6) [์ ๋ณด์ฒ๋ฆฌ์ฐ์
๊ธฐ์ฌ ๊ณผ์ ํ๊ฐํ] ์ง์ญ๊ตฌ์์ํฌํ - ๋ฐ์ดํฐ ์
/์ถ๋ ฅ
โจ๋ฐ์ดํฐ ์ ๋ ฅ ( INSERT )
ํฌํํ๊ธฐ ํ๋ฉด์์ ๋์ด์จ ๋ฐ์ดํฐ๋ฅผ DB ์ ์ ์ฅํ๋๋ก ํ๊ฒ ์ต๋๋ค.
ํฌํํ๊ธฐ ํ๋ฉด์์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฅํ ํฌํํ๊ธฐ๋ฒํผ์ ํด๋ฆญํ๋ฉด script.js ์์ ์ ํจ์ฑ๊ฒ์ฌ๋ฅผ ๋ง์น๋ค,
formํ๊ทธ์ action ๊ฒฝ๋ก vote.do ๋ก ์ด๋ํฉ๋๋ค.
1. VoteController.java (VoteDAO.java ํด๋์ค์ insertVote ๋ฉ์๋๋ก ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ง๊ณ ์ด๋)
case "/vote.do" :
int result = vote.insertVote(request, response);
break;
}
2. VoteDAO.java ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ DB์ ์ ๋ ฅ/์ ์ฅ ํฉ๋๋ค. (์คํจ์ 0, ์ฑ๊ณต์ 1 ๋ฐํ)
public int insertVote(HttpServletRequest request, HttpServletResponse response) {
int result = 0;
try {
conn = getConnection();
String v_jumin = request.getParameter("v_jumin");
String v_name = request.getParameter("v_name");
String m_no = request.getParameter("m_no");
String v_time = request.getParameter("v_time");
String v_area = request.getParameter("v_area");
String v_confirm = request.getParameter("v_confirm");
String sql = "INSERT INTO tbl_vote_202005 VALUES(?,?,?,?,?,?)";
ps = conn.prepareStatement(sql);
ps.setString(1, v_jumin);
ps.setString(2, v_name);
ps.setString(3, m_no);
ps.setString(4, v_time);
ps.setString(5, v_area);
ps.setString(6, v_confirm);
result = ps.executeUpdate(); // 0์คํจ, 1์ฑ๊ณต
System.out.println(result);
conn.close();
ps.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
3. VoteController.java ๋ก ๋์์์ ์๋ฆผ์ฐฝ์ ํ๋ฉด์ ์ถ๋ ฅํด์ค๋ค ํ์ธ ๋ฒํผ์ ๋๋ฅด๋ฉด ๋ฉ์ธํ๋ฉด์ผ๋ก ์ด๋ํฉ๋๋ค.
*context (๋ฉ์ธํ๋ฉด์ผ๋ก ์ด๋ํ๊ธฐ ์ํ ๊ฒฝ๋ก)
case "/vote.do" :
int result = vote.insertVote(request, response);
response.setContentType("text/html; charset=UTF-8");
PrintWriter out=response.getWriter();
if(result == 1) {
out.println("<script>");
out.println("alert('ํฌํํ๊ธฐ ์ ๋ณด๊ฐ ์ ์์ ์ผ๋ก ๋ฑ๋ก ๋์์ต๋๋ค!'); location.href='"+context+"'; ");
out.println("</script>");
out.flush();
}else {
out.println("<script>");
out.println("alert('๋ฑ๋ก์คํจ!'); location.href='"+context+"'; ");
out.println("</script>");
out.flush();
}
break;
}
โจ๋ฐ์ดํฐ ์ถ๋ ฅ( SELECT )
ํ๋ณด์กฐํ, ํฌํ๊ฒ์์กฐํ, ํ๋ณด์๋ฑ์ ์ ๋๊ฒจ์ค ๋ฐ์ดํฐ๋ฅผ DB ์์ ๋ถ๋ฌ์ค๋๋ก ํ๊ฒ ์ต๋๋ค.
ํ๋ณด์กฐํ
1. VoteController.java ( VoteDAO.java ํด๋์ค์ selectMember() ๋ฉ์๋๋ก ์ด๋)
case "/memberList.do" :
site = vote.selectMember(request, response);
break;
2. VoteDAO.java DB์์ ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์จ๋ค list ์ ๋ด์์ค๋๋ค.
*DECODE (if ๋ฌธ๊ณผ ๊ฐ๋ค), *|| (์ค๋ผํด์์ || ์ด ๊ธฐํธ๋ ๋ฌธ์์ด์ ๋ถ์ฌ์ค๋ค. CONCAT ๊ณผ ๊ฐ์), *substr(ํด๋น ์์น์ ๋ฌธ์๋ฅผ ๊ฐ์ ธ์จ๋ค.)
public String selectMember(HttpServletRequest request, HttpServletResponse response) {
ArrayList<Member> list = new ArrayList<Member>();
try {
conn = getConnection();
//ํ๋ณด ์กฐํ ํ๋ฉด ์ฟผ๋ฆฌ
String sql = "SELECT ";
sql+= " M.m_no, ";
sql+= " M.m_name, ";
sql+= " P.p_name, ";
sql+= " DECODE(M.p_school,'1','๊ณ ์กธ','2','ํ์ฌ','3','์์ฌ','๋ฐ์ฌ') p_school, ";
sql+= " substr(M.m_jumin,1,6)|| ";
sql+= " '-'||substr(M.m_jumin,7) m_jumin, ";
sql+= " M.m_city, ";
sql+= " substr(P.p_tel1,1,2)||'-'||P.p_tel2||'-'||";
sql+= " (substr(P.p_tel3,4)||";
sql+= " substr(P.p_tel3,4)||";
sql+= " substr(P.p_tel3,4)||";
sql+= " substr(P.p_tel3,4)) p_tel ";
sql+= " FROM tbl_member_202005 M, tbl_party_202005 P ";
sql+= " WHERE M.p_code = P.p_code";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()) {
Member member = new Member();
member.setM_no(rs.getString(1));
member.setM_name(rs.getString(2));
member.setP_name(rs.getString(3));
member.setP_school(rs.getString(4));
member.setM_jumin(rs.getString(5));
member.setM_city(rs.getString(6));
member.setP_tel(rs.getString(7));
list.add(member);
}
request.setAttribute("list",list);
conn.close();
ps.close();
rs.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "memberList.jsp";
}
3. memberList.jsp Viewํ๋ฉด์ ๋ถ๋ฌ์จ ๋ฐ์ดํฐ๋ฅผ ์ถ๋ ฅํด์ค๋๋ค.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="VoteBean.Member" %>
<%
request.setCharacterEncoding("UTF-8");
//list ๋ถ๋ฌ์ค๊ธฐ
ArrayList<Member> list = new ArrayList<Member>();
list = (ArrayList<Member>)request.getAttribute("list");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>memberList</title>
</head>
<body>
<%@ include file="topmenu.jsp" %>
<section>
<h2>โ ํ๋ณด์กฐํ โ</h2>
<div class="table">
<table style="width:700px;">
<tr>
<th>ํ๋ณด๋ฒํธ</th>
<th>์ฑ๋ช
</th>
<th>์์์ ๋น</th>
<th>ํ๋ ฅ</th>
<th>์ฃผ๋ฏผ๋ฒํธ</th>
<th>์ง์ญ๊ตฌ</th>
<th>๋ํ์ ํ</th>
</tr>
<%
for(int i=0; i<list.size(); i++){
%>
<tr>
<td><%=list.get(i).getM_no()%></td>
<td><%=list.get(i).getM_name()%></td>
<td><%=list.get(i).getP_name()%></td>
<td><%=list.get(i).getP_school()%></td>
<td><%=list.get(i).getM_jumin()%></td>
<td><%=list.get(i).getM_city()%></td>
<td><%=list.get(i).getP_tel()%></td>
</tr>
<%
}
%>
</table>
</div>
</section>
<%@ include file="footer.jsp" %>
</body>
</html>
ํฌํ๊ฒ์์กฐํ
1. VoteController.java ( VoteDAO.java ํด๋์ค์ selectAll() ๋ฉ์๋๋ก ์ด๋)
case "/voteList.do" :
site = vote.selectAll(request, response);
break;
2. VoteDAO.java DB์์ ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์จ๋ค list ์ ๋ด์์ค๋๋ค.
public String selectAll(HttpServletRequest request, HttpServletResponse response){
ArrayList<Vote> list = new ArrayList<Vote>();
try {
conn = getConnection();
//ํฌํ๊ฒ์์กฐํ ํ๋ฉด ์ฟผ๋ฆฌ
String sql = "SELECT v_name,";
sql+= " '19'||substr(v_jumin,1,2)||";
sql+= " '๋
'||substr(v_jumin,3,2)||";
sql+= " '์'||substr(v_jumin,5,2)||";
sql+= " '์ผ์' v_jumin,";
sql+= " '๋ง '||(to_number(to_char(sysdate,'yyyy'))";
sql+= " - to_number('19'||substr(v_jumin,1,2)))||'์ธ' v_age,";
sql+= " DECODE(substr(v_jumin,7,1),'1','๋จ','์ฌ') v_gender, ";
sql+= " m_no, ";
sql+= " substr(v_time,1,2)||':'||substr(v_time,3,2) v_time, ";
sql+= " DECODE(v_confirm,'Y','ํ์ธ','๋ฏธํ์ธ') v_confirm ";
sql+= " FROM tbl_vote_202005 ";
sql+= " WHERE v_area='์ 1ํฌํ์ฅ'";
ps = conn.prepareStatement(sql); // ๋ช
๋ น์ด๋ฅผ ๋ณด๋ธ๋ค.
rs = ps.executeQuery();
while(rs.next()) {
Vote vote = new Vote();
vote.setV_name(rs.getString(1));
vote.setV_jumin(rs.getString(2));
vote.setV_age(rs.getString(3));
vote.setV_gender(rs.getString(4));
vote.setM_no(rs.getString(5));
vote.setV_time(rs.getString(6));
vote.setV_confirm(rs.getString(7));
list.add(vote);
}
request.setAttribute("list",list);
conn.close();
ps.close();
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
return "voteList.jsp";
}
3. voteList.jsp Viewํ๋ฉด์ ๋ถ๋ฌ์จ ๋ฐ์ดํฐ๋ฅผ ์ถ๋ ฅํด์ค๋๋ค.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="VoteBean.Vote" %>
<%
request.setCharacterEncoding("UTF-8");
//list ๋ถ๋ฌ์ค๊ธฐ
ArrayList<Vote> list = new ArrayList<Vote>();
list = (ArrayList<Vote>)request.getAttribute("list");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>voteList</title>
</head>
<body>
<%@ include file="topmenu.jsp" %>
<section>
<h2>โ ํฌํ๊ฒ์์กฐํ โ</h2>
<div class="table">
<table style="width:700px;">
<tr>
<th>์ฑ๋ช
</th>
<th>์๋
์์ผ</th>
<th>๋์ด</th>
<th>์ฑ๋ณ</th>
<th>ํ๋ณด๋ฒํธ</th>
<th>ํฌํ์๊ฐ</th>
<th>์ ๊ถ์ํ์ธ</th>
</tr>
<%
for(int i=0; i<list.size(); i++){
%>
<tr>
<td><%=list.get(i).getV_name() %></td>
<td><%=list.get(i).getV_jumin() %></td>
<td><%=list.get(i).getV_age() %></td>
<td><%=list.get(i).getV_gender() %></td>
<td><%=list.get(i).getM_no() %></td>
<td><%=list.get(i).getV_time() %></td>
<td><%=list.get(i).getV_confirm() %></td>
</tr>
<%
}
%>
</table>
</div>
</section>
<%@ include file="footer.jsp" %>
</body>
</html>
ํ๋ณด์๋ฑ์
1. VoteController.java ( VoteDAO.java ํด๋์ค์ selectResult() ๋ฉ์๋๋ก ์ด๋)
case "/voteResult.do" :
site = vote.selectResult(request, response);
break;
2. VoteDAO.java DB์์ ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์จ๋ค list ์ ๋ด์์ค๋๋ค.
* v_total DESC (์ดํฌํ์๊ฐ ๋ง์ ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฌํฉ๋๋ค. DESC: ๋ด๋ฆผ์ฐจ์, ASC: ์ฌ๋ฆผ์ฐจ์, default: ASC)
public String selectResult(HttpServletRequest request, HttpServletResponse response) {
ArrayList<Result> list = new ArrayList<Result>();
try {
conn = getConnection();
//ํ๋ณด์ ๋ฑ์ ํ๋ฉด ์ฟผ๋ฆฌ
String sql = "SELECT ";
sql+= " M.m_no, M.m_name, count(*) AS v_total";
sql+= " FROM tbl_member_202005 M, tbl_vote_202005 V";
sql+= " WHERE M.m_no = V.m_no AND V.v_confirm = 'Y' ";
sql+= " GROUP BY M.m_no, M.m_name";
sql+= " ORDER BY v_total DESC";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()) {
Result result = new Result();
result.setM_no(rs.getString(1));
result.setM_name(rs.getString(2));
result.setV_total(rs.getString(3));
list.add(result);
}
request.setAttribute("list",list);
conn.close();
ps.close();
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
return "voteResult.jsp";
}
3. voteResult.jsp Viewํ๋ฉด์ ๋ถ๋ฌ์จ ๋ฐ์ดํฐ๋ฅผ ์ถ๋ ฅํด์ค๋๋ค.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="VoteBean.Result" %>
<%
request.setCharacterEncoding("UTF-8");
//list ๋ถ๋ฌ์ค๊ธฐ
ArrayList<Result> list = new ArrayList<Result>();
list = (ArrayList<Result>)request.getAttribute("list");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>voteResult</title>
</head>
<body>
<%@ include file="topmenu.jsp" %>
<section>
<h2>โ ํ๋ณด์๋ฑ์ โ</h2>
<div class="table">
<table>
<tr>
<th>ํ๋ณด๋ฒํธ</th>
<th>์ฑ๋ช
</th>
<th>์ดํฌํ๊ฑด์</th>
</tr>
<%
for(int i=0; i<list.size(); i++){
%>
<tr>
<td><%=list.get(i).getM_no()%></td>
<td><%=list.get(i).getM_name()%></td>
<td><%=list.get(i).getV_total() %></td>
</tr>
<%
}
%>
</table>
</div>
</section>
<%@ include file="footer.jsp" %>
</body>
</html>
์์ฑ !
( ๋ฐ์ดํฐ๋ฒ ์ด์ค ๐๐ป ์๋ฒ ๐๐ป ํ๋ก ํธ ๐๐ป ๋ฐฑ์๋ ) MVC ํจํด์ผ๋ก ์์๋๋ก ํ๋ก๊ทธ๋จ์ ๊ตฌํํด๋ณด์์ต๋๋ค :D
๋ณธ์ธ์ ํ์ฌ SQLD ์ํ๋ ํจ๊ป ์ค๋นํ๊ณ ์์ด์ sql์ฟผ๋ฆฌ๋ฌธ์ ์ต๋ํ ํ์ฉํ์ฌ ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์๋๋ฐ์
์ด ์ธ์๋ ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์์ ์ ์ฉํ๋ ๋ง์ ๋ฐฉ๋ฒ์ด ์์ผ๋ ํธํ๋ฐฉ๋ฒ์ผ๋ก ์์ ํ์๋ฉด ์ข์๊ฒ๊ฐ์ต๋๋ค :D
์ ์ฒด ์์คํ์ผ์ ๋ค์๊ธ์ ํฌ์คํ ํ๋๋ก ํ๊ฒ ์ต๋๋ค ๐๊ฐ์ฌํฉ๋๋ค ~!