1。这里会连接数据库--JDBC的学习实例
一共有3个页面。
2.第一个页面是一个form表单,第二个页面是处理数据,第三个页面是显示页面
vote.jsp
1
2
3
4
5
6
7
8
9
10
11
12 |
<body bgcolor= "green" > 选择你要投票的人: <form action= "vote_end.jsp" > <input type= "radio"
name= "pp"
value= "a" />周杰伦<img src= "img/a.jpg" /> <br><input type= "radio"
name= "pp"
value= "b" />张 杰<img src= "img/b.jpg" /> <br><input type= "radio"
name= "pp"
value= "c" />范冰冰<img src= "img/c.jpg" /> <br><input type= "radio"
name= "pp"
value= "d" />赵 薇<img src= "img/d.jpg" /> <br><input type= "radio"
name= "pp"
value= "e" />黄晓明<img src= "img/e.jpg" /> <br><br><input type= "submit"
value= "提交" /> </form> </body> |
vote_end2.jsp
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 |
<body bgcolor= "red" > <center> <% String sess = request.getSession().getId(); String sess2 = null ; out.print( "恭喜你,投票成功。<br>" ); String pp = request.getParameter( "pp" ); String people = null ; if (pp.equals( "a" )){ people= "‘周杰伦‘" ; } else
if (pp.equals( "b" )){ people= "‘张杰‘" ; } else
if (pp.equals( "c" )){ people= "‘范冰冰‘" ; } else
if (pp.equals( "d" )){ people= "‘赵薇‘" ; } else
if (pp.equals( "e" )){ people= "‘黄晓明‘" ; } Class.forName( "com.mysql.jdbc.Driver" ); Connection connection=DriverManager.getConnection( "jdbc:mysql://localhost/test?user=root&password=123&useUnicode=true&characterEncoding=gbk" ); Statement statement = connection.createStatement(); //查看是否投过票 ResultSet rs2 = statement.executeQuery( "SELECT * FROM sess" ); while (rs2.next()){ sess2 = rs2.getString( "id" ); if (sess2.equals(sess)){ %> <jsp:forward page= "vote_no.jsp" /> <% } } //查找数据库 ResultSet rs = statement.executeQuery( "SELECT * FROM people where name=" +people); rs.next(); int
count = rs.getInt( "count" ); count = count+ 1 ; //更新数据库 statement.executeUpdate( "UPDATE people SET count=" +count+ " where name=" +people); //投票session号保存到数据库 statement.executeUpdate( "insert into sess values(‘" +sess+ "‘)" ); //显示数据库 ResultSet rss = statement.executeQuery( "SELECT * FROM people" ); out.print( "<table border=1>" ); out.print( "<tr>" ); out.print( "<th>姓名</th>" ); out.print( "<th>票数</th>" ); out.print( "</tr>" ); while (rss.next()) { out.print( "<tr>" ); out.print( "<td>" +rss.getString( 1 )+ "</td>" ); out.print( "<td>" +rss.getString( 2 )+ "</td>" ); out.print( "</tr>" ); } out.print( "</table>" ); rs.close(); statement.close(); connection.close(); %> </center> </body> |
vote_end.jsp
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 |
<body bgcolor= "red" > <center> <% out.print( "恭喜你,投票成功。<br>" ); String pp = request.getParameter( "pp" ); String people = null ; if (pp.equals( "a" )){ people= "‘周杰伦‘" ; } else
if (pp.equals( "b" )){ people= "‘张杰‘" ; } else
if (pp.equals( "c" )){ people= "‘范冰冰‘" ; } else
if (pp.equals( "d" )){ people= "‘赵薇‘" ; } else
if (pp.equals( "e" )){ people= "‘黄晓明‘" ; } Class.forName( "com.mysql.jdbc.Driver" ); Connection connection=DriverManager.getConnection( "jdbc:mysql://localhost/test?user=root&password=123&useUnicode=true&characterEncoding=gbk" ); Statement statement = connection.createStatement(); //查找数据库 ResultSet rs = statement.executeQuery( "SELECT * FROM people where name=" +people); rs.next(); int
count = rs.getInt( "count" ); count = count+ 1 ; //更新数据库 statement.executeUpdate( "UPDATE people SET count=" +count+ " where name=" +people); //显示数据库 ResultSet rss = statement.executeQuery( "SELECT * FROM people" ); out.print( "<table border=1>" ); out.print( "<tr>" ); out.print( "<th>姓名</th>" ); out.print( "<th>票数</th>" ); out.print( "</tr>" ); while (rss.next()) { out.print( "<tr>" ); out.print( "<td>" +rss.getString( 1 )+ "</td>" ); out.print( "<td>" +rss.getString( 2 )+ "</td>" ); out.print( "</tr>" ); } out.print( "</table>" ); rs.close(); statement.close(); connection.close(); %> </center> </body> |
java web 程序---投票系统,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/langlove/p/3758254.html