From 3e26349e23f75f013545d3ee78457d2ab21b55f3 Mon Sep 17 00:00:00 2001 From: unknown <2099677008@qq.com> Date: Tue, 6 Jun 2023 22:35:58 +0800 Subject: [PATCH] =?UTF-8?q?=E8=80=83=E5=8B=A4=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...03\345\213\244\347\256\241\347\220\206.md" | 461 ++++++++++++++++++ 1 file changed, 461 insertions(+) create mode 100644 "34 \347\250\213\351\230\263/20230606\350\200\203\345\213\244\347\256\241\347\220\206/\350\200\203\345\213\244\347\256\241\347\220\206.md" diff --git "a/34 \347\250\213\351\230\263/20230606\350\200\203\345\213\244\347\256\241\347\220\206/\350\200\203\345\213\244\347\256\241\347\220\206.md" "b/34 \347\250\213\351\230\263/20230606\350\200\203\345\213\244\347\256\241\347\220\206/\350\200\203\345\213\244\347\256\241\347\220\206.md" new file mode 100644 index 0000000..44c0483 --- /dev/null +++ "b/34 \347\250\213\351\230\263/20230606\350\200\203\345\213\244\347\256\241\347\220\206/\350\200\203\345\213\244\347\256\241\347\220\206.md" @@ -0,0 +1,461 @@ +```mysql +CREATE DATABASE AttDB charset utf8; +USE AttDB; +CREATE TABLE Student ( +sid int PRIMARY KEY auto_increment COMMENT "学号", +sname varchar(20) UNIQUE NOT NULL COMMENT "学生姓名" +); + +INSERT INTO Student VALUES +(0,"张三"), +(0,"李四"), +(0,"王五"); + +CREATE TABLE Attence ( +aid int PRIMARY KEY auto_increment COMMENT "考勤编号", +time varchar(20) NOT NULL COMMENT "出勤时间", +type INT COMMENT "出勤状况", +sid int, +FOREIGN KEY (sid) REFERENCES Student (sid) +); + +INSERT INTO Attence VALUES +(0,"2022-05-20 08:20:00",1,1), +(0,"2022-05-21 08:20:00",2,2), +(0,"2022-05-22 08:20:00",3,3), +(0,"2022-05-23 08:20:00",2,3), +(0,"2022-05-24 08:20:00",1,2); +``` + +```java +//bean_Attence +package bean; + +public class Attence { + private int aid; + private int sid; + private int type; + private String name; + private String time; + + public Attence() { + } + + public Attence(int aid, int sid, int type, String name, String time) { + this.aid = aid; + this.sid = sid; + this.type = type; + this.name = name; + this.time = time; + } + + public int getAid() { + return aid; + } + + public void setAid(int aid) { + this.aid = aid; + } + + public int getSid() { + return sid; + } + + public void setSid(int sid) { + this.sid = sid; + } + + public int getType() { + return type; + } + + public void setType(int type) { + this.type = type; + } + + 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; + } + + @Override + public String toString() { + return "Attence{" + + "aid=" + aid + + ", sid=" + sid + + ", type=" + type + + ", name='" + name + '\'' + + ", time='" + time + '\'' + + '}'; + } +} +//bean_Student +package bean; + +public class Student { + private int id; + private String name; + + public Student() { + } + + public Student(int id, String name) { + this.id = id; + this.name = name; + } + + public int getId() { + return id; + } + + @Override + public String toString() { + return "Student{" + + "id=" + id + + ", name='" + name + '\'' + + '}'; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} +//servlet_add +package servlet; + +import bean.Attence; +import bean.Student; +import util.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +@WebServlet("/add") +public class Add extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String sql = "SELECT * from Student"; + ArrayList list = new ArrayList<>(); + Connection conn = null; + PreparedStatement pst = null; + ResultSet rs = null; + try { + conn = DBUtil.getConn(); + pst = conn.prepareStatement(sql); + rs = pst.executeQuery(); + while (rs.next()) { + int sid = rs.getInt(1); + String name = rs.getString(2); + Student student = new Student(sid, name); + list.add(student); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + try { + DBUtil.close(conn, pst, rs); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + request.setAttribute("list", list); + request.getRequestDispatcher("/WEB-INF/add.jsp").forward(request, response); + } + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} +//servlet_list +package servlet; + +import bean.Attence; +import util.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +@WebServlet("/list") +public class list extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String sql = "SELECT * from Attence A,Student stu where A.sid = stu.sid"; + ArrayList list = new ArrayList<>(); + Connection conn = null; + PreparedStatement pst = null; + ResultSet rs = null; + try { + conn = DBUtil.getConn(); + pst = conn.prepareStatement(sql); + rs = pst.executeQuery(); + while (rs.next()) { + int aid = rs.getInt(1); + int sid = rs.getInt(4); + int type = rs.getInt(3); + String name = rs.getString(6); + String time = rs.getString(2); + Attence attence = new Attence(aid, sid, type, name, time); + list.add(attence); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + try { + DBUtil.close(conn, pst, rs); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + request.setAttribute("list", list); + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request, response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} +//servlet_save +package servlet; + +import com.sun.org.apache.xerces.internal.xs.XSIDCDefinition; +import util.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.Connection; +import java.sql.Date; +import java.sql.PreparedStatement; +import java.sql.SQLException; + +@WebServlet("/sava") +public class Sava extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8");//乱码 + //获取表单 + String name = request.getParameter("name"); + String date = request.getParameter("date"); + int type = request.getIntHeader("type"); + String sql = "insert into Attence values(?,?,?,?)"; + int i = 0; + Connection conn = null; + PreparedStatement pst = null; + try { + conn = DBUtil.getConn(); + pst = conn.prepareStatement(sql); + pst.setInt(1, 0); + pst.setDate(2, Date.valueOf(date)); + pst.setInt(3, type); + pst.setInt(4, Integer.parseInt(name)); + i = pst.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + try { + DBUtil.close(conn, pst, null); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + if (i > 0) { + request.setAttribute("msg","添加成功"); + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + } else { + request.setAttribute("msg","添加失败"); + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + } + } +} +//util_DBUtil +package util; + +import java.sql.*; + +public class DBUtil { + static String url = "jdbc:mysql:///AttDB?characterEncoding=utf8"; + static String username = "root"; + static String pwd = "123456"; + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + System.out.println("注册驱动异常"); + throw new RuntimeException(e); + } + } + public static Connection getConn() throws SQLException { + Connection conn = DriverManager.getConnection(url, username, pwd); + return conn; + } + public static void close(Connection conn, PreparedStatement pst, ResultSet rs) throws SQLException { + if (rs !=null){ + rs.close(); + } if (pst !=null){ + pst.close(); + } if (conn !=null){ + conn.close(); + } + } +} +``` + +```jsp +//add,jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-05 + Time: 11:12 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + +
+ +

学生考勤系统

+ + + + + + + + + + + + + + + + +
学生姓名 + +
考勤时间
考勤状况 + 已到 + 迟到 + 旷课 +
+
+ + +//list.jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-05 + Time: 10:28 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + + + + + + + + + + + + + + + + + + +
考勤编号学生编号学生姓名出勤时间出勤状况
${att.aid}${att.sid}${att.name}${att.time} + + 已到 + + + 迟到 + + + 旷课 + +
+ + +//msg.jsp +<%-- + Created by IntelliJ IDEA. + User: y + Date: 2023/6/6 + Time: 21:58 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 友情提醒 + + +${msg} +
+返回列表 + + +``` + -- Gitee