<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>
|