diff --git "a/08 \345\256\230\346\226\207\350\257\232/20230612 \346\210\277\345\261\213\347\256\241\347\220\206.md" "b/08 \345\256\230\346\226\207\350\257\232/20230612 \346\210\277\345\261\213\347\256\241\347\220\206.md" new file mode 100644 index 0000000000000000000000000000000000000000..b3191889a87c2a4c50a7aee85542baa89ff3b0f9 --- /dev/null +++ "b/08 \345\256\230\346\226\207\350\257\232/20230612 \346\210\277\345\261\213\347\256\241\347\220\206.md" @@ -0,0 +1,552 @@ +## sql语句 + +~~~ sql +数据库名称: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 # 房屋类型 不允许为空 +); + +插入数据 + +insert into house_type values + (null,'两室一厅'), + (null,'三室一厅'), + (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_info values + (null,'整租',20000,'叶子豪','压一付三',2,'翻斗花园'), + (null,'整租',100000,'周富','压一付二',3,'洛杉矶'), + (null,'合租',1000,'肖种凯韩','压一付五',1,'团结里'); +~~~ + +## 标准类 + +## 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; + } + } + + +~~~ + +## HouseType + +~~~ java +package bean; + +public class HouseType { + private int id; // 编号 + private String type; //房屋类型 + + @Override + public String toString() { + return "HouseType{" + "id=" + id + ", 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; + } + + public HouseType() { + } + + public HouseType(int id, String type) { + this.id = id; + this.type = type; + } +} + +~~~ + + ## ListServlet + +~~~ java +package servlet; + +import Util.DBUtil; +import bean.HouseInfo; + +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 { + //1.添加查询语句 + String sql="select * from house_info i,house_type t where i.house_type_id=t.id"; + // 2 调用工具类结果集 + 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 type = rs.getString(9); + HouseInfo houseInfo = new HouseInfo(id, mode, rent, contacts, method, typeId, address, type); + list.add(houseInfo); + } + } catch (SQLException e) { + e.printStackTrace(); + } + //5.把结果集加入request + request.setAttribute("list",list); + //6.把请求转发给jsp + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +~~~ + +## AddServlet + +~~~ java +package servlet; + +import Util.DBUtil; +import bean.HouseType; + +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 { + String sql="select * from house_type"; + ResultSet res = DBUtil.query(sql); + //3.把结果集变成一个集合 + ArrayList list = new ArrayList<>(); + //4.遍历结果集 + try { + while (res.next()){ + int id = res.getInt(1); + String type = res.getString(2); + HouseType houseType = new HouseType(id, type); + list.add(houseType); + } + } catch (SQLException e) { + e.printStackTrace(); + } + //5.把结果集加入request + request.setAttribute("list",list); + //6.把请求转发给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"); + // 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.setAttribute("msg","添加失败"); + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + } + } +} + + + +~~~ + +## DeleteServlet + +~~~ java +package servlet; + +import Util.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +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"); + // 2 编写sql语句 + String sql = "delete from house_info where id=?"; + // 3 调用工具类,执行sql,得到影响的行数 + int i = DBUtil.update(sql, id); + // 4 根据影响的行数,进行判断提示 + if (i>0){ + response.sendRedirect("/list"); + }else{ + request.setAttribute("msg","删除失败"); + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + } + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} +~~~ + +## SearchServlet + +~~~ java +package servlet; + +import Util.DBUtil; +import bean.HouseInfo; + +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 username = request.getParameter("username"); + String sql = "select * from house_info i,house_type t where i.house_type_id=t.id and contacts like ?"; + // 2 调用DBUtil工具类 + ResultSet rs = DBUtil.query(sql, "%" + username + "%"); + // 3 将结果集变成一个集合 + ArrayList list = new ArrayList<>(); + // 3.5 遍历list集合,将元素封装成一个对象,添加到集合里 + 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 type = rs.getString(9); + HouseInfo houseInfo = new HouseInfo(id, mode, rent, contacts, method, typeId, address, type); + list.add(houseInfo); + } + } catch (SQLException e) { + e.printStackTrace(); + } + // 4 将集合添加到request中 + request.setAttribute("list",list); + // 5 请求转发到jsp + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response); + } +} +~~~ + +## jsp + +list + +~~~ java +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: cihan + Date: 2023/6/12 + Time: 12:26 + 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 + +~~~ java +<%-- jsp --%> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 房屋管理系统 + + +
+ +
+
+
+ + + 姓名: + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + +
编号租赁方式租金(元)联系人押金方式房屋类型详细地址操作
${h.id}${h.leaseMode}${h.rent}${h.contacts}${h.depositMethod}${h.type}${h.address} + 删除 +
+ + + +~~~ + +msg + +~~~ java +<%-- + Created by IntelliJ IDEA. + User: cihan + Date: 2023/6/12 + Time: 16:48 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +${msg} + + + +~~~ +