1。这里会连接数据库--JDBC的学习实例
一共有3个页面。
2.第一个页面是一个form表单,第二个页面是处理数据,第三个页面是显示页面
vote.jsp
<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
<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
<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>