diff --git "a/03 \350\265\226\345\277\203\345\246\215/20230608 \345\221\230\345\267\245\347\256\241\347\220\206.md" "b/03 \350\265\226\345\277\203\345\246\215/20230608 \345\221\230\345\267\245\347\256\241\347\220\206.md" new file mode 100644 index 0000000000000000000000000000000000000000..21f44a5277eb51a09ffc4192df8b79af41d4e6e3 --- /dev/null +++ "b/03 \350\265\226\345\277\203\345\246\215/20230608 \345\221\230\345\267\245\347\256\241\347\220\206.md" @@ -0,0 +1,401 @@ +# 员工管理 + +## mysql + +```mysql +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 + (1,'开发部'), + (2,'测试部'), + (3,'UI部'); + +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) unique, #不允许为空(唯一约束) 联系电话 + PassWord varchar(12) not null, # 密码不允许为空 + DeptID int, # 部门编号外键,关联部门表DeptID字段 + foreign key (DeptID) references Dept (DeptID) +); + +insert into Emp values + (1,'张三','男',25,'1507844454','123',1), + (2,'李四','女',21,'1507444454','123',2), + (3,'王五','男',24,'1507644454','123',3), + (4,'111','男',11,'1507744454','123',1); +``` + +## bean包 + +### Dept类 + +```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; + } +} +``` + +### emp类 + +```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 + + ", deptName='" + deptName + '\'' + + '}'; + } + + 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 int getEmpId() { + return empId; + } + + public void setEmpId(int empId) { + this.empId = empId; + } + + public Emp(int empId, String empName, String sex, int age, String tel, String password, int deptId, String deptName) { + this.empId = empId; + this.empName = empName; + this.sex = sex; + this.age = age; + this.tel = tel; + this.password = password; + this.deptId = deptId; + this.deptName = deptName; + } +} +``` + +## utils包 + +### DBUtil类 + +```java +package utils; + +import java.sql.*; + +public class DBUtil { + private static final String URL="jdbc:mysql:///CompanyManager?characterEncoding=utf8&useSSL=false"; + private static final String USE="root"; + private static final String PWD="root"; + + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + + public static Connection getConn() throws SQLException { + Connection conn = DriverManager.getConnection(URL, USE, PWD); + return conn; + } + + public static ResultSet query(String sql,Object... keys){ + ResultSet rs = null; + try { + Connection conn = getConn(); + 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) { + throw new RuntimeException(e); + } + return rs; + } + + public static int update(String sql,Object... keys){ + int num=0; + try { + Connection conn = getConn(); + 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) { + throw new RuntimeException(e); + } + return num; + } +} +``` + +## servlet包 + +### ListServlet类 + +```java +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 { + String sql="select * from emp e,dept d where e.deptid=d.deptid"; + ResultSet rs = DBUtil.query(sql); + ArrayList list = new ArrayList<>(); + 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 deptName = rs.getString("DeptName"); + int deptId = rs.getInt("DeptId"); + String passWord = rs.getString("PassWord"); + Emp emp = new Emp(empId,empName,sex,age,tel,passWord,deptId,deptName); + list.add(emp); + } + } 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 { + + } +} +``` + +### selectList类 + +```java +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("/select") +public class SelectServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + String name = request.getParameter("name"); + String sql="select * from dept d,emp e where d.deptid=e.deptid and empname like ? "; + ResultSet rs = DBUtil.query(sql, "%" + name + "%"); + ArrayList list = new ArrayList<>(); + 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 deptName = rs.getString("DeptName"); + int deptId = rs.getInt("DeptId"); + String passWord = rs.getString("PassWord"); + Emp emp = new Emp(empId,empName,sex,age,tel,passWord,deptId,deptName); + list.add(emp); + } + } catch (SQLException e) { + e.printStackTrace(); + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response); + } +} +``` + +## jsp网页 + +### list.jsp + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-08 + Time: 16:06 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +
+ 姓名: + +
+
+ + + + + + + + + + + + + + + + + + + +
编号姓名性别年龄电话所属部门
${emp.empId}${emp.empName}${emp.sex}${emp.age}${emp.tel}${emp.deptName}
+ + +``` + diff --git "a/03 \350\265\226\345\277\203\345\246\215/20230609 \346\210\277\345\261\213\347\256\241\347\220\206.md" "b/03 \350\265\226\345\277\203\345\246\215/20230609 \346\210\277\345\261\213\347\256\241\347\220\206.md" new file mode 100644 index 0000000000000000000000000000000000000000..2a9db44605109a572d860261e89e5bb4ee538596 --- /dev/null +++ "b/03 \350\265\226\345\277\203\345\246\215/20230609 \346\210\277\345\261\213\347\256\241\347\220\206.md" @@ -0,0 +1,612 @@ +# 房屋管理 + +## mysql + +```mysql +# 数据库名称:test +create database test charset utf8; +use test; +# 表:house_type (房屋类型表) +create table house_type +( + id int primary key auto_increment, # 编号 主键,自动增长列 + type varchar(50) not null # 房屋类型 不允许为空 +); +# 表:house_info (房源信息表) +# 字段显示 字段名 数据类型 默认值 备注和说明 +create table house_info +( + id int primary key auto_increment, # 编号 主键,自动增长列 + lease_mode varchar(50), # 租赁方式 可以为空 + rent double not null, # 租金 不允许为空 + contacts varchar(20), # 联系人 可以为空 + deposit_method varchar(20), # 押金方式 可以为空 + house_type_id int, # 房屋类型 外键 + address varchar(200) not null, # 详细地址 不允许为空 + foreign key (house_type_id) references house_type (id) +); +insert into house_type values +(null,'两室一厅一卫'), +(null,'三室一厅一卫'), +(null,'三室两厅一卫'); +insert into house_info values +(null,'整租',2300.0,null,'押一付三',1,'地球53区'), +(null,'整租',2200.0,'李四','押一付三',1,'地球53区'), +(null,'整租',3400.0,null,'押一付三',2,'地球51区'), +(null,'合租',800.0,'张三','押一付三',2,'地球52区'), +(null,'合租',690.0,null,'押一付三',3,'地球51区'); +``` + +## bean包 + +### HouseType类 + +```java +package bean; + +public class HouseType { + private int id; + private String type; + + public HouseType() { + } + + public HouseType(int id, String type) { + this.id = id; + this.type = type; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + @Override + public String toString() { + return "HouseType{" + + "id=" + id + + ", type='" + type + '\'' + + '}'; + } +} +``` + +### HouseInfo类 + +```java +package bean; + +public class HouseInfo { + + private int id; // 编号 + private String leaseMode; // 租赁方式 + private double rent; // 租金 + private String contacts; // 联系人 + private String depositMethod; // 押金方式 + private int houseTypeId; // 房屋类型编号 + private String address; // 详细地址 + private String type; // 房屋类型 + + @Override + public String toString() { + return "HouseInfo{" + "id=" + id + ", leaseMode='" + leaseMode + '\'' + ", rent=" + rent + ", contacts='" + contacts + '\'' + ", depositMethod='" + depositMethod + '\'' + ", houseTypeId=" + houseTypeId + ", address='" + address + '\'' + ", type='" + type + '\'' + '}'; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getLeaseMode() { + return leaseMode; + } + + public void setLeaseMode(String leaseMode) { + this.leaseMode = leaseMode; + } + + public double getRent() { + return rent; + } + + public void setRent(double rent) { + this.rent = rent; + } + + public String getContacts() { + return contacts; + } + + public void setContacts(String contacts) { + this.contacts = contacts; + } + + public String getDepositMethod() { + return depositMethod; + } + + public void setDepositMethod(String depositMethod) { + this.depositMethod = depositMethod; + } + + public int getHouseTypeId() { + return houseTypeId; + } + + public void setHouseTypeId(int houseTypeId) { + this.houseTypeId = houseTypeId; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public HouseInfo() { + } + + public HouseInfo(int id, String leaseMode, double rent, String contacts, String depositMethod, int houseTypeId, String address, String type) { + this.id = id; + this.leaseMode = leaseMode; + this.rent = rent; + this.contacts = contacts; + this.depositMethod = depositMethod; + this.houseTypeId = houseTypeId; + this.address = address; + this.type = type; + } +} +``` + +## utils包 + +### DBUtil类 + +```java +package utils; + +import java.sql.*; + +public class DBUtil { + // 所有全员都是static修饰的 + // 1 定义好 url,user,pwd + static String url="jdbc:mysql:///test?characterEncoding=utf8";// 主机,端口,数据库,编码 + static String user = "root"; + static String pwd = "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, pwd); + return conn; + } + // 4 通用的查询方法,接收SQL,返回结果集 + public static ResultSet query(String sql,Object... keys){ + ResultSet rs = null; + try { + // 1 获取链接 + Connection conn = getConn(); + // 2 获取执行SQL的pst + PreparedStatement pst = conn.prepareStatement(sql); + // 3 遍历keys给?号赋值 + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + } + // 4 pst执行sql得到结果集 + rs = pst.executeQuery(); + } catch (SQLException e) { + e.printStackTrace(); + } + // 5 返回结果集 + return rs; + } + // 5 通用的update方法,接收SQL,返回影响的行数(整数) + public static int update(String sql,Object... keys){ + int rs = 0; + try { + // 1 获取链接 + Connection conn = getConn(); + // 2 获取执行SQL的pst + PreparedStatement pst = conn.prepareStatement(sql); + // 3 遍历keys给?号赋值 + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + } + // 4 pst执行sql得到结果集 + rs = pst.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + // 5 返回结果集 + return rs; + } +} +``` + +## servlet包 + +### ListServlet类 + +```java +package servlet; + +import bean.HouseInfo; +import utils.DBUtil; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +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 { + // * 想办法从数据库查询所有房源信息,将用jsp显示 + // 1 编写SQL + String sql = "select * from house_info i,house_type t where i.house_type_id=t.id"; + // 2 将SQL传给工具类的查询方法,得到结果集 + ResultSet rs = DBUtil.query(sql); + // 3 设置一个房源的集合,遍历结果集,将结果封装成房源对象,再将对象添加到集合 + //设置一个房源的集合 + ArrayList list = new ArrayList<>(); + //遍历结果集 + try { + while (rs.next()){ + // 将结果封装成房源对象 + int id = rs.getInt(1); + String mode = rs.getString(2); + double rent = rs.getDouble(3); + String contacts = rs.getString(4); + String method = rs.getString(5); + int typeId = rs.getInt(6); + String address = rs.getString(7); + String typeName = rs.getString(9); + HouseInfo house = new HouseInfo(id, mode, rent, contacts, method, typeId, address, typeName); + //再将对象添加到集合 + list.add(house); + } + } 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 { + // 根据姓名搜索对应的房源 + // 1 处理乱码 + request.setCharacterEncoding("utf-8"); + // 1获取姓名的值 + String user = request.getParameter("user"); + // 1 编写SQL + String sql = "select * from house_info i,house_type t where i.house_type_id=t.id and contacts like ? "; + // 2 将SQL传给工具类的查询方法,得到结果集 + ResultSet rs = DBUtil.query(sql,"%"+user+"%"); + // 3 设置一个房源的集合,遍历结果集,将结果封装成房源对象,再将对象添加到集合 + //设置一个房源的集合 + ArrayList list = new ArrayList<>(); + //遍历结果集 + try { + while (rs.next()){ + // 将结果封装成房源对象 + int id = rs.getInt(1); + String mode = rs.getString(2); + double rent = rs.getDouble(3); + String contacts = rs.getString(4); + String method = rs.getString(5); + int typeId = rs.getInt(6); + String address = rs.getString(7); + String typeName = rs.getString(9); + HouseInfo house = new HouseInfo(id, mode, rent, contacts, method, typeId, address, typeName); + //再将对象添加到集合 + list.add(house); + } + } catch (SQLException e) { + e.printStackTrace(); + } + // 4 把集合添加到request域中 + request.setAttribute("list",list); + // 5 将请求转发给jsp + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response); + } +} +``` + +### AddServlet类 + +```java +package servlet; + +import bean.HouseType; +import utils.DBUtil; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +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 { +// * 想办法从数据库查询所有房源信息,将用jsp显示 + // 1 编写SQL + String sql = "select * from house_type"; + // 2 将SQL传给工具类的查询方法,得到结果集 + ResultSet rs = DBUtil.query(sql); + // 3 设置一个房源的集合,遍历结果集,将结果封装成房源对象,再将对象添加到集合 + //设置一个房源的集合 + ArrayList list = new ArrayList<>(); + //遍历结果集 + try { + while (rs.next()){ + // 将结果封装成房源对象 + int id = rs.getInt(1); + String type = rs.getString(2); + HouseType houseType = new HouseType(id, type); + //再将对象添加到集合 + list.add(houseType); + } + } catch (SQLException e) { + e.printStackTrace(); + } + // 4 把集合添加到request域中 + request.setAttribute("list",list); + // 5 将请求转发给jsp + request.getRequestDispatcher("/WEB-INF/add.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // 1 处理乱码 + request.setCharacterEncoding("utf-8"); + // 2 接收表单数据 + String leaseMode = request.getParameter("leaseMode"); + String rent = request.getParameter("rent"); + String contacts = request.getParameter("contacts"); + String depositMethod = request.getParameter("depositMethod"); + String type = request.getParameter("type"); + String address = request.getParameter("address"); + // 3 编写sql + String sql = "insert into house_info values (?,?,?,?,?,?,?)"; + // 4 调用工具类执行SQL,得到影响的行数 + int i = DBUtil.update(sql, null, leaseMode, rent, contacts, depositMethod, type, address); + // 5 根据影响的行数,做判断提示 + if (i>0){ + // 使用响应的重定向可以直接刷新列表 + response.sendRedirect("/list"); + }else{ + // 在request域中添加失败的提示信息 + request.setAttribute("msg","添加失败"); + // 5 将请求转发给jsp + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + } + } +} +``` + +DeleteServlet + +```java +package servlet; + +import utils.DBUtil; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +@WebServlet("/delete") +public class DeleteServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // 根据id删除对应的房源 + // 1获取ID的值 + String id = request.getParameter("id"); + // 3 编写sql + String sql = "delete from house_info where id=?"; + // 4 调用工具类执行SQL,得到影响的行数 + int i = DBUtil.update(sql, id); + // 5 根据影响的行数,做判断提示 + if (i>0){ + // 使用响应的重定向可以直接刷新列表 + response.sendRedirect("/list"); + }else{ + // 在request域中添加失败的提示信息 + request.setAttribute("msg","删除失败"); + // 5 将请求转发给jsp + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + } + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} +``` + +## JSP网页 + +### list.jsp + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-09 + Time: 14:43 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + + + + + + + + + + + + + + + + + + + + + + + + + +
编号租赁方式租金(元)联系人押金方式房屋类型详细地址操作
${house.id}${house.leaseMode}${house.rent}${house.contacts}${house.depositMethod}${house.type}${house.address} + 删除 +
+
+ 姓名: +
+ + +``` + +add.jsp + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-09 + Time: 15:09 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
租赁方式
租金(元)
联系人
押金方式
房屋类型 + +
详细地址
+
+ + +``` + +### msg.list + +```jsp +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-09 + Time: 15:30 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +${msg} +返回列表 + + +``` +