From 03d073239ed3c9c9e57f695da8121db9c3b2c5af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E6=95=8F?= <3234934487@qq.com> Date: Thu, 8 Jun 2023 12:00:23 +0800 Subject: [PATCH] zy --- .../20230608 \350\200\203\345\213\244.md" | 384 ++++++++++++++++++ 1 file changed, 384 insertions(+) create mode 100644 "56 \350\265\265\346\225\217/20230608 \350\200\203\345\213\244.md" diff --git "a/56 \350\265\265\346\225\217/20230608 \350\200\203\345\213\244.md" "b/56 \350\265\265\346\225\217/20230608 \350\200\203\345\213\244.md" new file mode 100644 index 0000000..ab12c1e --- /dev/null +++ "b/56 \350\265\265\346\225\217/20230608 \350\200\203\345\213\244.md" @@ -0,0 +1,384 @@ +bean + +```java +package bean; + +public class Attence { + private int aid; + private String time; + private int type; + private int sid; + private String sname; + + public int getAid() { + return aid; + } + + public void setAid(int aid) { + this.aid = aid; + } + + public String getTime() { + return time; + } + + public void setTime(String time) { + this.time = time; + } + + public int getType() { + return type; + } + + public void setType(int type) { + this.type = type; + } + + public int getSid() { + return sid; + } + + public void setSid(int sid) { + this.sid = sid; + } + + public String getSname() { + return sname; + } + + public void setSname(String sname) { + this.sname = sname; + } + + public Attence() { + } + + public Attence(int aid, String time, int type, int sid, String sname) { + this.aid = aid; + this.time = time; + this.type = type; + this.sid = sid; + this.sname = sname; + } + + @Override + public String toString() { + return "Attence{" + + "aid=" + aid + + ", time='" + time + '\'' + + ", type=" + type + + ", sid=" + sid + + ", sname='" + sname + '\'' + + '}'; + } +} + +``` + +```java +package bean; + +public class Student { + private int sid; + private String sname; + + public int getSid() { + return sid; + } + + public void setSid(int sid) { + this.sid = sid; + } + + public String getSname() { + return sname; + } + + public void setSname(String sname) { + this.sname = sname; + } + + public Student() { + } + + public Student(int sid, String sname) { + this.sid = sid; + this.sname = sname; + } + + @Override + public String toString() { + return "Student{" + + "sid=" + sid + + ", sname='" + sname + '\'' + + '}'; + } +} + +``` + +dao + +```java +package dao; + +import bean.Attence; +import bean.Student; +import util.DBUtil; + +import java.sql.Array; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +public class ServletDao { + //查询所有考勤记录 + public ArrayList allAttence() throws SQLException { + String sql="select * from attence"; + ArrayList list = new ArrayList<>(); + ResultSet re = DBUtil.getSelect(sql); + while (re.next()) { + int aid = re.getInt("aid"); + String time = re.getString("time"); + int type = re.getInt("type"); + int sid = re.getInt("sid"); + String sql2 = "select * from student where sid=?"; + ResultSet restu = DBUtil.getSelect(sql2, sid); + String sname = null; + while (restu.next()) { + sname = restu.getString("sname"); + } + Attence attence = new Attence(aid, time, type, sid, sname); + list.add(attence); + } + return list; + } + + //查看所有学生 + public ArrayList getStu() throws SQLException { + String sql="select * from student"; + ArrayList students = new ArrayList<>(); + ResultSet re = DBUtil.getSelect(sql); + while (re.next()){ + int sid = re.getInt("sid"); + String sname = re.getString("sname"); + Student student = new Student(sid, sname); + students.add(student); + } + return students; + } + + //添加考勤 + public int addAttence(String sid,String time,String type) throws SQLException { + String sql="insert into attence values (0,?,?,?)"; + int i = DBUtil.getUpdate(sql, time, type,sid); + return i; + } +} + +``` + +servlet + +```java +package servlet; + +import bean.Attence; +import bean.Student; +import dao.ServletDao; + +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; +import java.sql.SQLException; +import java.util.ArrayList; + +@WebServlet("/attence/*") +public class TestServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + req.setCharacterEncoding("utf-8"); + resp.setContentType("text/html;charset=utf-8"); + ServletDao dao = new ServletDao(); + String path = req.getPathInfo(); + if (path==null || path.equals("/")){ + ArrayList list = null; + try { + list = dao.allAttence(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + req.setAttribute("list",list); + req.getRequestDispatcher("/WEB-INF/allAtt.jsp").forward(req,resp); + } else if (path.matches("/add")) { + ArrayList stu = null; + try { + stu = dao.getStu(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + req.setAttribute("stu",stu); + req.getRequestDispatcher("/WEB-INF/addAtt.jsp").forward(req,resp); + } + + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + req.setCharacterEncoding("utf-8"); + resp.setContentType("text/html;charset=utf-8"); + + String sid = req.getParameter("sid"); + String time = req.getParameter("time"); + String type = req.getParameter("type"); + try { + int i = new ServletDao().addAttence(sid, time, type); + if (i>0){ + resp.sendRedirect("/attence"); + }else { + resp.getWriter().write("添加失败"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} + +``` + +util + +```java +package util; + +import java.sql.*; + +public class DBUtil { + private static final String url ="jdbc:mysql:///attdb?useSSL=false&characterEncoding=utf-8"; + private static final String user="root"; + private static final String pwd="root"; + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + + public static Connection getConn() throws SQLException { + Connection conn = DriverManager.getConnection(url, user, pwd); + return conn; + } + public static ResultSet getSelect(String sql,Object... keys) throws SQLException { + PreparedStatement pre = getConn().prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pre.setObject((i+1),keys[i]); + } + ResultSet re = pre.executeQuery(); + return re; + } + public static int getUpdate(String sql,Object... keys) throws SQLException { + PreparedStatement pre = getConn().prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pre.setObject((i+1),keys[i]); + } + int j = pre.executeUpdate(); + return j; + } +} + +``` + +jsp + +```jsp +<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +
+ + + + + + + + + + + + + + + + +
姓名:
时间:
出勤情况: + + 已到 + + 迟到 + + 旷课 +
+
+ + + +``` + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +
+

+
+ + + + + + + + + + + + + + + + + +
考勤编号学生编号学生姓名出勤时间出勤状况
${li.getAid()}${li.getSid()}${li.getSname()}${li.getTime()} + + 已到 + + + 迟到 + + + 旷课 + +
+ + + +``` + -- Gitee