012
[#M_ 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<HTML>
<HEAD>
<TITLE> JSP를 이용한 여론조사 </TITLE>
</HEAD>
<BODY>
<CENTER><table border="0">
<tr>
<td align="center">
<h2> 여론조사 </h2>
<font size="3"> 좋아하는 과일을 조사합니다.</font><br><br>
<font size="3"> 투표하기 링크를 누르면 조사에 참여할 수 있습니다.</font><br><br>
<font size="3"> 결과보기 링크를 누르면 결과를 확인할 수 있습니다.</font><br><br>
<img src="image/green_tree.gif"> <a href="opinion-poll.html"> 투표하기 </a>
<img src="image/red_tree.gif"> <a href="opinion-result.jsp"> 결과보기 </a><br>
<img src="image/island.gif" width="70%">
</td>
</tr>
</table></CENTER>
</BODY>
</HTML>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <HTML> <HEAD> <TITLE> JSP를 이용한 여론조사 </TITLE> </HEAD> <BODY> <table border="0"> <tr><td align="center"> <h2> 여론조사</h2> <form action="opinion-result.jsp" method="post"> <img src="image/ball.gif"> 좋아하는 과일을 선택해 주세요. <p> <input type="radio" name="opinion_id" value="0"> 사과 <br> <input type="radio" name="opinion_id" value="1"> 포도 <br> <input type="radio" name="opinion_id" value="2"> 딸기 <br> <input type="radio" name="opinion_id" value="3"> 메론 <p> <input type="submit" value="투표하기"> </form> <img src="image/island.gif" width="70%"> </td></tr> </table> </BODY> </HTML> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <%@ page import="java.sql.*" %> <%@ page import="java.text.*" %> <% request.setCharacterEncoding("euc-kr"); %> <html> <head><title> JSP를 이용한 여론조사 </title></head> <body> <% int choice_id, i; int opinion[]={0, 0, 0, 0}; float total, rate[]={0, 0, 0, 0}; String choice=""; String fruit[]={"사과", "포도", "딸기", "메론"}; String opinion_id=request.getParameter("opinion_id"); Connection conn=null; Statement stmt=null; String sql=null; ResultSet rs=null; try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/opinion_db"; conn = DriverManager.getConnection(url,"root","1234"); stmt = conn.createStatement(); sql = "select * from opinion_tbl"; rs = stmt.executeQuery(sql); } catch(Exception e) { out.println("DB 연동 오류입니다. : " + e.getMessage() ); } if(!rs.next()) { for(i = 0; i < 4; i++) { String sql1 = "insert into opinion_tbl values (" + i + ",'" + fruit[i] + "', 0)"; try{ stmt.executeUpdate(sql1); } catch(Exception e) { out.println("DB 연동 오류입니다. : " + e.getMessage()); } } } else { rs.previous(); i = 0; while(rs.next()) { opinion[i] = Integer.parseInt(rs.getString("sum")); i++; } } if(opinion_id != null) { String sql2 = "update opinion_tbl set sum = sum + 1 where id = " + opinion_id; try{ stmt.executeUpdate(sql2); } catch(Exception e) { out.println("DB 연동 오류입니다. : " + e.getMessage()); } } choice_id = Integer.parseInt(opinion_id); opinion[choice_id] += 1; total = opinion[0] + opinion[1] + opinion[2] + opinion[3]; for(i = 0; i < 4; i++) rate[i] = (opinion[i] / total) * 100; %> <center> <h2> 여론조사 결과 </h2> <table border="0" width="500"> <tr> <td bgcolor="yellow" width="30%"><b> 사과 : <%=Math.round(rate[0])%>%</b></td> <td><img src="image/blue_pole.gif" width=<%=rate[0]%>% height="25"></td> </tr> <tr> <td bgcolor="orange"><b> 포도 : <%=Math.round(rate[1])%>% </b></td> <td><img src="image/blue_pole.gif" width=<%=rate[1]%>% height="25"></td> </tr> <tr> <td bgcolor="salmon"><b> 딸기 : <%=Math.round(rate[2])%>%</b></td> <td><img src="image/white_pole.gif" width=<%=rate[2]%>% height="25"></td> </tr> <tr> <td bgcolor="blue" width="30%"><b> 메론 : <%=Math.round(rate[3])%>%</b></td> <td><img src="image/white_pole.gif" width=<%=rate[3]%>% height="25"></td> </tr> </table><br><br> 당신은<font color="blue"><b><%=fruit[choice_id]%></b></font>를(을) 선택하였습니다.<br> 총 <b><font color="red"><%=Math.round(total)%></font><b>명이 참여하였습니다.<p> <a href="opinion.html"> 처음으로</a> </center> </body> </html> |
책에 있는 예제 그대로임. 끝.
'웹프로그래밍 > 실습' 카테고리의 다른 글
JSP) for, JDBC (0) | 2016.12.24 |
---|---|
JSP) String, 예외처리 (0) | 2016.12.05 |
SQL (0) | 2016.11.24 |
HTML5) 캔버스, drag and drop (0) | 2016.11.18 |
HTML5) number, range, meter, 입력태그 (1) | 2016.11.15 |