javaweb项目案例投票系统

javaweb项目案例投票系统

本博客只是记载自己学习javaweb历程
本项目用到了数据库的链接
不知道怎么链接数据库的可以参考一下

开发工具:myeclipse
数据库:mysql(Navicat for mySQL)
数据库代码与jsp写在同一页面了
先看一下最终效果

投票系统界面图

  1. jsp:2个jsp
  2. 一个是处理前台显示,一个是前台实现功能的业务处理逻辑

先看一下完整的项目目录

投票系统项目目录

  • index.jsp代码
<%@ page language="java" import="java.sql.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    <h1>欢迎给老师投票</h1>
    <table>
    	<tr>
    <td>编号</td>
    <td>姓名</td>
    <td>得票数</td>
    <td>投票</td>
    </tr>
    
    
    <%
    	
    	Class.forName("com.mysql.jdbc.Driver");
    	 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/teacherdb?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=UTC","root", "root");
    	Statement stat = conn.createStatement();
    	String sql = 
    	"select teacherno,teachername,vote from t_vote";
    	ResultSet rs = stat.executeQuery(sql);
    	while(rs.next()){
    	String teacherno = rs.getString("teacherno");
    	String teachername = rs.getString("teachername");
    	int vote = rs.getInt("vote");
    	
     %>
     <tr>
     <td><%= teacherno %></td>
     <td><%= teachername %></td>
     
     <td><img alt="" src="img/bar.jpg" width="<%=vote %>"height="10"><%=vote %></td>
     <td><a href="dealindex.jsp?teacherno=<%=teacherno%>">投票</a></td>
     </tr>
     <%
     }
     stat.close();
     rs.close();
     
      %>
    </table>
  </body>
</html>

  • dealindex.jsp
<%@ page language="java" import="java.sql.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'dealindex.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <%
    	String teacherno = request.getParameter("teacherno");
    	Class.forName("com.mysql.jdbc.Driver");
    	 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/teacherdb?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=UTC","root", "root");
    	String sql = "update t_vote set vote = vote + 1 where teacherno = ?";
    	PreparedStatement ps = conn.prepareStatement(sql);
    	ps.setString(1, teacherno);
    	ps.executeUpdate();
    	ps.close();
    	conn.close();
    	
     %>
     <jsp:forward page="index.jsp"></jsp:forward>
  </body>
</html>




bar.jpg图片自己用画图工具画一个就行了,大小没有限制。
有关软件下载,资源查找可以来看看小编的网站,谢谢
网址:www.miaozan.art

  • 4
    点赞
  • 63
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

RDSunday

爱,就供养;喜欢/受益,就打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值