diff --git "a/50 \345\274\240\350\265\267\347\221\236/20230606 \345\255\246\347\224\237\350\200\203\345\213\244\351\241\271\347\233\256.md" "b/50 \345\274\240\350\265\267\347\221\236/20230606 \345\255\246\347\224\237\350\200\203\345\213\244\351\241\271\347\233\256.md" new file mode 100644 index 0000000000000000000000000000000000000000..a90cfc8e0c179ec81ee02a254ec83d47aae41385 --- /dev/null +++ "b/50 \345\274\240\350\265\267\347\221\236/20230606 \345\255\246\347\224\237\350\200\203\345\213\244\351\241\271\347\233\256.md" @@ -0,0 +1,465 @@ +```java +create database AttDB charset utf8; +use AttDB; +create table Student( + sid int primary key auto_increment,# 学号 + sname varchar(20) unique not null # 学生姓名 +); +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 Student values (1,"张三"), + (2,"李四"), + (3,"王五"); +insert into Attence values (1,'2023-10-02 13:30',1,1), + (2,'2023-10-04 13:30',2,2), + (3,'2023-10-03 13:30',3,3); +select * from attence a,student s where a.sid=s.sid; +``` + +```java +工具类: + package utils; + +import java.sql.*; + +public class DBUtil { +// 1. 定义数据库地址 + static String URL = "jdbc:mysql:///AttDB?characterEncoding = UTF8"; + static String USER = "root"; + static String PWD = "root"; + +// 2. 注册驱动 + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } +} +// 3. 获取连接对象 + public static Connection getConn() throws SQLException { + Connection conn = DriverManager.getConnection(URL, USER, PWD); + return conn; + } +// 4.通用的查询 + public static ResultSet query(String sql,Object...keys) { +// 1. 获取连接对象 + ResultSet rs = null; + try { + Connection conn = getConn(); +// 2. 获取执行sql语句对象 + PreparedStatement pst = conn.prepareStatement(sql); +// 2.5 遍历参数 将参数赋值给?号 + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + } +// 3. 执行sql语句 + rs = pst.executeQuery(); + } catch (SQLException e) { + e.printStackTrace(); + } + return rs; + } +// 5.通用的update + public static int update(String sql,Object...keys) { +// 1. 获取连接对象 + int num = 0; + try { + Connection conn = getConn(); +// 2. 获取执行sql语句对象 + PreparedStatement pst = conn.prepareStatement(sql); +// 2.5 遍历参数 将参数赋值给?号 + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + } +// 3. 执行sql语句 + num = pst.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + return num; +} + +} + + +``` + +```java +package bean; + +public class Arrence { + private int aid ; // 考勤编号 + private String time; // 出勤时间 + private int type; // 出勤状况 + private int sid; // 学号 + private String sname; // 姓名 + + public Arrence() { + } + + @Override + public String toString() { + return "Arrence{" + + "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 Arrence(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; + } +} + +``` + +```java +package bean; + +public class Student { + private int sid; // 学号 + private String 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 + '\'' + + '}'; + } + + 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; + } +} + +``` + +```java +package servlet; + +import bean.Arrence; +import bean.Student; +import utils.DBUtil; + +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.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +@WebServlet("/add") +public class AddServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) 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"); + Student Student = new Student( sid, sname); + list.add(Student); + } + } catch (SQLException e) { + e.printStackTrace(); + } +// 将集合添加到 request 域中 + req.setAttribute("list",list); + // 挑转到jsp表单 + req.getRequestDispatcher("/WEB-INF/add.jsp").forward(req,resp); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + req.setCharacterEncoding("utf-8"); + String sid = req.getParameter("sid"); + String time = req.getParameter("time"); + String type = req.getParameter("type"); +// 编写SQL语句 + String sql = "insert into attence values (?,?,?,?)"; + int i = DBUtil.update(sql,null,time,type,sid); + if (i>0){ + req.setAttribute("msg","添加成功"); + req.getRequestDispatcher("/WEB-INF/msg.jsp").forward(req,resp); + }else { + req.setAttribute("msg","添加失败"); + req.getRequestDispatcher("/WEB-INF/msg.jsp").forward(req,resp); + } + + } +} + +``` + +```java +package servlet; + +import bean.Arrence; +import utils.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("/list") +public class ListServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { +// 查询数据库 + String sql = "select * from attence a,student s where a.sid = s.sid order by aid"; + ResultSet rs = DBUtil.query(sql); +// 设置一个集合 + ArrayList list = new ArrayList<>(); +// 将结果封装到集合 + 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"); + Arrence arrence = new Arrence(aid, time, type, sid, sname); + list.add(arrence); + } + } catch (SQLException e) { + e.printStackTrace(); + } +// 将集合添加到 request 域中 + request.setAttribute("list",list); +// 请求转发给一个JSP + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response); + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +``` + +```java +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-06 + Time: 10:19 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + $Title$ + + + $END$ + + + +``` + +```java +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: admin + Date: 2023/6/6 + Time: 16:53 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 添加页面 + + +<%--

学生考勤系统

--%> +
+

学生考勤系统

+ + + + + + + + + + + + + + + + + +
学生姓名 + +
添加时间 + +
考勤状态 + 已到: + 迟到: + 旷课: +
+ +
+
+ + + +``` + +```java +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + + +<%-- 考勤编号”、“学生编号”、“学生姓名”、“出勤时间”、“出勤状况”--%> + + + + + + + + + + + + + + + + +
考勤编号学生编号学生姓名出勤时间出勤状况
${a.aid}${a.sid}${a.sname}${a.time} + + 已到 + + + 迟到 + + + 旷课 + +
+ + + +``` + +```java +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-06 + Time: 18:33 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +

${msg}

+ + + +``` +