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>
    

      

  • 相关阅读:
    “指定的SAS安装数据(sid)文件不能用于选定的SAS软件订单
    windows下如何快速优雅的使用python的科学计算库?
    量化分析师的Python日记【第1天:谁来给我讲讲Python?】
    Python的lambda函数与排序
    使用python管理Cisco设备-乾颐堂
    python移除系统多余大文件-乾颐堂
    python算法
    python实现高效率的排列组合算法-乾颐堂
    使用python把图片存入数据库-乾颐堂
    Python将阿拉伯数字转化为中文大写-乾颐堂
  • 原文地址:https://www.cnblogs.com/langlove/p/3758254.html
Copyright © 2011-2022 走看看