public void addMark(int mark,String name,String uname) {
Connection con = null;
Statement sta;
Connection con1 = null;
Statement sta1;
int count = 0;
int n = 2;//这是我数据库目前只有两个用户
Connection con2 = null;
Statement sta2;
ResultSet res = null;
try {
con = OpenConnection.getCon();
con1 = OpenConnection.getCon();
con2 = OpenConnection.getCon();
String sql = "update user set "+name+" = "+mark+" where username = '"+uname+"'";
String sql1 = "select "+name+" from user";
sta = con.createStatement();
sta.executeUpdate(sql);
sta1 = con1.createStatement();
res = sta1.executeQuery(sql1);
while(res.next()){
if(res.getInt(name)!=0)
count += res.getInt(name);
else
n -= 1;
}
if(n==0)
count = 0;
else
count = count/n;
String sql2 = "update player set mark = "+count+" where name = '"+name+"'";
sta2 = con2.createStatement();
sta2.executeUpdate(sql2);
} catch (SQLException e) {
e.printStackTrace();
}
}
package com.demo.service;
import com.demo.Database.Database;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class SqlServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public SqlServlet() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
String username = request.getSession().getAttribute("username").toString();
int total = 0;
for(int i=1;i<=2;i++){ //两个需要打分的角色
String name = request.getParameter("name"+i);
total = Integer.parseInt(request.getParameter("total"+i));
System.out.println(name);
System.out.println(total);
Database database = new Database();
database.addMark(total,name,username);
}
if(total>=0){
request.getRequestDispatcher("success.jsp").forward(request,response);
}
}
}
<%--
Created by IntelliJ IDEA.
User: 89462
Date: 2020/6/22
Time: 15:27
To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Statement" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Vote</title>
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div style="text-align:center;margin-top:30px">、
<% //显示登陆用户,调整字体%>
<%String username = request.getSession().getAttribute("username").toString();%>
<font color="red" size="30px">您好,<%=username %></font>
<br style="margin-top:10px"></br>
<form action="SqlServlet" name="Vote" method="post"> <% // action连接自定类进行后续操作%>
<table style="margin-left:18%" > <% // 边距,使整体在中间显示%>
<marquee width="450"scrolldelay="100"style="font-size:30px;color: floralwhite">欢迎使用在线评分系统</marquee>
<% // 滚动显示%>
<br style="margin-top:40px"></br>
<tr>
<td width="100" style="font-size:20px;color: salmon">表演者</td>
<td width="100" style="font-size:20px;color: salmon">语言(40)</td>
<td width="100" style="font-size:20px;color: salmon">神态(30)</td>
<td width="100" style="font-size:20px;color: salmon">动作(30)</td>
<td width="100" style="font-size:20px;color: salmon">总评(100)</td>
<td width="100" style="font-size:20px;color: salmon">当前得分</td>
</tr>
<%
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/users_data?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false";
String user = "root";
String password = "4483815zym";
Connection conn = DriverManager.getConnection(url, user, password);
if(conn != null){
%>
<%
Statement stmt;
ResultSet rs;
String sql = "SELECT * FROM player;"; //查询语句
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
String r = "role";
int n = 1;
String rr;
String nn;
String tt;
while (rs.next()) {
rr = r + n;
nn = "name" + n;
tt = "total" + n;
n += 1;
%>
<tr>
<td style="font-size:20px;color: salmon"><%=rs.getString("name") %></td>
<td><input id="<%=nn%>1" name="word" type="number" oninput="if(value>30)value=40" size="10" onkeyup="calculate();"></td>
<td><input id="<%=nn%>2" name="expression" type="number" oninput="if(value>30)value=30" size="10" onkeyup="calculate();"></td>
<td><input id="<%=nn%>3" name="action" type="number" oninput="if(value>30)value=30" size="10" onkeyup="calculate();"></td>
<td><input id="<%=nn%>4" name="<%=tt%>" type="number" size="10";></td>
<td style="font-size:20px;color: salmon"><%=rs.getString("mark") %></td>
<input type="hidden" name="<%=nn%>" value="<%=rr%>"/>
</tr>
<%
}
}else{
out.print("连接失败!");
}
}catch (Exception e) {
//e.printStackTrace();
out.print("数据库连接异常!");
}
%>
</table>
<input type="submit" value="提交"> <% // 表单提交%>
<input type="reset" value="重置"> <% // 自带清零%>
</form>
<script language="javascript" type="text/javascript">
function calculate(){
for(var i=1;i<=2;i++){
var text1 = document.getElementById("name"+i+"1").value;
var text2 = document.getElementById("name"+i+"2").value;
var text3 = document.getElementById("name"+i+"3").value;
document.getElementById("name"+i+"4").value=parseInt(text1)+parseInt(text2)+parseInt(text3);
}
}
</script>
</div>
</body>
</html>
效果,123是用户
player数据库
user数据库
calculate是用来自动计算求和的,总评会自动出来,效果如下:
因为我从来没有碰过JSP,所以用的循环,name定义也比较粗糙(哭),但总体需要的效果已经有了。
包括多用户的打分求均(0分会剔除)并在页面显示(当前得分)
大作业接近尾声,
下面就是一些小修改。
还有离散大作业= =
这是我第一次记录我的编程,过程都比较粗糙,不好意思哈