From 80ead9fa99a20628a4934fcf65ab0d9328383344 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=8F=AD=E9=98=B3=E4=B8=BD?= <2431466589@qq.com> Date: Thu, 8 Jun 2023 12:20:38 +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 --- ...73\347\273\237\344\275\234\344\270\232.md" | 457 ++++++++++++++++++ 1 file changed, 457 insertions(+) create mode 100644 "37 \346\217\255\351\230\263\344\270\275/\350\200\203\345\213\244\347\263\273\347\273\237\344\275\234\344\270\232.md" diff --git "a/37 \346\217\255\351\230\263\344\270\275/\350\200\203\345\213\244\347\263\273\347\273\237\344\275\234\344\270\232.md" "b/37 \346\217\255\351\230\263\344\270\275/\350\200\203\345\213\244\347\263\273\347\273\237\344\275\234\344\270\232.md" new file mode 100644 index 0000000..a0e7354 --- /dev/null +++ "b/37 \346\217\255\351\230\263\344\270\275/\350\200\203\345\213\244\347\263\273\347\273\237\344\275\234\344\270\232.md" @@ -0,0 +1,457 @@ +### mysql + +```mysql + + create database AttDB charset utf8; + + use AttDB; + + create table Student ( + sid int primary key auto_increment, + sname varchar(20) unique not null + ); +insert into Student value + (1,'张三'), + (2,'李四'), + (3,'赵六'); + + 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 Attence value + (1,'2022-05-20 08:20:00',1,1), + (2,'2022-05-20 08:20:00',2,2), + (3,'2022-05-20 08:20:00',1,3), + (4,'2022-05-20 08:20:00',3,4); + +select * from Student; + +select * from Attence; + +``` + +## 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() { + } + + 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 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 + '\'' + + '}'; + } +} + +``` + +### Student类 + +```java +package bean; + +public class Student { + private int sid; + public 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 + '\'' + + '}'; + } +} + +``` + +## until包 + +### DBUtil工具类 + +```java +package tool; + +import java.sql.*; + +public class DBUtil { + //数据块的主机名,端口号,数据库名 + private String url="jdbc:mysql:///attdb?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private String user="root"; + private String password="root"; + + //注册驱动 + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + System.out.println("注册驱动异常"); + e.printStackTrace(); + } + } + //获取连接对象 + public static Connection getConn(){ + Connection conn = null; + try { + conn = DriverManager.getConnection("url", "user", "password"); + } catch (SQLException e) { + e.printStackTrace(); + System.out.println("连接对象失败"); + } + return conn; + } + //通用查询方法 + public static ResultSet query(String sql,Object... keys){ + ResultSet res =null; + try { + //获取连接对象 + Connection conn = getConn(); + //预编译对象 + PreparedStatement pre= conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pre.setObject((i+1),keys[i]); + } + //执行SQL语句 + res = pre.executeQuery(); + } catch (SQLException e) { + e.printStackTrace();//打印错误 + System.out.println("通用查询方法发生错误"); + } + return res; + } + + //通用增加、删除、修改方法 + public static int update(String sql,Object... keys){ + int num =0; + try { + //获取连接对象 + Connection conn = getConn(); + //预编译对象 + PreparedStatement pre= conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pre.setObject((i+1),keys[i]); + } + //执行SQL语句 + num = pre.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace();//打印错误 + System.out.println("通用查询方法发生错误"); + } + return num; + } +} +``` + + + +```java +package all; + +import dao.Attence; +import dao.Student; +import tool.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("/AddServlet") +public class AddServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + //定义SQL语句 + String sql ="select * from student"; + //执行SQL语句 + ResultSet re = DbUTIl.query(sql); + ArrayList list = new ArrayList<>(); + try { + while (re.next()){ + int sid = re.getInt(sid); + String sname = re.getString(sname); + Student stu = new Student(sid, sname); + list.add(stu); + } + } catch (SQLException e) { + e.getStackTrace();//打印错误 + System.out.println("addserviet发生错误"); + } + System.out.println(list);//输出结果 + //将集合转发给all.jsp + request.setAttribute("stuList",list);//将集合放在请求域中 + //跳到一个JSP中 + request.getRequestDispatcher("/WEB-INF/add.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { +//接受表单数据,做处理后,添加到数据库的attence + //处理乱码问题 + request.setCharacterEncoding("utf-8"); + //通过表单的name参数取得表单项的值 + String sid =request.getParameter("sid"); + String time =request.getParameter("time"); + String type =request.getParameter("type"); + //编写SQL语句 + String sql ="insert into attence values (?,?,?,?)"; + int i = DbUTIl.update(sql, null, time, type); + 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 all; + +import dao.Attence; +import tool.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("/stu") +public class SelectServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + ArrayList list = new ArrayList<>(); + //定义SQL语句 + String sql ="select * from attence a ,student s where a.sid=s.sid"; + //执行SQL语句 + ResultSet re = DbUTIl.query(sql); + try { + while (re.next()){ + int aid = re.getInt(1); + int sid = re.getInt(2); + String name = re.getString(3); + String time = re.getString(4); + int type = re.getInt(5); + Attence att = new Attence(aid,sid , name, time, type); + list.add(att); + } + } catch (SQLException e) { + e.getStackTrace();//打印错误 + System.out.println("selectserviet发生错误"); + } + System.out.println(list);//输出结果 + //将集合转发给all.jsp + request.setAttribute("stuList",list);//将集合放在请求域中 + request.getRequestDispatcher("/WEB-INF/all.jsp").forward(request,response); + + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} +``` + + + +```java +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +

添加

+
+ + + + + + + + +<%-- list集合要遍历,需要attence库--%> + + + + + + + + + +
考勤编号学生编号学生姓名出勤时间出勤状况
${student.aid}${student.sid}${student.name}${student.time} + + 已到 + + + 迟到 + + + 早退 + +
+ + +``` + +```java +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 添加考勤 +<%-- 表单--%> + + +

学生考勤系统

+ + + +<%-- 遍历--%> + + + + + + + + + + + + + +
学生姓名
添加时间
考勤状态 + 已到 + 迟到 + 旷课 +
+ + + +``` + +```java +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 提示信息 + + +

${msg}

+ + +``` + -- Gitee