From 091928c5c4700c060eed40a17127cd78e00d9ef4 Mon Sep 17 00:00:00 2001 From: hql <3239791920@qq.com> Date: Sun, 20 Apr 2025 15:26:12 +0800 Subject: [PATCH] Update README.md --- .idea/workspace.xml | 42 ++++++++++------- .../deptadmin/DeptAddStuPunchServlet.java | 47 ++++++++++--------- 2 files changed, 52 insertions(+), 37 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index a0b001a..d040e98 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -10,7 +10,8 @@ - + + @@ -358,7 +359,8 @@ - + + - diff --git a/src/com/controller/deptadmin/DeptAddStuPunchServlet.java b/src/com/controller/deptadmin/DeptAddStuPunchServlet.java index 1e2fa5c..d7c1bd7 100644 --- a/src/com/controller/deptadmin/DeptAddStuPunchServlet.java +++ b/src/com/controller/deptadmin/DeptAddStuPunchServlet.java @@ -7,54 +7,59 @@ import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.io.IOException; +import java.io.IOException; //导入IO异常和日期类 import java.util.Date; @WebServlet("/DeptAddStuPunchServlet") -public class DeptAddStuPunchServlet extends HttpServlet { +public class DeptAddStuPunchServlet extends HttpServlet { //定义一个继承自HttpServlet的Servlet类。 @Override - protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + 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"); - String sispunch = req.getParameter("sispunch"); - String spunchdate = req.getParameter("spunchdate"); - String spunchtime = req.getParameter("spunchtime"); - String sishot = req.getParameter("sishot"); - String siscough = req.getParameter("siscough"); - String sisseem = req.getParameter("sisseem"); - String sisdiagnose = req.getParameter("sisdiagnose"); - String sstatus = req.getParameter("sstatus"); + 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 = ?"; - Object[] objects = {sno, spunchdate}; - int count = DeptAdminDao.findTotalCount(sql, objects); + 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){//无则操作 + 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); + 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); - } + 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); + doGet(req, resp);//处理POST请求:重写doPost方法,直接调用doGet,统一处理GET和POST请求。 + + } }