From bc745b09f460ffb5c55de804e222fa207d40bb0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=B4=8B?= Date: Tue, 6 Jun 2023 23:10:08 +0800 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 --- .../20230605 attdb.md" | 503 ++++++++++++++++++ 1 file changed, 503 insertions(+) create mode 100644 "48 \346\235\250\346\264\213/20230605 attdb.md" diff --git "a/48 \346\235\250\346\264\213/20230605 attdb.md" "b/48 \346\235\250\346\264\213/20230605 attdb.md" new file mode 100644 index 0000000..3cfa440 --- /dev/null +++ "b/48 \346\235\250\346\264\213/20230605 attdb.md" @@ -0,0 +1,503 @@ +# 数据库 + +```mysql +create database AttDB character set utf8; +use AttDB; + +create table Student( +sid int primary key auto_increment, -- 学号 +sname varchar(20) unique not null -- 学生姓名 +); +insert into Student values(0,'张三'); +insert into Student values(0,'李四'); +insert into Student values(0,'王五'); + +create table Attence( +aid int primary key auto_increment, -- 考勤编号 +time varchar(20) not null, -- 出勤时间 +type int comment '1:已到;2:迟到;3:旷课;', -- 出勤状况 +sid int, -- 学号 +foreign key (sid) references Student(sid) +); +insert into Attence values(0,'2022-05-20 08:20:00',1,1); +insert into Attence values(0,'2022-05-23 08:20:00',2,1); +insert into Attence values(0,'2022-05-23 13:40:00',2,2); +insert into Attence values(0,'2022-05-27 08:20:00',3,2); +insert into Attence values(0,'2022-05-30 08:20:00',2,3); +``` + +# bean + +## attence + +```java +package bean; + +public class Attence { + private int aid; + private String time; + private int type; + private int sid; + private String sname; + + public Attence(int aid, String time, int type, int sid) { + this.aid = aid; + this.time = time; + this.type = type; + this.sid = sid; + } + + @Override + public String toString() { + return "Attence{" + + "aid=" + aid + + ", time='" + time + '\'' + + ", type=" + type + + ", sid=" + sid + + ", sname='" + 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; + } +} + +``` + +## student + +```java +package bean; + +public class Student { + private int sid; + private String sname; + + @Override + public String toString() { + return "Student{" + + "sid=" + sid + + ", 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; + } + + public Student() { + } + + public Student(int sid, String sname) { + this.sid = sid; + this.sname = sname; + } +} + +``` + + # dao + +## attencedao + +```java +package dao; + +import bean.Attence; +import util.DBUtil; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +public class AttenceDao { + //1.获取信息 + public List getAllAttdb(){ + List list = new ArrayList<>(); + String sql="select * from attence a,student s where a.sid=s.sid;"; + ResultSet rs = DBUtil.query(sql); + try { + while (rs.next()){ + int aid = rs.getInt("aid"); + String time = rs.getString("time"); + int type = rs.getInt("type"); + int sid = rs.getInt("sid"); + String sname = rs.getString("sname"); + Attence attence = new Attence(aid, time, type, sid, sname); + list.add(attence); + } + } catch (SQLException e) { + e.printStackTrace(); + } + return list; + } + //2.添加信息 + public int addAttdb(Attence attence){ + String sql="insert into attence values(?,?,?,?)"; + int i=DBUtil.update(sql,attence.getAid(),attence.getTime(),attence.getType(),attence.getSid()); + return i; + } +} + +``` + +## studentdao + +```java +package dao; + +import bean.Student; +import util.DBUtil; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +public class StudentDao { + //获取信息 + public ArrayList getAllStudent(){ + String sql="select * from student"; + ResultSet rs = DBUtil.query(sql); + ArrayList list = new ArrayList<>(); + try { + while (rs.next()){ + int sid = rs.getInt(1); + String sname = rs.getString(2); + Student student = new Student(sid, sname); + list.add(student); + } + } catch (SQLException e) { + e.printStackTrace(); + } + return list; + } +} + +``` + +# servlet + +```java +package servlet; + +import bean.Attence; +import bean.Student; +import dao.AttenceDao; +import dao.StudentDao; +import util.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +@WebServlet("/attdb/*") +public class AttdbController extends HttpServlet { + AttenceDao attDao = new AttenceDao(); + StudentDao stuDao = new StudentDao(); + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + response.setContentType("text/html;charset=utf-8"); + String pathInfo = request.getPathInfo(); + if (pathInfo == null || pathInfo.equals("/")) { + List list = attDao.getAllAttdb(); + request.setAttribute("list", list); + request.getRequestDispatcher("/WEB-INF/attdblist.jsp").forward(request, response); + } else if (pathInfo.equals("/add")) { + List list = stuDao.getAllStudent(); + request.setAttribute("list", list); + request.getRequestDispatcher("/WEB-INF/addattdb.jsp").forward(request, response); + } + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + response.setContentType("text/html;charset=utf-8"); + String pathInfo = request.getPathInfo(); + if (pathInfo.equals("/save")) { + int sid = Integer.parseInt(request.getParameter("sid")); + String time = request.getParameter("time"); + int type = Integer.parseInt(request.getParameter("type")); + Attence attence = new Attence(0, time, type, sid); + int i = attDao.addAttdb(attence); + 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 + +```java +package util; + +import java.sql.*; + +public class DBUtil { + + static String url="jdbc:mysql:///AttDB?characterEncoding=utf-8&useSSL=false&useUnicode=true"; + static String user="root"; + static String password="122319"; + //1.注册驱动 + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } + //2.获取连接 + public static Connection getConn() throws SQLException { + Connection conn = DriverManager.getConnection(url, user, password); + return conn; + } + //3.通用query + 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) { + throw new RuntimeException(e); + } + return rs; + } + //4.通用update + 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; + } + //5.通用close + public static void close(Connection conn,PreparedStatement pst,ResultSet rs){ + try { + if(rs!=null){ + rs.close(); + } + if(pst!=null){ + pst.close(); + } + if(conn!=null){ + conn.close(); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} + +``` + +# jsp + +## add + +```java +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: sqh + Date: 2023/6/6 + Time: 15:39 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 添加 + + +
+

学生考勤系统

+ + + + + + + + + + + + + + + + +
学生姓名 + +
考勤时间
考勤状况 + 已到: + 迟到: + 旷课: +
+
+ + + +``` + +## list + +```java +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: sqh + Date: 2023/6/6 + Time: 15:15 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 学生考勤 + + +添加 + + + + + + + + + + + + + + + + + + +
考勤编号学生编号学生姓名出勤时间出勤状况
${stu.aid}${stu.sid}${stu.sname}${stu.time} + + 已到 + + + 迟到 + + + 旷课 + +
+ + + +``` + +## msg + +```java +<%-- + Created by IntelliJ IDEA. + User: sqh + Date: 2023/6/6 + Time: 17:01 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 提示 + + +

${msg}

+ + + +``` \ No newline at end of file -- Gitee