使用JDBC实现一个简易的投票系统

news/2024/5/18 23:27:26 标签: jdbc, mysql, jsp, web
webkit-tap-highlight-color: rgba(0, 0, 0, 0);">

使用JDBC实现一个简易的投票系统

首先在votesystem数据库中设计两张表:
person表用来存放被投人信息
在这里插入图片描述
t_ip表用来存放已经投过票的ip
在这里插入图片描述
然后设计主程序-投票页面vote.jsp
这是网上找的一个模板就拿来用了,侵删

<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="gb2312"%>
<%
	Class.forName("com.mysql.jdbc.Driver");
	Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/votesystem?user=root&password=123456");
	String sql = "select * from person";
	PreparedStatement psta = conn.prepareStatement(sql);
	ResultSet rs = psta.executeQuery();

%>

<html>
	<head>
		<title>投票主页</title>
		<style type="text/css">
			.a1:link {    
				COLOR: #E0FFFF; TEXT-DECORATION: none; font-size:11pt;
			}
			.a1:visited {   
				COLOR: #FFFFFF; TEXT-DECORATION: none; font-size:11pt;
			}
			.a1:active {    
				COLOR: #CD0000; TEXT-DECORATION: none; font-size:11pt;
			}
			.a1:hover {   
				COLOR: #4B0082; font-size:11pt;
			}
		</style>
	</head> 
	<body bgcolor="#F0FFFF">
		<table width="600" border="0" align="center" cellpadding="0"
			cellspacing="0">
			<tr>
				<td width="600" height="35"   background="images/vote_01_1.jpg">
					<table align="center">
						<tr>
							<td width="110"><a href="#" class="a1">首页</a></td>
							<td width="110"><a href="#" class="a1">最新新闻</a></td>
							<td width="110"><a href="#" class="a1">留言板</a></td>
							<td width="110"><a href="#" class="a1">联系我们</a></td>
							
						</tr>
					</table>
				</td>
			</tr>
			<tr>	
				<td>
					<img src="images/vote_01_2.jpg" width="600" height="256" />
				</td>
			</tr>	
			<tr>
				<td colspan="3">
					<img src="images/vote_02.gif" width="600" height="39" />
				</td>
			</tr>
			<tr>
				
				<td width="448" height="271" background="images/vote_03.jpg">
					<table width="300" height="250" border="1" align="center" cellpadding="0"cellspacing="0">
						<tr>
							<td height="20" align="center"><font size=4>选择你所投票的项目:</font></td>
						</tr>
						<tr>
							<td>
								<form action="voteadd.jsp">
								<table border=1 align=center>
									<tr>
										<td colspan="2">请选择你投票的人:</td>
									</tr>
									<%
										while(rs.next()){
									%>
									<tr>
										<td>
											<input type="radio" name="id" value="<%=rs.getString("uid")%>">
										</td>
										<td>
											<%=rs.getString("name") %>
										</td>
									</tr>
									<%} %>
									
									<tr>
										<td><input type="submit" value="投票"></td>
										<td><input type="button" value="查看"></td>
									</tr>
								</table>
								</form>
							</td>
						</tr>
					</table>
				</td>
				
			</tr>
			<tr>
				<td colspan="3">
					<img src="images/vote_04.jpg" width="600" height="48" />
				</td>
			</tr>
		</table>
	</body>
</html>
<%
	conn.close();
%>

voteadd.jsp判断页面,如果这个登入的IP地址在t_ip中存在,就拒绝登入,跳转到sorry.jsp页面;如果是第一次投票,就将对应被投人的voteCount加1,然后跳转到voteok.jsp查看投票结果。

<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="gb2312"%>
<html>
  <head>
   <title>vote-add</title>
</head>
  
  <body>
    <%
    	Class.forName("com.mysql.jdbc.Driver");//注册数据库	驱动
		//获取数据库连接
		Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/votesystem?user=root&password=123456");
		
    	//获取投票人的ip地址
    	//String IP = request.getRemoteAddr();
    	String IP = "";
    	if (request.getHeader("x-forwarded-for") == null) {  
	        IP = request.getRemoteAddr();  
	    }else{ 
	    	IP = request.getHeader("x-forwarded-for");  
	    }
	    //去数据库查询这个ip是否投票过,如果没有,如果投过 去跳转到sorry.jsp
    	String sql = "select * from t_ip where ip = ?";
    	PreparedStatement psta = conn.prepareStatement(sql);
    	psta.setString(1,IP);
		ResultSet rs = psta.executeQuery();
		if(rs.next()){
			response.sendRedirect("sorry.jsp");
			conn.close();
		}else{
			sql = "insert into t_ip(ip) values('"+ IP +"')";
			psta = conn.prepareStatement(sql);
			psta.executeUpdate();
			
			//接受传过来的id
	    	String str = request.getParameter("id");
	    	//链接数据库  将这个id的人的票数加1
			sql = "update person set voteCount = voteCount+1 where uid = ?";
			psta = conn.prepareStatement(sql);
	    	psta.setInt(1,Integer.parseInt(str));
			psta.executeUpdate();
			conn.close();
	    	//页面跳转到ok.jsp
	    	response.sendRedirect("voteok.jsp");
		}
    %>
  </body>
</html>

voteok.jsp展示投票结果

<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="gb2312"%>
<%
	Class.forName("com.mysql.jdbc.Driver");//注册数据库	驱动
	//获取数据库连接
	Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/votesystem?user=root&password=123456");
	
	String sql = "select * from person";
	PreparedStatement psta = conn.prepareStatement(sql);
	ResultSet rs = psta.executeQuery();
	
	Statement st = conn.createStatement();
	ResultSet rs1 = st.executeQuery("select sum(voteCount) from person");  
	int keyValue = -1;   
	if (rs1.next()) {  
    	rs1.getInt(1);  
	}  
	//out.print(rs1.getInt(1));
%>
<html>
	<head>
		<title>显示</title>
		<style type="text/css">
			body,td,th {
				font-size: 12px;
			}			
			body {
				margin-top: 0px;
				background-color: #FFF;
			}			
			.STYLE1 {
				font-size: 14px
			}			
			.STYLE3 {
				color: #FF0000;
				font-size: 14;
			}			
			.STYLE5 {
				color: #FF0000;
				font-size: 16px;
			}			
			.STYLE6 {
				font-family: Geneva, Arial, Helvetica, sans-serif;
				color: #FF0000;
			}
		</style>
	</head>
	<body>
		<table width="550" border="0" align="center" cellpadding="0" cellspacing="0">
	  		<tr>
	    		<td height="30" colspan="3" bgcolor="#FFCC00"><p class="STYLE1">&nbsp;&nbsp;&nbsp;&nbsp; 选项调查中总共有<span class="STYLE5"><%=rs1.getInt(1) %></span>人参加投票!</p></td>
	  		</tr>
	  		<tr bgcolor="f9cd34">
	    		<td width="40">&nbsp;</td>
	    		<td width="464" height="" bgcolor="fff2bb">
	    			<table border=1 align=center>
									<tr >
										<td colspan="3" width="800">当前投票结果:</td>
									</tr>
									<tr>
										<td>编号</td>
										<td>Idol</td>
										<td>票数</td>
									</tr>
									<%
										while(rs.next()){
									%>
									<tr>
										<td>
											<%=rs.getInt("uid") %>
										</td>
										<td>
											<%=rs.getString("name") %>
										</td>
										<td>
											<%=rs.getInt("voteCount") %>
										</td>
									</tr>
									<%} %>
					</table>
	    		</td>
	    		<td width="40">&nbsp;</td>
	  		</tr>
	  		<tr>
	   			 <td height="30" colspan="3" bgcolor="#FFCC00">&nbsp;</td>
	  		</tr>
		</table>
	</body>
</html>
<%
	conn.close();
%>

sorry.jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>投票失败</title>
	</head>
	<body>
		<p align="center">
			对不起,你已投过一次票了,请不要多次投票!
		</p>
		<p align="center">
			<span class="STYLE1">
				<a href="vote.jsp">回主页面</a>&nbsp; 
				<a href="voteok.jsp">查看结果</a>
			</span>
		</p>
	</body>
</html>

效果展示
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


http://www.niftyadmin.cn/n/776420.html

相关文章

shell脚本学习_函数_5

学习目标&#xff1a; 学习shell脚本函数使用 总结&#xff1a; 1- 函数使用和C语言类似。函数调用直接写函数名和传入参数&#xff1b;函数返回值可以借用$?处理。 2- shell中也有局部变量全局变量。局部变量加关键字local&#xff0c;不加的都是全局变量。 正文 1- 基本…

IIS如何部署JavaWeb环境

IIS如何部署JavaWeb环境 打个桩 https://blog.csdn.net/qq_31394845/article/details/77619377 https://blog.csdn.net/hualele/article/details/79310823?utm_termiis%E9%83%A8%E7%BD%B2java%E9%A1%B9%E7%9B%AE&utm_mediumdistribute.pc_aggpage_search_result.none-tas…

shell脚本学习_正则表达式_6

写在前面&#xff1a; 正则表达式接触的不多&#xff0c;从接触的用法来看主要用于搜索、匹配&#xff0c;主要包括文件名、文件内容。可用于shell脚本中&#xff0c;但是没怎么用过。。。 最常用的是我们想从当前目录下查找文件名中包含hello的文件 ls -R |grep hello 当然…

内网穿透搭建JavaWeb环境并部署JavaWeb项目

IIS搭建JavaWeb环境并部署JavaWeb项目 需求&#xff1a;将项目利用Ecplisetomcat花生壳搭建起来&#xff0c;内网穿透。 工具&#xff1a;Ecplise Tomcat7.0 花生壳 环境&#xff1a;Windows7part 1&#xff1a;本机电脑部署JavaWeb运行环境 安装JDK&#xff0c;我使用的是JDK…

云数据库搭建——项目上线(1)

云数据库搭建——项目上线&#xff08;1&#xff09; 本着一切从简的理念开始 云平台&#xff1a;阿里云 项目&#xff1a;JavaWeb 服务器&#xff1a;Tomcat 数据库&#xff1a;polardb1、获取云服务器及其使用 打开阿里云开发者试用中心&#xff0c;有试用权限的的可以免费…

C语言基础_文件操作-1

目录 写在前面 学习目标&#xff1a; 学习总结&#xff1a; 正文&#xff1a; 1- 文件打开、关闭 2- getc、putc 3- fprintf 、fscanf &#xff08;gets不安全&#xff0c;用fgets&#xff09; 4- fgets、gputs 5- 文件光标操作 a- fseek、ftell&#xff08;long范围…

idea打开项目无法运行的解决办法

idea打开项目无法运行的解决办法 今天同学把项目传给我让我帮他看看什么问题&#xff0c;但是当我用idea打开后发现无法运行 整个src下的Java文件都有红点标记&#xff0c;main方法也不能运行&#xff0c;但实际上我的jdk都是配置好的。 解决如下&#xff1a; 这种情况是IDE…

linux基础操作_GDB简单使用

学习目标&#xff1a; 学习gdb的用法&#xff0c;包括常用的单步调试、设置断点。 总结&#xff1a; gdb使用可执行文件进入调试模式&#xff0c;可执行文件生成过程加-g参数 重点关注相关断点命令&#xff08;break 行; info break; delet 断点号; clear 断点行&#xff09…