From 093ecfef8246e33160191cca3dd9c2aff92662cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E6=B8=85=E5=8D=8E?= Date: Fri, 9 Jun 2023 23:37:02 +0800 Subject: [PATCH] =?UTF-8?q?20230607=E4=BD=9C=E4=B8=9A'=20git=20commit=20-m?= =?UTF-8?q?=2020230607=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ; --- .../20230607 test.md" | 595 ++++++++++++++++++ 1 file changed, 595 insertions(+) create mode 100644 "01 \350\213\217\346\270\205\345\215\216/20230607 test.md" diff --git "a/01 \350\213\217\346\270\205\345\215\216/20230607 test.md" "b/01 \350\213\217\346\270\205\345\215\216/20230607 test.md" new file mode 100644 index 0000000..efbbb61 --- /dev/null +++ "b/01 \350\213\217\346\270\205\345\215\216/20230607 test.md" @@ -0,0 +1,595 @@ +# 房屋管理 + +## mysql + +```mysql +create database test charset utf8; +use test; +create table house_type +( + id int primary key auto_increment, # 编号 主键,自动增长列 + type varchar(50) not null # 房屋类型 不允许为空 +); +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 String url="jdbc:mysql:///test?characterEncoding=utf8";// 主机,端口,数据库,编码 + static String user = "root"; + static String pwd = "122319"; + // 注册驱动 + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } + // 获取连接的方法 + public static Connection getConn() throws SQLException { + Connection conn = DriverManager.getConnection(url, user, 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) { + e.printStackTrace(); + } + return rs; + } + // 通用的update方法 + public static int update(String sql,Object... keys){ + int rs = 0; + 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.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + 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} +返回列表 + + +``` \ No newline at end of file -- Gitee