diff --git "a/38 \351\273\204\346\255\243\347\204\225/20230607 \346\210\277\345\261\213\347\256\241\347\220\206.md" "b/38 \351\273\204\346\255\243\347\204\225/20230607 \346\210\277\345\261\213\347\256\241\347\220\206.md" new file mode 100644 index 0000000000000000000000000000000000000000..0b5976b050bf6eea60cdf4a2b253898565915e6a --- /dev/null +++ "b/38 \351\273\204\346\255\243\347\204\225/20230607 \346\210\277\345\261\213\347\256\241\347\220\206.md" @@ -0,0 +1,468 @@ +```java +//bean +package bean; + +public class House_Info { + private int id;//编号 + private String lease_mode;//租赁方式 + private double rent;//租金 + private String contacts;//联系人 + private String deposit_method;//押金方式 + private String house_type_id;//房屋类型id + private String address;//详细地址 + private String type;//房型 + + public House_Info() { + } + + public House_Info(int id, String lease_mode, double rent, String contacts, String deposit_method, String house_type_id, String address, String type) { + this.id = id; + this.lease_mode = lease_mode; + this.rent = rent; + this.contacts = contacts; + this.deposit_method = deposit_method; + this.house_type_id = house_type_id; + this.address = address; + this.type = type; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getLease_mode() { + return lease_mode; + } + + public void setLease_mode(String lease_mode) { + this.lease_mode = lease_mode; + } + + 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 getDeposit_method() { + return deposit_method; + } + + public void setDeposit_method(String deposit_method) { + this.deposit_method = deposit_method; + } + + public int getHouse_type_id() { + return house_type_id; + } + + public void setHouse_type_id(String house_type_id) { + this.house_type_id = house_type_id; + } + + 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 "House_Info{" + + "id=" + id + + ", lease_mode='" + lease_mode + '\'' + + ", rent=" + rent + + ", contacts='" + contacts + '\'' + + ", deposit_method='" + deposit_method + '\'' + + ", house_type_id=" + house_type_id + + ", address='" + address + '\'' + + ", type='" + type + '\'' + + '}'; + } +} + +package bean; + +public class House_Type { + private int id; //房型id + private String type; //房型 + + public House_Type() { + } + + public House_Type(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 "House_Type{" + + "id=" + id + + ", type='" + type + '\'' + + '}'; + } +} + +``` + +```java +//servlet +package servlet; + +import bean.House_Info; +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 调用工具类,执行sql语句的到结果集 + ResultSet rs = DBUtil.query(sql); + //3 将结果集封装到集合 + ArrayList list = new ArrayList<>(); + try { + while (rs.next()){ + int iid = rs.getInt("i.id"); + int tid = rs.getInt("t.id"); + String mode = rs.getString("lease_mode"); + double rent = rs.getDouble("rent"); + String contacts = rs.getString("contacts"); + String method = rs.getString("deposit_method"); + String address = rs.getString("address"); + String type = rs.getString("type"); + House_Info info = new House_Info(iid, mode, rent, contacts, method, tid, address, type); + list.add(info); + } + } catch (SQLException e) { + e.printStackTrace(); + } + //4 将结果集放到request请求域中 + request.setAttribute("list",list); + //5 将请求转发给list.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.House_Info; +import bean.House_Type; +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 { + //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("id"); + String type = rs.getString("type"); + House_Type houseType = new House_Type(id,type); + list.add(houseType); + } + } catch (SQLException e) { + e.printStackTrace(); + } + //4 将结果集放到request请求域中 + request.setAttribute("list",list); + //5 将请求转发给list.jsp + request.getRequestDispatcher("/WEB-INF/add.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // 表单是以post方式提交到/add的 + //1处理乱码 + request.setCharacterEncoding("utf-8"); + response.setContentType("text/html;charset=utf-8"); + //2接收表单数据 + String mode = request.getParameter("lease_mode"); + String rent = request.getParameter("rent"); + String contacts = request.getParameter("contacts"); + String deposit_method = request.getParameter("deposit_method"); + String type = request.getParameter("type"); + String address = request.getParameter("address"); + //3编写添加数据库的sql + String sql ="insert into house_info values (0,?,?,?,?,?,?)"; + //4调用工具类,执行sql,并传入表单数据 + int i = DBUtil.update(sql,mode,rent,contacts,deposit_method,type,address); + //5根据执行结果显示提示信息 + if (i>0){ + response.sendRedirect("/list");//添加成功,返回list + }else { + //添加失败的话,就在request域中添加一个msg属性 + request.setAttribute("msg","添加失败"); + //请求转发给msg.jsp,让他显示添加失败的信息. + request.getRequestDispatcher("WEB-INF/msg.jsp").forward(request,response); + } + + + + } +} + +``` + +```java +//util +package utils; + +import java.sql.*; + +public class DBUtil { + //1连接数据库 + 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) { + throw new RuntimeException(e); + } + } + //3获取连接 + public static Connection getCoon() throws SQLException { + Connection conn = DriverManager.getConnection(url, user, pwd); + return conn; + } + //4通用的query 接收sql,返回结果集 + public static ResultSet query(String sql,Object... keys) { + ResultSet rst = null; + try { + //取连接 + Connection coon = getCoon(); + //取执行sql语句对象 + PreparedStatement pst = coon.prepareStatement(sql); + //遍历keys[i] 给?赋值 + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + + } + //执行sql,得到结果集 + rst = pst.executeQuery(); + } catch (SQLException e) { + e.printStackTrace(); + } + //返回结果集 + return rst; + } + //5通用的update 接收sql,返回受影响行数 + public static int update(String sql,Object... keys) { + int rst = 0; + try { + //取连接 + Connection coon = getCoon(); + //取执行sql语句对象 + PreparedStatement pst = coon.prepareStatement(sql); + //遍历keys[i] 给?赋值 + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + + } + //执行sql,得到结果集 + rst = pst.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + //返回结果集 + return rst; + } + //6关闭资源 + public static void close(Connection conn,PreparedStatement pst,ResultSet rst) throws SQLException { + if (rst != null) { + rst.close(); + } + if (pst != null) { + pst.close(); + } + if (conn != null) { + conn.close(); + } + } +} + +``` + +```jsp +//add +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 添加房源 + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
租赁方式
租金
联系人
押金方式
房屋类型 + +
详细地址
+
+ + + +//list +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 房源信息列表 + + + + + + + + + + + + + + + + + + + + + + + + + + + +
编号租赁方式租金联系人押金方式房屋类型详细地址
${house.id}${house.lease_mode}${house.rent}${house.contacts}${house.deposit_method}${house.type}${house.address}
+ + + +//msg +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-07 + Time: 12:42 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 提示信息 + + +

${msg}

+
+返回列表 + + + +``` +