diff --git "a/22 \350\202\226\351\222\237\345\207\257\351\237\251/20230608 \345\221\230\345\267\245\347\256\241\347\220\206\347\263\273\347\273\237.md" "b/22 \350\202\226\351\222\237\345\207\257\351\237\251/20230608 \345\221\230\345\267\245\347\256\241\347\220\206\347\263\273\347\273\237.md" new file mode 100644 index 0000000000000000000000000000000000000000..1a8fa1e3e8b5399dfd50cb0b92ad98b52e6bf67f --- /dev/null +++ "b/22 \350\202\226\351\222\237\345\207\257\351\237\251/20230608 \345\221\230\345\267\245\347\256\241\347\220\206\347\263\273\347\273\237.md" @@ -0,0 +1,417 @@ +~~~ mysql +# CompanyManager数据库 +create database CompanyManager charset utf8; +use CompanyManager; +create table Dept +( + DeptID int primary key auto_increment, # 部门编号 主键,自动增长列 + DeptName varchar(20) not null # 部门名称 不允许为空 + +); +insert into Dept +values (null, '开发部'), + (null, '测试部'), + (null, '实施部'); + +create table Emp +( + EmpID int primary key auto_increment, # 员工编号 主键,自动增长列 + EmpName varchar(20) not null, # 员工姓名 不允许为空 + Sex char(2) not null, # 性别 不允许为空 + Age int not null, # 员工年龄 不允许为空 + Tel varchar(20) not null unique, # 联系电话 不允许为空(唯一约束) + PassWord varchar(12) not null, # 密码 不允许为空 + DeptID int, # 部门编号 外键,关联部门表DeptID字段 + foreign key (DeptID) references Dept (DeptID) +); +insert into Emp +values (null, '张三', '男', 18, '10086', '654123', 1), + (null, '李四', '女', 19, '10087', '321456', 2), + (null, '王五', '男', 20, '10085', '654321', 1), + (null, '赵六', '女', 21, '10088', '123456', 3); +select * from Dept d,Emp e where d.DeptID=e.DeptID; + +~~~ + +~~~ java +// 部门类 +package bean; + +public class Dept { + private int deptId; + private String deptName; + + + + @Override + public String toString() { + return "Dept{" + + "deptId=" + deptId + + ", deptName='" + deptName + '\'' + + '}'; + } + + public int getDeptId() { + return deptId; + } + + public void setDeptId(int deptId) { + this.deptId = deptId; + } + + public String getDeptName() { + return deptName; + } + + public void setDeptName(String deptName) { + this.deptName = deptName; + } + + public Dept() { + } + + public Dept(int deptId, String deptName) { + this.deptId = deptId; + this.deptName = deptName; + } +} + +~~~ + +~~~ java +// 员工类 +package bean; + +public class Emp { + private int empId; + private String empName; + private String sex; + private int age; + private String tel; + private String passWord; + private int deptId; + private String deptName; + + @Override + public String toString() { + return "Emp{" + + "empId=" + empId + + ", empName='" + empName + '\'' + + ", sex='" + sex + '\'' + + ", age=" + age + + ", tel='" + tel + '\'' + + ", passWord='" + passWord + '\'' + + ", deptId=" + deptId + + ", depName='" + deptName + '\'' + + '}'; + } + + public int getEmpId() { + return empId; + } + + public void setEmpId(int empId) { + this.empId = empId; + } + + public String getEmpName() { + return empName; + } + + public void setEmpName(String empName) { + this.empName = empName; + } + + public String getSex() { + return sex; + } + + public void setSex(String sex) { + this.sex = sex; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getTel() { + return tel; + } + + public void setTel(String tel) { + this.tel = tel; + } + + public String getPassWord() { + return passWord; + } + + public void setPassWord(String passWord) { + this.passWord = passWord; + } + + public int getDeptId() { + return deptId; + } + + public void setDeptId(int deptId) { + this.deptId = deptId; + } + + public String getDeptName() { + return deptName; + } + + public void setDeptName(String deptName) { + this.deptName = deptName; + } + + public Emp() { + } + + public Emp(int empId, String empName, String sex, int age, String tel, String passWord, int deptId, String depName) { + this.empId = empId; + this.empName = empName; + this.sex = sex; + this.age = age; + this.tel = tel; + this.passWord = passWord; + this.deptId = deptId; + this.deptName = depName; + } +} + +~~~ + +~~~ java +// 工具类 +package utils; + +import java.sql.*; + +public class DBUtil { + // 1 定义数据库地址,用户名,密码 + private static String url = "jdbc:mysql:///companyManager?useSSL=false&characterEncoding=utf8"; + private static String user = "root"; + private static String passWord = "root"; + + // 2 注册驱动 + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } + + // 3 获取链接对象 + public static Connection getConn(){ + Connection conn = null; + try { + conn = DriverManager.getConnection(url, user, passWord); + } catch (SQLException e) { + e.printStackTrace(); + } + return conn; + } + + // 4 通用的查询方法,接收sql语句,返回结果集 + public static ResultSet query(String sql,Object... keys){ + Connection conn = getConn(); + ResultSet rs = null; + try { + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + } + rs = pst.executeQuery(); + } catch (SQLException e) { + e.printStackTrace(); + } + return rs; + } + + // 5 通用的添加,删除方法,接收sql语句,返回结果集 + public static int update(String sql,Object... keys){ + Connection conn = getConn(); + int num = 0; + try { + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + } + num = pst.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + return num; + } +} + +~~~ + +~~~ java +// ListServlet +package servlet; + +import bean.Emp; +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 { + // 1 编写sql语句 + String sql = "select * from Dept d,Emp e where d.DeptID=e.DeptID"; + // 2 调用DBUtil工具类 + ResultSet rs = DBUtil.query(sql); + // 3 将结果集变成一个集合 + ArrayList list = new ArrayList<>(); + // 3.5 遍历list集合,将元素封装成一个对象,添加到集合里 + try { + while (rs.next()){ + int empId = rs.getInt("empId"); + String empName = rs.getString("empName"); + String sex = rs.getString("sex"); + int age = rs.getInt("age"); + String tel = rs.getString("tel"); + String passWord = rs.getString("passWord"); + int deptId = rs.getInt("deptId"); + String deptName = rs.getString("deptName"); + Emp emp = new Emp(empId, empName, sex, age, tel, passWord, deptId, deptName); + list.add(emp); + } + } catch (SQLException e) { + e.printStackTrace(); + } + // 4 把集合放入request域中 + request.setAttribute("list",list); + // 5 请求转发给jsp + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response); + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +~~~ + +~~~ java +// searchServlet +package servlet; + +import bean.Emp; +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("/search") +public class SearchServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // 0 编写字符集,防止乱码 + request.setCharacterEncoding("utf-8"); + // 1 编写sql语句 + String sql = "select * from Dept d,Emp e where d.DeptID=e.DeptID and empName like ?"; + String username = request.getParameter("username"); + // 2 调用DBUtil工具类 + ResultSet rs = DBUtil.query(sql,"%"+username+"%"); + // 3 将结果集变成一个集合 + ArrayList list = new ArrayList<>(); + // 3.5 遍历list集合,将元素封装成一个对象,添加到集合里 + try { + while (rs.next()){ + int empId = rs.getInt("empId"); + String empName = rs.getString("empName"); + String sex = rs.getString("sex"); + int age = rs.getInt("age"); + String tel = rs.getString("tel"); + String passWord = rs.getString("passWord"); + int deptId = rs.getInt("deptId"); + String deptName = rs.getString("deptName"); + Emp emp = new Emp(empId, empName, sex, age, tel, passWord, deptId, deptName); + list.add(emp); + } + } catch (SQLException e) { + e.printStackTrace(); + } + // 4 把集合放入request域中 + request.setAttribute("list",list); + // 5 请求转发给jsp + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response); + + } +} + +~~~ + +~~~ jsp +// JSP +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 员工管理系统 + + +
+ + + 姓名: + + + + + +
+
+ + + + + + + + + + + + + + + + + + + +
编号姓名性别年龄电话所属部门
${e.empId}${e.empName}${e.sex}${e.age}${e.tel}${e.deptName}
+ + +~~~ + diff --git "a/22 \350\202\226\351\222\237\345\207\257\351\237\251/20230608 \350\200\203\345\213\244\347\256\241\347\220\206\347\263\273\347\273\237.md" "b/22 \350\202\226\351\222\237\345\207\257\351\237\251/20230608 \350\200\203\345\213\244\347\256\241\347\220\206\347\263\273\347\273\237.md" new file mode 100644 index 0000000000000000000000000000000000000000..96e64c0374c64d1167915acd4c9c648225d4449f --- /dev/null +++ "b/22 \350\202\226\351\222\237\345\207\257\351\237\251/20230608 \350\200\203\345\213\244\347\256\241\347\220\206\347\263\273\347\273\237.md" @@ -0,0 +1,462 @@ +~~~ java +package bean; + +public class Student { + int sid; + String sname; + + public Student(int sid, String sname) { + this.sid = sid; + this.sname = sname; + } + + public Student() { + } + + 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 + '\'' + + '}'; + } +} + +~~~ + +~~~ java +package bean; + +public class Attence { + int aid; + String time; + int type; + int sid; + 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 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("/list") +public class ListServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String sql = "select * from student s,attence a where s.sid=a.sid"; + 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"); + Attence a = new Attence(aid, time, type, sid, sname); + list.add(a); + } + } catch (SQLException e) { + e.printStackTrace(); + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +~~~ + +~~~ 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("/add") +public class addServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) 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 stu = new Student(sid, sname); +// list.add(stu); +// } +// } catch (SQLException e) { +// e.printStackTrace(); +// } +// +// request.setAttribute("list",list); +// request.getRequestDispatcher("/WEB-INF/add.jsp").forward(request,response); + ArrayList list = new ArrayList<>(); + request.setCharacterEncoding("utf-8"); + String sql = "select * from student"; + ResultSet rs = DBUtil.query(sql); + try { + while (rs.next()){ + int sid = rs.getInt("sid"); + String sname = rs.getString("sname"); + Student stu = new Student(sid, sname); + list.add(stu); + } + } 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 type = request.getParameter("type"); +// String sql = "insert into attence values (?,?,?,?)"; +// int i = DBUtil.update(sql, null, time, type, 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); +// } + request.setCharacterEncoding("utf-8"); + + String sid = request.getParameter("sid"); + String time = request.getParameter("time"); + String type = request.getParameter("type"); + + String sql = "insert into attence values(?,?,?,?)"; + int i = DBUtil.update(sql, null, time, type, sid); + + if(i>0){ + request.setAttribute("msg","添加成功"); + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + }else{ + request.setAttribute("msg","添加失败"); + } + } +} + + +~~~ + +~~~ java +package utils; + +import java.sql.*; + +public class DBUtil { + private static String url = "jdbc:mysql:///attdb?useSSL=false&characterEncoding=utf8"; + private static String user = "root"; + private static String password = "root"; + + // 2 注册驱动 + static{ + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } + + // 3 获取链接对象 + public static Connection getConn(){ + Connection conn = null; + try { + conn = DriverManager.getConnection(url, user, password); + } catch (SQLException e) { + e.printStackTrace(); + } + return conn; + } + + // 4 新建一个通用的查询方法,接收sql语句,返回结果集 + public static ResultSet query(String sql,Object... keys){ + Connection conn = getConn(); + ResultSet rs = null; + try { + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setObject((1+i),keys[i]); + } + rs = pst.executeQuery(); + } catch (SQLException e) { + e.printStackTrace(); + } + return rs; + } + + // 5 新建一个通用的删除添加方法,接收sql语句,返回结果集 + public static int update(String sql,Object... keys){ + Connection conn = getConn(); + int num = 0; + try { + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setObject((1+i),keys[i]); + } + num = pst.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + return num; + } + +} + +~~~ + +~~~ java +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 添加系统 + + +<%--
--%> +<%--

学生考勤系统

--%> +<%-- --%> +<%-- --%> +<%-- --%> +<%-- --%> +<%-- --%> +<%-- --%> +<%-- --%> +<%-- --%> +<%-- --%> +<%-- --%> +<%-- --%> +<%-- --%> +<%-- --%> +<%-- --%> +<%-- --%> +<%-- --%> +<%--
--%> +<%-- 学生姓名:--%> +<%-- --%> +<%-- --%> +<%--
--%> +<%-- 考勤时间:--%> +<%-- --%> +<%-- --%> +<%--
考勤状况--%> +<%-- 已到:--%> +<%-- 迟到:--%> +<%-- 旷课:--%> +<%--
--%> +<%--
--%> + +
+

学生考勤系统

+ + + + + + + + + + + + + + + + +
学生姓名 + +
考勤时间
考勤情况 + 已到: + 迟到: + 旷课: +
+
+ + + +~~~ + +~~~ jsp +<%@ 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} + + 已到 + + + 迟到 + + + 旷课 + +
+ + + +~~~ + +~~~ jsp +<%-- + Created by IntelliJ IDEA. + User: XR + Date: 2023/6/8 + Time: 12:52 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 提示 + + +

${msg}

+ + + +~~~ +