diff --git "a/06 \351\231\210\345\277\227\344\274\237/20230607 \344\275\234\344\270\232.md" "b/06 \351\231\210\345\277\227\344\274\237/20230607 \344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..944d2863b34a6542ee2fb266d1635d1e376a3771 --- /dev/null +++ "b/06 \351\231\210\345\277\227\344\274\237/20230607 \344\275\234\344\270\232.md" @@ -0,0 +1,426 @@ + # bean + +```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, String sname) { + this.aid = aid; + this.time = time; + this.type = type; + this.sid = sid; + this.sname = sname; + } + + public Attence() { + } + + 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; + } + + @Override + public String toString() { + return "Attence{" + + "aid=" + aid + + ", time='" + time + '\'' + + ", type=" + type + + ", sid=" + sid + + ", 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; + } + + 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; + } + + @Override + public String toString() { + return "Student{" + + "sid=" + sid + + ", sname='" + sname + '\'' + + '}'; + } +} + +``` + +# servlet + +```java +package servlet; + +import bean.Student; +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("/addstu") +public class AddServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + ArrayList list = new ArrayList<>(); + String sql = "select * from student"; + ResultSet re = DBUtil.getquery(sql); + try { + while (re.next()){ + int sid = re.getInt("sid"); + String sname = re.getString("sname"); + Student student = new Student(sid, sname); + list.add(student); + } + } catch (SQLException e) { + e.printStackTrace(); + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/add.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + String sid = request.getParameter("sid"); + String time = request.getParameter("time"); + String dao = request.getParameter("dao"); + System.out.println(sid+" "+time+" "+dao); + String sql = "insert into attence values (?,?,?,?)"; + int i = DBUtil.getupdate(sql, null, time, dao, sid); + 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); + } + } +} + +``` + +```java +package servlet; + +import bean.Attence; +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("/allstu") +public class AllServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + ArrayList list = new ArrayList<>(); + String sql = "select * from attence a,student s where a.sid=s.sid"; + ResultSet re = DBUtil.getquery(sql); + try { + while (re.next()){ + int aid = re.getInt("aid"); + String time = re.getString("time"); + int type = re.getInt("type"); + int sid = re.getInt("sid"); + String sname = re.getString("sname"); + Attence attence = new Attence(aid,time,type,sid,sname); + list.add(attence); + } + } catch (SQLException e) { + e.printStackTrace(); + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/all.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +``` + +# utils + +```java +package utils; + +import java.sql.*; + +public class DBUtil { + private static final String url = "jdbc:mysql:///AttDB?useSSL=false&characterEncoding=utf8"; + private static final String use = "root"; + private static final String password = "root"; + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } + public static Connection getconn() { + Connection conn = null; + try { + conn = DriverManager.getConnection(url, use, password); + } catch (SQLException e) { + e.printStackTrace(); + } + return conn; + } + public static ResultSet getquery(String sql,Object ...keys) { + Connection conn = getconn(); + ResultSet re = null; + try { + PreparedStatement pre = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pre.setObject((i + 1), keys[i]); + } + re = pre.executeQuery(); + } catch (SQLException e) { + e.printStackTrace(); + } + return re; + } + public static int getupdate(String sql,Object ...keys) { + Connection conn = getconn(); + int num = 0; + try { + PreparedStatement pre = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pre.setObject((i + 1), keys[i]); + } + num = pre.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + return num; + } +} + +``` + +# jsp + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-06 + Time: 11:38 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +
+ +

学生考勤系统

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

${msg}

+ + + +``` + +# mysql + +```mysql +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 , + typr int comment '1:已到;2:迟到;3旷课', + sid int, + foreign key (sid) references Student(sid) +); +insert into Student values + (0,"张三"), + (0,"李四"), + (0,"王五"); +insert into Attence values + (0,"2022-05-20 08:20:00",1,1), + (0,"2022-05-23 08:20:00",2,1), + (0,"2022-05-23 13:40:00",2,2), + (0,"2022-05-27 08:20:00",3,2), + (0,"2022-05-30 08:20:00",2,3); +``` +