JavaWeb实验 JavaWeb编程实训(投票系统)

实验目的

   通过本课程的学习,能够独立初步完成完整的Web系统。 

实验内容

编写一个投票系统,包括两个JSP页面,vote.jsp页面如图1所示,显示投票选项,并显示当前的投票进度;点击“投他一票”后,所选的投票人增加一票(利用struts2实现投票功能),然后在display.jsp页面显示投票结果,并高亮显示第一名的投票人,如图2所示。

Struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<constant name="struts.aciton.extension" value="ac"></constant>
	<package name="struts" extends="struts-default">
		<action name="vote" class="com.jsp1.Vote" method="voteone">
			<result name="success">voteview.jsp</result>
			<result name="fail">voteview.jsp</result>
		</action>
	</package>
</struts>


Jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8" import="com.jsp1.VoteSQL,java.util.ArrayList,com.jsp1.Vote"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>投票界面</title>
</head>
<body>
	<h1>十大角色投票</h1>
	<%
  	ArrayList list=VoteSQL.getAllVoteList();
	int maxVal=VoteSQL.getMaxCount();
  %>
	<table>
		<%
  for(int i=0;i<list.size();i++){
	  Vote v=(Vote)list.get(i);
	  int valper=(int)(((float)v.getVotecount()/maxVal)*200);
  %>
		<tr>
			<td><%=i+1 %></td>
			<td style="width: 80px;"><%=v.getName() %></td>
			<td>
				<div style="width: 250px; background-color: pink;">
					<div style="float:left;width:<%=valper%>px;background-color:red;">&nbsp;</div>
					<%=v.getVotecount() %>
				</div>
			</td>
			<td><a href="./vote.html?id=<%=v.getId()%>">投他一票</a></td>
		</tr>
		<%
     }
    %>
	</table>
</body>
</html>
Java
package com.jsp1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

public class VoteSQL {
	public static int getMaxCount() {
		int maxval=0;
		try {
			Class.forName("com.mysql.jdbc.Driver"); // 驱动程序名
			String url = "jdbc:mysql://localhost:3306/vote?useUnicode=true&characterEncoding=utf8"; // 数据库名
			String username = "root"; // 数据库用户名
			String password = "root"; // 数据库用户密码
			Connection conn = DriverManager.getConnection(url, username, password);

			Statement stmt = null;
			ResultSet rs = null;
			stmt = conn.createStatement();
			String sql = "SELECT MAX(votecount) FROM voteresult;";
			rs = stmt.executeQuery(sql);

			while (rs.next()) {
				maxval=rs.getInt(1);
			}

			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return maxval;
	}
	
	
	
	public static ArrayList getAllVoteList() {
		ArrayList list = new ArrayList();
		try {

			Class.forName("com.mysql.jdbc.Driver"); // 驱动程序名
			String url = "jdbc:mysql://localhost:3306/vote?useUnicode=true&characterEncoding=utf8"; // 数据库名
			String username = "root"; // 数据库用户名
			String password = "root"; // 数据库用户密码
			Connection conn = DriverManager.getConnection(url, username, password);

			Statement stmt = null;
			ResultSet rs = null;
			stmt = conn.createStatement();
			String sql = "SELECT * FROM voteresult;";
			rs = stmt.executeQuery(sql);

			while (rs.next()) {
				int id = rs.getInt("id");
				String name = rs.getString("name");
				int votecount = rs.getInt("votecount");

				Vote v = new Vote();
				v.setId(id);
				v.setName(name);
				v.setVotecount(votecount);
				list.add(v);

			}

			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return list;
	}
	
	public static void addVoteById(int id) {
		try {

			Class.forName("com.mysql.jdbc.Driver"); // 驱动程序名
			String url = "jdbc:mysql://localhost:3306/vote?useUnicode=true&characterEncoding=utf8"; // 数据库名
			String username = "root"; // 数据库用户名
			String password = "root"; // 数据库用户密码
			Connection conn = DriverManager.getConnection(url, username, password);

			Statement stmt = null;
			ResultSet rs = null;
			stmt = conn.createStatement();
			String sql = "update voteresult set votecount=votecount+1 where id=" + id;
			stmt.executeUpdate(sql);

			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


}
package com.jsp1;

public class Vote {
		private int id;
		private String name;
		private int votecount;
		
		public int getVotecount() {
			return votecount;
		}
		public void setVotecount(int votecount) {
			this.votecount=votecount;
		}
		public int getId() {
			return id;
		}
		public void setId(int id) {
			this.id=id;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}

		public String voteone() {
			VoteSQL.addVoteById(id);
			return "success";

		}

	 }













连接数据库参考代码:
//说明:需要在头部引入java.sql.*包,下面的代码为JSP的连接参考,如果在Java使用需要进行适当修改。
try {
		Class.forName("com.mysql.jdbc.Driver"); //驱动程序名
		String url = "jdbc:mysql://localhost:3306/chg"; //数据库名
		String username = "root"; //数据库用户名
		String password = "root"; //数据库用户密码
		Connection conn = DriverManager.getConnection(url, username,
				password); //连接状态

		if (conn != null) {
			out.print("数据库连接成功!");
			out.print("<br />");
			Statement stmt = null;
			ResultSet rs = null;
			stmt = conn.createStatement();
			String sql = "SELECT * FROM teachers;"; //查询语句
			rs = stmt.executeQuery(sql);
			
			out.print("查询结果:");
			out.print("<br />");
			while (rs.next()) {
				out.println(rs.getString(1)+rs.getString(2)+"<br>"); //将查询结果输出  
			}
		} else {
			out.print("连接失败!");
		}
} catch (Exception e) {
		out.print("数据库连接异常!" + e.getStackTrace().toString());
		e.printStackTrace(new java.io.PrintWriter(out));
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值