Compare commits

..

4 Commits
master ... zp

Author SHA1 Message Date
hql e1eae68917 2
10 months ago
hql e278f6c487 1.1
10 months ago
hql 7ca55e099d 1
10 months ago
hql d18e6aee4e Default Changelist
10 months ago

@ -5,56 +5,61 @@ 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.HttpServletRequest;//fdsfdsfds2542354823
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请求
}
}

Loading…
Cancel
Save