From 9c8f60c881d24cfddf21f7ef72e7eaa43e397454 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=98=E6=96=87=E8=AF=9A?= <3287861587@qq.com> Date: Wed, 7 Jun 2023 23:19:23 +0800 Subject: [PATCH] =?UTF-8?q?=E8=80=83=E5=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...03\345\213\244\347\263\273\347\273\237.md" | 317 ++++++++++++++++++ 1 file changed, 317 insertions(+) create mode 100644 "08 \345\256\230\346\226\207\350\257\232/20230607 \350\200\203\345\213\244\347\263\273\347\273\237.md" diff --git "a/08 \345\256\230\346\226\207\350\257\232/20230607 \350\200\203\345\213\244\347\263\273\347\273\237.md" "b/08 \345\256\230\346\226\207\350\257\232/20230607 \350\200\203\345\213\244\347\263\273\347\273\237.md" new file mode 100644 index 0000000..5d243c7 --- /dev/null +++ "b/08 \345\256\230\346\226\207\350\257\232/20230607 \350\200\203\345\213\244\347\263\273\347\273\237.md" @@ -0,0 +1,317 @@ +### mysql语句 + +~~~ sql +create database AttDB charset utf8; +use AttDB; + + + +create table student +( + sid int primary key auto_increment, #学号 主键,自动增长列 + sname varchar(20) not null unique #学生姓名 唯一,非空 +); + + + +create table Attence +( + aid int primary key auto_increment, # 考勤编号 主键 自动增长列 + time varchar(20) not null , #出勤时间 非空 + type int, # 出勤状况 1:已到;2:迟到;3旷课 + sid int, # 学号 外键 + foreign key (sid) references student(sid) +); + +insert into student values + (null,'子豪'), + (null,'凯汗'), + (null,'呜呜呜龟'); + +insert into attence values + (null,'2023-06-06 10:42:00',1,1), + (null,'2023-06-06 10:42:00',2,1), + (null,'2023-06-06 10:42:00',3,1), + (null,'2023-06-06 10:42:00',2,2), + (null,'2023-06-06 10:42:52',3,2), + (null,'2023-06-06 10:41:27',3,3); +~~~ + + + +### 标准类 + +#### **Attence** + +~~~ java +package bean; + +public class Kaoqin { + 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 Kaoqin() { + } + + public Kaoqin(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 xueshen { + 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 xueshen() { + } + + public xueshen(int sid, String sname) { + this.sid = sid; + this.sname = sname; + } +} + +~~~ + +### 工具类 + +~~~ java +package utiis; + +import java.sql.*; + +public class DBUtil { + // 1. 定义数据库地址,用户名,密码,字符集 + private static final String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String user="root"; + private static final String password="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, password); + return conn; + } + + // 4. 通用的查询方法,接收sql语句 +public static ResultSet query(String sql, Object... keys){ + // 1.获取连接对象 + ResultSet res = null; + try { + Connection conn=getConn(); + //2.获取执行sql的对象 + PreparedStatement pre = conn.prepareStatement(sql); + //2.5 遍历数组,将参数赋值给?号 + for (int i = 0; i < keys.length; i++) { + pre.setObject((i+1),keys[i]); + } + // 3.执行sql,得到一个结果集 + res = pre.executeQuery(); + } catch (SQLException e) { + e.printStackTrace(); + } + + return res; +} + // 5.通用的update,更新,添加,删除 + + public static int update(String sql, Object... keys){ + // 1.获取连接对象 + int num=0; + try { + Connection conn=getConn(); + //2.获取执行sql的对象 + PreparedStatement pre = conn.prepareStatement(sql); + //2.5 遍历数组,将参数赋值给?号 + for (int i = 0; i < keys.length; i++) { + pre.setObject((i+1),keys[i]); + } + // 3.执行sql,得到一个结果集 + num = pre.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + + return num; + } +} + +~~~ + +### ListServlet + +~~~ java +package servlet; + +import utiis.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"; + ResultSet res = DBUtil.query(sql); + // 设置一个集合 + ArrayList list = new ArrayList<>(); + // 将结果封装到集合 + try { + while (res.next()){ + int aid = res.getInt("aid"); + String time = res.getString("time"); + int type = res.getInt("type"); + int sid = res.getInt("sid"); + String sname = res.getString("sname"); + Attence attence = new Attence(aid, time, type, sid, sname); + list.add(attence); + } + } 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 { + + } +} + +~~~ + +### jsp + +~~~java +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 考勤 + + +添加 + + + + + + + + + + + + + + + + + + + +
考勤编号学生编号学生姓名出勤时间出勤状况
${a.aid}${a.sid}${a.sname}${a.time} + + 已到 + + + 迟到 + + + 旷课 + +
+ + + +~~~ + -- Gitee