You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lsepidemicsituationsystem8/src/com/controller/deptadmin/DeptAddStuPunchServlet.java

66 lines
4.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.controller.deptadmin;
//
import com.dao.DeptAdminDao;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; //导入IO异常和日期类
import java.util.Date;
@WebServlet("/DeptAddStuPunchServlet")
public class DeptAddStuPunchServlet extends HttpServlet { //定义一个继承自HttpServlet的Servlet类。
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //重写doGet方法处理HTTP GET请求。
//统一请求和响应的字符编码为UTF-8防止中文乱码并设置响应内容类型为HTML。
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//获取表单请求的参数
String sno = req.getParameter("sno"); //从 HTTP 请求中获取参数名为 "sno" 的值,并将其赋值给 sno 变量
String sispunch = req.getParameter("sispunch");//从 HTTP 请求中获取参数名为 "sispunch" 的值,并将其赋值给 sispunch 变量
String spunchdate = req.getParameter("spunchdate");//从 HTTP 请求中获取参数名为 "spunchdate" 的值,并将其赋值给 spunchdate 变量
String spunchtime = req.getParameter("spunchtime");//从 HTTP 请求中获取参数名为 "spunchtime" 的值,并将其赋值给 spunchtime 变量
String sishot = req.getParameter("sishot");//从 HTTP 请求中获取参数名为 "sishot" 的值,并将其赋值给 sishot 变量
String siscough = req.getParameter("siscough");//从 HTTP 请求中获取参数名为 "siscough" 的值,并将其赋值给 siscough 变量
String sisseem = req.getParameter("sisseem");//从 HTTP 请求中获取参数名为 "sisseem" 的值,并将其赋值给 sisseem 变量
String sisdiagnose = req.getParameter("sisdiagnose");//从 HTTP 请求中获取参数名为 "sisdiagnose" 的值,并将其赋值给 sisdiagnose 变量
String sstatus = req.getParameter("sstatus");//从 HTTP 请求中获取参数名为 "sstatus" 的值,并将其赋值给 sstatus 变量
String sql = null;
//声明SQL语句变量并输出调试信息
System.out.println("shgshgh");
//查询是否存在此人
sql = "select count(*) as num from stupunchin where sno = ? and spunchdate = ?";//检查stupunchin表中是否存在相同学号和日期的记录
Object[] objects = {sno, spunchdate}; //使用Object数组传递参数防止SQL注入。
int count = DeptAdminDao.findTotalCount(sql, objects);//调用DAO方法findTotalCount返回匹配记录数。
if (count == 0){//条件判断如果不存在重复记录count为0执行插入操作。
sql = "insert into stupunchin values(?, ?, ?, ?, ?, ?, ?, ?, ?)";
Object[] objects1 = {sno, sispunch, spunchdate, spunchtime, sishot, siscough, sisseem, sisdiagnose, sstatus};
//SQL插入语句向stupunchin表插入包含9个字段的新记录
//参数绑定:将表单参数按顺序填入占位符
int num = DeptAdminDao.executeUpdate(sql, objects1);//执行更新调用DAO方法executeUpdate执行插入返回受影响的行数。
System.out.println(num);
req.getRequestDispatcher("/DeptQueryStuPunchByPageServlet?currentPage=1&rows=7&sname=&sclass=&spunchdate=").forward(req, resp);
}else {
req.getRequestDispatcher("/view/alluse/existdataofadd.jsp").forward(req, resp);//转发请求插入成功后转发到分页查询Servlet显示第一页数据参数currentPage=1每页7行
}//处理重复数据如果记录已存在转发到提示页面existdataofadd.jsp
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);//处理POST请求重写doPost方法直接调用doGet统一处理GET和POST请求
}
}