From ebf0d62d3d2c8421690019ee0018a285a3d03e40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E4=BA=A8=E4=BC=9F?= <529310475@qq.com> Date: Wed, 7 Jun 2023 22:41:06 +0800 Subject: [PATCH] =?UTF-8?q?0607=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Day0607.md" | 305 ++++++++++++++++++ 1 file changed, 305 insertions(+) create mode 100644 "11 \351\202\271\344\272\250\344\274\237/Day0607.md" diff --git "a/11 \351\202\271\344\272\250\344\274\237/Day0607.md" "b/11 \351\202\271\344\272\250\344\274\237/Day0607.md" new file mode 100644 index 0000000..62ecc0d --- /dev/null +++ "b/11 \351\202\271\344\272\250\344\274\237/Day0607.md" @@ -0,0 +1,305 @@ +``` + #数据库 +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 comment '1:已到;2:迟到;3旷课', + sid int, + foreign key (sid) references Student(sid) +); +insert into Student values + (null,"张三"), + (null,"李四"), + (null,"王五"); +insert into Attence values + (null,'2022-05-20 08:20:00',1,1), + (null,'2022-05-20 08:20:00',2,2), + (null,'2022-05-23 13:40:00',3,3); +``` + +``` +//标准类Attence +package bean; + +public class Attence { + int aid; + String time; + int type; + int sid; + String sname; + + @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 +package bean; + +public class Student { + int sid; + 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; + } +} +``` + +``` +//工具类DBUtils +package utils; + +import java.sql.*; + +public class DBUtils { + //定义数据库地址,用户名,密码,字符集 + private static final String url = "jdbc:mysql:///AttDB?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String username = "root"; + private static final String password = "root"; + + //注册驱动 + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } + //获取连接对象的方法 + public static Connection getConn() throws SQLException { + Connection conn = DriverManager.getConnection(url, username, password); + return conn; + } + + //获取通用查询的方法 + public static ResultSet query(String sql, Object... keys) { + //获取连接对象 + ResultSet rs = null; + try { + Connection conn = getConn(); + //获取执行sql语句的对象 + PreparedStatement pst = conn.prepareStatement(sql); + //便利参数,把参数赋值给?号 + for (int i = 0; i < keys.length; i++) { + pst.setObject((i + 1), keys[i]); + } + //执行sql语句 + 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(); + //获取执行sql语句的对象 + PreparedStatement pst = conn.prepareStatement(sql); + //便利参数,把参数赋值给?号 + for (int i = 0; i < keys.length; i++) { + pst.setObject((i + 1), keys[i]); + } + //执行sql语句 + num = pst.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + return num; + } +} +``` + +``` +//显示考勤列表的servlet +package servlet; + +import bean.Attence; +import utils.DBUtils; + +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 inner join student b on a.sid = b.sid;"; + ResultSet rs = DBUtils.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"); + 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--%> + +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 考勤列表 + + +添加 + + + + + + + + + + + + + + + + + +
考勤编号学生编号学生姓名出勤时间出勤状况
${li.aid}${li.sid}${li.sname}${li.time} + + 已到 + + + 迟到 + + + 旷课 + +
+ + + +``` \ No newline at end of file -- Gitee