From 24eae5b96203bd85234511df06f050ac470e4218 Mon Sep 17 00:00:00 2001 From: q1w1e1r1 <1963831974@qq.com> Date: Fri, 9 Jun 2023 19:12:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...44\351\271\217\344\275\234\344\270\232.md" | 382 ++++++++++++++++++ 1 file changed, 382 insertions(+) create mode 100644 "12 \350\213\217\344\273\244\351\271\217/20230607_12 \350\213\217\344\273\244\351\271\217\344\275\234\344\270\232.md" diff --git "a/12 \350\213\217\344\273\244\351\271\217/20230607_12 \350\213\217\344\273\244\351\271\217\344\275\234\344\270\232.md" "b/12 \350\213\217\344\273\244\351\271\217/20230607_12 \350\213\217\344\273\244\351\271\217\344\275\234\344\270\232.md" new file mode 100644 index 0000000..b31427e --- /dev/null +++ "b/12 \350\213\217\344\273\244\351\271\217/20230607_12 \350\213\217\344\273\244\351\271\217\344\275\234\344\270\232.md" @@ -0,0 +1,382 @@ +Bean + +```java +package House; + +public class HouseInfo { + private int id; + private String lease_mode; + private double rent; + private String contacts; + private String deposit_method; + private int house_type_id; + private String address; + + public HouseInfo() { + } + + public HouseInfo(int id, String lease_mode, double rent, String contacts, String deposit_method, int house_type_id, String address) { + 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; + } + + 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(int house_type_id) { + this.house_type_id = house_type_id; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } +} +package House; + +public class HouseType { + private int id; + private String 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; + } +} + +``` + +DAO + +```java +package DAO; + +import House.HouseInfo; +import House.HouseType; +import Util.DBUtil; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +public class HouseDao { + public List getAllHouseType(){ + ArrayList TypeList = new ArrayList<>(); + String sql = "select * from house_type"; + ResultSet rs = DBUtil.Query(sql); + try { + while (rs.next()){ + int id = rs.getInt("id"); + String type = rs.getString("type"); + HouseType ht = new HouseType(id, type); + TypeList.add(ht); + } + DBUtil.close(null,null,rs); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return TypeList; + } + public List getAllHouseInfo(){ + ArrayList InfoList = new ArrayList<>(); + String sql = "select * from house_info"; + ResultSet rs = DBUtil.Query(sql); + try { + while (rs.next()){ + int id = rs.getInt("id"); + String leaseMode = rs.getString("lease_mode"); + double rent = rs.getDouble("rent"); + String contacts = rs.getString("contacts"); + String depositMethod = rs.getString("deposit_method"); + int houseTypeId = rs.getInt("house_type_id"); + String address = rs.getString("address"); + HouseInfo hi = new HouseInfo(id, leaseMode, rent, contacts, depositMethod, houseTypeId, address); + InfoList.add(hi); + } + DBUtil.close(null,null,rs); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return InfoList; + } + public HouseType getIdHouseType(String sql,int Hid) { + ResultSet rs = DBUtil.Query(sql, Hid); + HouseType ht = null; + try { + while (rs.next()) { + int id = rs.getInt("id"); + String type = rs.getString("type"); + ht = new HouseType(id, type); + } + DBUtil.close(null,null,rs); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return ht; + } + public HouseInfo getIdHouseInfo(String sql,int Hid){ + ResultSet rs = DBUtil.Query(sql, Hid); + HouseInfo hi = null; + try { + while (rs.next()){ + int id = rs.getInt("id"); + String leaseMode = rs.getString("lease_mode"); + double rent = rs.getDouble("rent"); + String contacts = rs.getString("contacts"); + String depositMethod = rs.getString("deposit_method"); + int houseTypeId = rs.getInt("house_type_id"); + String address = rs.getString("address"); + hi = new HouseInfo(id, leaseMode, rent, contacts, depositMethod, houseTypeId, address); + } + DBUtil.close(null,null,rs); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return hi; + } + public int addHouseType(HouseType ht){ + String sql = "insert into house_type values(null,?)"; + int i = 0; + try { + i = DBUtil.update(sql,ht.getType()); + } catch (Exception e) { + throw new RuntimeException(e); + } + return i; + } + public int addHouseInfo(HouseInfo hi){ + String sql = "insert into house_info values(null,?,?,?,?,?,?)"; + int i = 0; + try { + i = DBUtil.update(sql,hi.getLease_mode(),hi.getRent(),hi.getContacts(),hi.getDeposit_method(),hi.getHouse_type_id(),hi.getAddress()); + } catch (Exception e) { + throw new RuntimeException(e); + } + return i; + } +} + +``` + +Servlet + +```java +package Servlet; + +import DAO.HouseDao; +import House.HouseInfo; +import House.HouseType; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.util.List; + +@WebServlet("/list/*") +public class HouseServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + response.setContentType("text/html;charset=utf-8"); + String pathInfo = request.getPathInfo(); + System.out.println("pathInfo = " + pathInfo); + HouseDao Dao = new HouseDao(); + if (pathInfo == null){ + List Hilist = Dao.getAllHouseInfo(); + request.setAttribute("HiList",Hilist); + request.getRequestDispatcher("WEB-INF/list.jsp").forward(request,response); + } + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +``` + +DBUtil + +```java +package Util; + +import java.sql.*; + +public class DBUtil { + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + private static final String url = "jdbc:mysql:/localhost:3306//test?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String username = "root"; + private static final String pwd = "root"; + + public static Connection getConn() throws SQLException { + Connection conn = DriverManager.getConnection(url, username, pwd); + return conn; + } + public static int update(String sql ,Object...keys){ + int a = 0; + try { + Connection conn = getConn(); + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + } + a = pst.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + if (a>0){ + return a; + } + return 0; + } + public static ResultSet Query(String sql ,Object...keys){ + ResultSet rst = null; + try { + Connection conn = getConn(); + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + } + rst = pst.executeQuery(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return rst; + } + public static void close(Connection conn,PreparedStatement pst,ResultSet rst) throws SQLException { + if (conn != null){ + conn.close(); + } + if (pst != null){ + pst.close(); + } + if (rst != null){ + rst.close(); + } + } +} + +``` + +list + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-07 + Time: 10:20 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 所有房屋信息 + + + + + + + + + + + + + + + + + + + + + + + + +
编号租赁方式租金(元)联系人押金方式房屋类型详细地址
${hi.id}${hi.lease_mode}${hi.rent}${hi.contacts}${hi.deposit_method}${hi.house_type_id}${hi.address}
+ + + +``` + -- Gitee