From 52fc02c01340381d3f3d4c5c051fc2046796d230 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=8E=E4=BF=8A=E6=9D=A8?= <11785097+li-junyang3455@user.noreply.gitee.com> Date: Wed, 7 Jun 2023 10:54:14 +0000 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 黎俊杨 <11785097+li-junyang3455@user.noreply.gitee.com> --- .../20230607 \344\275\234\344\270\232.md" | 429 ++++++++++++++++++ 1 file changed, 429 insertions(+) create mode 100644 "11 \351\273\216\344\277\212\346\235\250/20230607 \344\275\234\344\270\232.md" diff --git "a/11 \351\273\216\344\277\212\346\235\250/20230607 \344\275\234\344\270\232.md" "b/11 \351\273\216\344\277\212\346\235\250/20230607 \344\275\234\344\270\232.md" new file mode 100644 index 0000000..41c077d --- /dev/null +++ "b/11 \351\273\216\344\277\212\346\235\250/20230607 \344\275\234\344\270\232.md" @@ -0,0 +1,429 @@ +# AddStudent + +```java +package bean; + +public class AddStudent { + int sid; + String sname; + + public AddStudent(int sid, String sname) { + this.sid = sid; + this.sname = 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; + } + + @Override + public String toString() { + return "AddStudent{" + + "sid=" + sid + + ", sname='" + sname + '\'' + + '}'; + } +} +``` + + + +# Student + +```java +package bean; + +import java.text.SimpleDateFormat; +import java.util.Date; + +public class Student { + int id; + String name; + String time; + int auto_sid; + int type; + + public Student(int id, String name,String time, int auto_sid, int type) { + this.id = id; + this.name = name; + this.time = time; + this.auto_sid = auto_sid; + this.type = type; + } + + public Student() { + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getTime() { + return time; + } + + public void setTime(String time) { + this.time = time; + } + + public int getAuto_sid() { + return auto_sid; + } + + public void setAuto_sid(int auto_sid) { + this.auto_sid = auto_sid; + } + + public int getType() { + return type; + } + + public void setType(int type) { + this.type = type; + } + + @Override + public String toString() { + + return "Student{" + + "id=" + id + + ", name='" + name + '\'' + + ", time=" + time + + ", auto_sid=" + auto_sid + + ", type=" + type + + '}'; + } +} +``` + + + +# AddServlet + +```java +package servlet; + +import bean.AddStudent; +import bean.Student; +import util.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +@WebServlet("/Add") +public class AddServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + String sql = "select * from student"; + + ResultSet rs = DBUtil.query(sql); + + ArrayList list = new ArrayList<>(); + + try { + while (rs.next()){ + int sid = rs.getInt("sid"); + String sname = rs.getString("sname"); + AddStudent stu = new AddStudent(sid, sname); + list.add(stu); + } + } catch (SQLException e) { + e.printStackTrace(); + } + + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/Add.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + + String sid = request.getParameter("sid"); + String time = request.getParameter("time"); + String type = request.getParameter("type"); + + String sql="insert into attence values (?,?,?,?)"; + System.out.println(time); + int i = DBUtil.update(sql, null, time, type, sid); + + if (i>0){ + response.sendRedirect("/student"); + }else{ + request.setAttribute("msg","添加失败"); + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + } + } +} +``` + + + +# StudentServlet + +```java +package servlet; + +import bean.Student; +import util.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.util.Date; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; + +@WebServlet("/student") +public class StudentServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String sql="select * from student,attence where attence.sid=student.sid"; + ResultSet rs = DBUtil.query(sql); + ArrayList list = new ArrayList<>(); + try { + while(rs.next()){ + + String time = rs.getString("time"); + int id = rs.getInt("sid"); + String name = rs.getString("sname"); + int auto_ud = rs.getInt("aid"); + int type = rs.getInt("type"); + + Student student = new Student(id, name, time, auto_ud, type); + list.add(student); + } + } catch (SQLException throwables) { + throwables.printStackTrace(); + + } + + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response); + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} +``` + + + +# DBUtil + +```java +package util; + +import java.sql.*; + +public class DBUtil { + static{ + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + System.out.println("驱动注册失败"); + e.printStackTrace(); + } + } + + public static Connection getConn() throws SQLException { + Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Attdb?useSSL=false&useUnicode=true&characterEncoding=utf8", "root", "root"); + return conn; + } + + public static ResultSet query(String sql,Object... keys){ + ResultSet rs= null; + try { + Connection conn = getConn(); + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + } + rs = pst.executeQuery(); + } catch (SQLException e) { + e.printStackTrace(); + } + return rs; + } + + public static int update(String sql,Object... keys){ + int num=0; + try { + Connection conn = getConn(); + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + } + num = pst.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + return num; + } +} +``` + + + +# Add.jsp + +```java +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: c + Date: 2023/6/7 + Time: 17:04 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +

学生考勤系统

+
+ + + + + + + + + + + + + + + + +
学生姓名 + +
考勤时间
考勤状况 + 已到 + 迟到 + 旷课 +
+
+ + +``` + + + +# list.jsp + +```java +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> +<%-- + Created by IntelliJ IDEA. + User: c + Date: 2023/6/7 + Time: 10:00 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 学生信息 + + +添加 + + + + + + + + + + + + + + + + + + + + +
考勤编号学生编号学生姓名出勤时间出勤状况
${student.auto_sid}${student.id}${student.name}${student.time} + 已到 + 迟到 + 旷课 + 未知 +
+ + +``` + + + +# msg/jsp + +```java +<%-- + Created by IntelliJ IDEA. + User: c + Date: 2023/6/7 + Time: 17:17 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +${msg} +返回列表 + + +``` \ No newline at end of file -- Gitee