zoukankan      html  css  js  c++  java
  • java web 程序---投票系统

    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>
    

      

  • 相关阅读:
    android 6.0 新特性
    接口_ _接口回调机制
    bug_ _
    volley_缓存介绍
    bug__android studio 出现布局文件不提示,且点击代码不能跟踪代码
    文章--笔记本蓝牙可以搜索到手机,但是怎么连接不了?
    Dialog_ _dialog系统样式讲解 及 透明背景
    动画_ _ Android应用开发之所有动画使用详解
    view坐标_ _ Android应用坐标系统全面详解
    html__脚本之家
  • 原文地址:https://www.cnblogs.com/langlove/p/3758254.html
Copyright © 2011-2022 走看看