diff --git "a/05 \350\260\242\351\223\226\346\265\251/20230612\346\210\277\345\261\213\347\263\273\347\273\237.md" "b/05 \350\260\242\351\223\226\346\265\251/20230612\346\210\277\345\261\213\347\263\273\347\273\237.md" new file mode 100644 index 0000000000000000000000000000000000000000..f5ef97ead6561d36fc6a6bade79ec9dba0cd0afb --- /dev/null +++ "b/05 \350\260\242\351\223\226\346\265\251/20230612\346\210\277\345\261\213\347\263\273\347\273\237.md" @@ -0,0 +1,520 @@ +```java +create database test charset utf8; +use test; +# (房屋类型表) +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) +); + +// 类 +package bean; + +public class HouseInfo { + + + + + + + + + private int id; // 编号 + private String leaseMode; // 租赁方式 + private double rent; // 租金 + private String contacts; // 联系人 + private String depositMethod; // 押金方式 + private int typeId; // 房屋类型 + private String address; // 详细地址 + private String type; // 房屋类型 + + public HouseInfo(int id, String leaseMode, double rent, String contacts, String depositMethod, int typeId, String address, String type) { + this.id = id; + this.leaseMode = leaseMode; + this.rent = rent; + this.contacts = contacts; + this.depositMethod = depositMethod; + this.typeId = typeId; + this.address = address; + this.type = type; + } + + public HouseInfo() { + } + + 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 getTypeId() { + return typeId; + } + + public void setTypeId(int typeId) { + this.typeId = typeId; + } + + 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; + } + + @Override + public String toString() { + return "HouseInfo{" + + "id=" + id + + ", leaseMode='" + leaseMode + '\'' + + ", rent=" + rent + + ", contacts='" + contacts + '\'' + + ", depositMethod='" + depositMethod + '\'' + + ", typeId=" + typeId + + ", address='" + address + '\'' + + ", type='" + type + '\'' + + '}'; + } +} +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; + } +} +// servlet +package servlet; + +import bean.HouseInfo; +import bean.HouseType; +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 { + // 1 编写sql语句 + String sql = "select * from house_type"; + // 2 调用工具类结果集 + 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.setAttribute("msg","添加失败"); + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + } + } +} + +package servlet; + +import utils.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 { + + } +} + + package servlet; + +import bean.HouseInfo; +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 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(); + } + // 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 { + + } +} + +package servlet; + +import bean.HouseInfo; +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 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 --%> +<%@ 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} + 删除 +
+ + + + +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 添加数据 + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ 租赁方式: + + +
+ 租金(元): + + +
+ 联系人: + + +
+ 押金方式: + + +
+ 房屋类型: + + +
+ 详细地址: + + +
+ +
+
+ + + + +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +${msg} + + +``` \ No newline at end of file