--첫 화면 jsp 파일
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*, poll.*" %>
<jsp:useBean id="pMgr" class="poll.PollMgr" />
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
body {background-color : #FAED7D;}
a {color: blue;}
div { width : 600px;}
hr { width: 600px;}
</style>
</head>
<body>
<div align="center">
<h1>투표프로그램</h1>
<hr>
<h3>설문폼</h3>
<jsp:include page="pollForm.jsp" />
<hr>
<h4>설문리스트</h4>
<table border="1">
<tr>
<td>번호</td>
<td>제목</td>
<td>시작일~종료일</td>
</tr>
<%
ArrayList<PollListBean> alist = pMgr.getAllList();
int count = alist.size(); //총 리스트의 개수
for(int i=0; i<alist.size(); i++) {
PollListBean plBean = alist.get(i);
//alist에 while문을 통한 객체들을 넣었기 때문에 PollListBean
int num = plBean.getNum();
String question = plBean.getQuestion();
String sdate = plBean.getSdate();
String edate = plBean.getEdate();
out.print("<tr>");
out.print(" <td>"+ count +"</td>");
//글번호를 시퀀스로 하면 삭제했을 때 번호오류 가능성이 있음 그래서 count로
out.print(" <td><a href='pollList.jsp?num=" + num + "'>"+ question +"</a></td>");
//제목을 눌렀을 때 설문폼이 나오는 창이 있어야 해서 a태그
out.print(" <td>"+ sdate + "~" + edate +"</td>");
out.print("</tr>");
count--;
}
%>
<tr>
<td colspan="3" align="right"><a href="pollInsert.jsp">설문작성하기</a></td>
</tr>
</table>
</div>
</body>
</html>
--설문 작성하는 폼의 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>
<body>
<div align="center">
<h1>투표 프로그램</h1>
<hr>
<h4>설문작성</h4>
<form method="post" action="pollInsertProc.jsp">
<table border="1" width=600px>
<tr>
<th>질문</th>
<td colspan="2">q: <input name="question" size="50"></td>
</tr>
<tr>
<th rowspan="7">항목</th>
<%
for(int i=1; i<=4; i++) {
out.print(" <td>" + (i * 2 -1) + ": <input name='item'></td>");
out.print(" <td>" + (i * 2) + ": <input name='item'></td>");
out.print("</tr>");
if(i<4)
out.print("<tr>");
}
%>
<!-- <tr> 위처럼 for문으로 만들 수 있음
<td>3: <input name="item"></td>
<td>4: <input name="item"></td>
</tr>
<tr>
<td>5: <input name="item"></td>
<td>6: <input name="item"></td>
</tr>
<tr>
<td>7: <input name="item"></td>
<td>8: <input name="item"></td>
</tr>
<tr> -->
<td>시작일</td>
<td>
<select name="sdateY">
<option value="2023">2023</option>
<option value="2024">2024</option>
</select>년
<select name="sdateM">
<%
for(int i=1; i<13; i++) {
out.println("<option value='" + i + "'>" + i);
}
%>
</select>월
<select name="sdateD">
<%
for(int i=1; i<32; i++) {
out.println("<option value='" + i + "'>" + i);
}
%>
</select>일
</td>
</tr>
<tr>
<td>종료일</td>
<td>
<select name="edateY">
<option value="2023">2023</option>
<option value="2024">2024</option>
</select>년
<select name="edateM">
<%
for(int i=1; i<13; i++) {
out.println("<option value='" + i + "'>" + i);
}
%>
</select>월
<select name="edateD">
<%
for(int i=1; i<32; i++) {
out.println("<option value='" + i + "'>" + i);
}
%>
</select>일
</td>
</tr>
<tr>
<td>이중답변</td>
<td>
<input type="radio" name="type" value="1" checked>YES
<input type="radio" name="type" value="0">NO
</td>
</tr>
<tr>
<td colspan="3" align="center">
<input type="submit" value="작성하기">
<input type="reset" value="초기화">
<input type="button" value="리스트보기" onclick="location.href='pollList.jsp'">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
--설문 폼에 내용기입하여 설문폼 추가하는 jsp 파일
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("UTF-8"); %>
<jsp:useBean id="pMgr" class="poll.PollMgr" />
<jsp:useBean id="plBean" class="poll.PollListBean"/>
<jsp:setProperty property="*" name="plBean" />
<jsp:useBean id="piBean" class="poll.PollItemBean" />
<jsp:setProperty property="*" name="piBean" />
<%
//sdate와 edate 값 불러오기
String sdate = request.getParameter("sdateY") + "/"
+ request.getParameter("sdateM") + "/"
+ request.getParameter("sdateD");
String edate = request.getParameter("edateY") + "/"
+ request.getParameter("edateM") + "/"
+ request.getParameter("edateD");
plBean.setSdate(sdate); //값 넣어주기
plBean.setEdate(edate);
boolean result = pMgr.insertPoll(plBean, piBean);
String msg = "설문 추가에 실패하였습니다";
String url = "pollInsert.jsp";
if(result) {
msg = "설문이 추가되었습니다";
url = "pollList.jsp";
}
%>
<script type="text/javascript">
alert("<%=msg %>");
location.href="<%=url %>";
</script>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
--오라클 테이블 데이터와 연동하는 java파일
package poll;
import java.sql.*;
import java.util.ArrayList;
public class PollMgr {
private DBConnectionMgr pool;
public PollMgr() {
pool = DBConnectionMgr.getInstance();
}
//메소드 만들어서 데이터 가져오기
public boolean insertPoll(PollListBean plBean, PollItemBean piBean) {
Connection con = null;
PreparedStatement pstmt = null;
boolean flag= false;
try {
con = pool.getConnection();
String sql = "insert into pollList values(SEQ_POLL.NEXTVAL, ?, ?, ?,SYSDATE,?,DEFAULT)";
//질문,시작날짜,끝날짜,작성날짜,이중답변
pstmt = con.prepareStatement(sql);
pstmt.setString(1, plBean.getQuestion()); //첫번째 물음표
pstmt.setString(2, plBean.getSdate()); //두번째 물음표
pstmt.setString(3, plBean.getEdate()); //세번째 물음표
pstmt.setInt(4, plBean.getType()); //네번째 물음표
int result = pstmt.executeUpdate();
int result2 = 0;
if(result == 1) { //insert가 정상적으로 이루어졌다는 뜻
sql = "insert into pollitem values(SEQ_POLL.CURRVAL, ?,?,DEFAULT)";
pstmt = con.prepareStatement(sql);
String item[] = piBean.getItem();
for(int i=0; i<item.length; i++) {
if(item[i] == null || item[i].equals("")) //만약 아이템 값이 없을 때
break;
pstmt.setInt(1, i);
pstmt.setString(2, item[i]);
result2 = pstmt.executeUpdate();
}//for end
if(result2 == 1) { //잘 입력됐으면 1
flag = true;
}//if end
}
} catch (Exception e) {
e.printStackTrace();
} finally {
pool.freeConnection(con, pstmt);
}
return flag;
}//메서드 끝
public ArrayList<PollListBean> getAllList() {
Connection con = null;
PreparedStatement pstmt = null;
boolean flag= false;
ResultSet rs = null;
ArrayList<PollListBean> alist = new ArrayList<PollListBean>();
try {
con = pool.getConnection();
String sql = "SELECT * FROM POLLLIST ORDER BY NUM DESC";
//시퀀스니까 가장 최근일수록 숫자가 가장 커서 내림차순
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()) {
PollListBean plBean = new PollListBean();
plBean.setNum(rs.getInt("num"));
plBean.setQuestion(rs.getString("question"));
plBean.setSdate(rs.getString("sdate"));
plBean.setEdate(rs.getString("edate"));
//필요한 것들 가져오기
alist.add(plBean); // 값 넣어주기
}
} catch(Exception e) {
e.printStackTrace();
} finally {
pool.freeConnection(con, pstmt, rs);
}
return alist;
} //목록 보여주는 메서드 끝
public PollListBean getList(int num) {
Connection con = null;
PreparedStatement pstmt = null;
boolean flag= false;
ResultSet rs = null;
PollListBean plBean = new PollListBean();
try {
con = pool.getConnection();
String sql = "SELECT * FROM POLLLIST WHERE NUM=" + num;
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
if(rs.next()) { //값이 없을 수도 있으니 if문
plBean.setQuestion(rs.getString("question"));
plBean.setType(rs.getInt("type"));
plBean.setActive(rs.getInt("active")); //종료날짜에 따라 활성화 여부
}
} catch(Exception e) {
e.printStackTrace();
} finally {
pool.freeConnection(con, pstmt, rs);
}
return plBean;
}
//설문폼에 라디오로 해줄 아이템 프로그래밍
public ArrayList<String> getItem(int num) { //한 행당 값이 여러개여서 ArrayList
Connection con = null;
PreparedStatement pstmt = null;
boolean flag= false;
ResultSet rs = null;
ArrayList<String> alist = new ArrayList<String>();
try {
con = pool.getConnection();
String sql = "SELECT item FROM POLLITEM WHERE LISTNUM=" + num;
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()) {
alist.add(rs.getString(1));
}
} catch(Exception e) {
e.printStackTrace();
} finally {
pool.freeConnection(con, pstmt, rs);
}
return alist;
}
}
--설문폼의 선택항목을 제외한 다른 항목들의 javaBean파일
package poll;
public class PollListBean {
private int num; //테이블이 두개니까 하나로 합쳐야되기 때문에 테이블을 구분하려는 용도
private String question;
private String sdate;
private String edate;
private String wdate; //설문이 입력된 날짜
private int type; //이중답변 가능(checkbox)/불가(radio)
private int active; //설문 종료일이 초과되면 폼 응답 불가한 기능
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getSdate() {
return sdate;
}
public void setSdate(String sdate) {
this.sdate = sdate;
}
public String getEdate() {
return edate;
}
public void setEdate(String edate) {
this.edate = edate;
}
public String getWdate() {
return wdate;
}
public void setWdate(String wdate) {
this.wdate = wdate;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public int getActive() {
return active;
}
public void setActive(int active) {
this.active = active;
}
}
--설문폼의 선택사항 아이템들의 javaBean파일
package poll;
public class PollItemBean {
private int listnum; //설문번호
private int itemnum; //아이템 번호
private String[] item; //아이템 내용
private int count; //투표수
public int getListnum() {
return listnum;
}
public void setListnum(int listnum) {
this.listnum = listnum;
}
public int getItemnum() {
return itemnum;
}
public void setItemnum(int itemnum) {
this.itemnum = itemnum;
}
public String[] getItem() {
return item;
}
public void setItem(String[] item) {
this.item = item;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
--num값이 같은지 비교하고 checkbox와 radio로 구분하는 jsp파일
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*, poll.*" %>
<jsp:useBean id="pMgr" class="poll.PollMgr" />
<%
int num = 0; //비어있으면 초기화값인 0이 들어감
if(!(request.getParameter("num")==null || request.getParameter("num").equals(""))) {
//비어있지 않으면
num = Integer.parseInt(request.getParameter("num"));
}
PollListBean plBean = pMgr.getList(num);
ArrayList<String> alist = pMgr.getItem(num);
String question = plBean.getQuestion();
int type = plBean.getType();
int active = plBean.getActive();
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="pollFormProc.jsp">
<table>
<tr>
<td>Q : <%=question %> </td>
</tr>
<tr>
<td>
<% //하나가 아니라서 for문
for(int i=0; i<alist.size(); i++) {
String itemList = alist.get(i);
//아이템값이 문자열 하나씩밖에 없어서 하나에만 넣어줘도 됨
if(type==1) {
out.print("<input type=checkbox name='itemnum' value='" + i + "'>");
} else {
out.print("<input type=radio name='itemnum' value='" + i + "'>");
}
out.print(itemList + "<br>");
//폼의 선택사항의 종류 이름들 보여주기
}
%>
</td>
</tr>
<tr>
<td>
<%
if(active == 1) { //활성화==1
out.print("<input type='submit value='투표'>");
} else {
out.print("투표");
}
%>
  <input type="button" value="결과"
onclick="window.open('pollView.jsp?num=<%=num%>','PollView','width=500, height=400')">
</td>
</tr>
</table>
<input type="hidden" name="num" value="<%=num %>">
<!-- 폼의 submit을 누르면 얘도 같이 넘어가서 num값을 파라미터로 받을 수 있음 -->
</form>
</body>
</html>
행 개수만큼 bean 객체가 생성되고 그 객체들이 alist의 인덱스 0번부터 차곡차곡 쌓이기 때문에 자료형 PollListBean
num에 해당하는 item 값 가져오기