From e495679dea0ecee9aad58a1630665783b1184486 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=88=E7=86=99=E7=A5=A5?= <1817589159@qq.com> Date: Fri, 9 Jun 2023 16:52:21 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230609 \344\275\234\344\270\232.md" | 389 ++++++++++++++++++ 1 file changed, 389 insertions(+) create mode 100644 "15 \346\262\210\347\206\231\347\245\245/20230609 \344\275\234\344\270\232.md" diff --git "a/15 \346\262\210\347\206\231\347\245\245/20230609 \344\275\234\344\270\232.md" "b/15 \346\262\210\347\206\231\347\245\245/20230609 \344\275\234\344\270\232.md" new file mode 100644 index 0000000..9f0d6ab --- /dev/null +++ "b/15 \346\262\210\347\206\231\347\245\245/20230609 \344\275\234\344\270\232.md" @@ -0,0 +1,389 @@ +### bean + +```java +package bean; + +public class HouseInfo { + private int id; + private String lease_mode; + private double rent; + private String contacts; + private String deposit_method; + private String house_type_id; + private String 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 String 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 HouseInfo(int id, String lease_mode, double rent, String contacts, String deposit_method, String 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 HouseInfo() { + } +} + +``` + +```java +package bean; + +public class Type { + 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 Type(int id, String type) { + this.id = id; + this.type = type; + } + + public Type() { + } +} + +``` + + + +### servlet + +```java +package servlet; + +import Util.Jdbc; +import bean.Type; + +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(name = "add", value = "/add") +public class add extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + ArrayList list = new ArrayList<>(); + ResultSet re = Jdbc.select("select * from house_type"); + try { + while (re.next()) { + list.add(new Type(re.getInt(1),re.getString(2))); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + request.setAttribute("list",list); + request.getRequestDispatcher("WEB-INF/add.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + String leaseMode = request.getParameter("lease_mode"); + String rent = request.getParameter("rent"); + String contacts = request.getParameter("contacts"); + String depositMethod = request.getParameter("deposit_method"); + String type = request.getParameter("type"); + String address = request.getParameter("address"); + int i = Jdbc.update("insert into house_info values(null,?,?,?,?,?,?)", leaseMode, rent, contacts, depositMethod, type, address); + if (i>0){ + response.sendRedirect("/select"); + } + } +} + +``` + +```java +package servlet; + +import Util.Jdbc; +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(name = "select", value = "/select") +public class select extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + ResultSet re = Jdbc.select("select * from house_type a,house_info b where a.id=b.house_type_id"); + ArrayList list = new ArrayList<>(); + try { + while (re.next()){ + list.add(new HouseInfo( + re.getInt(3), + re.getString(4), + re.getDouble(5), + re.getString(6), + re.getString(7), + re.getString(2), + re.getString(9))); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + request.setAttribute("list",list); + request.getRequestDispatcher("WEB-INF/index.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +``` + + + +### util + +```java +package Util; + +import java.sql.*; + +public class Jdbc { + static + { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + private static Connection getConn(){ + String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8"; + try { + return DriverManager.getConnection(url,"root","sxxddytdn"); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + public static ResultSet select(String sql ,String...arr){ + try { + PreparedStatement pst = getConn().prepareStatement(sql); + for (int i = 0; i < arr.length; i++) { + pst.setString((i+1),arr[i]); + } + return pst.executeQuery(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + public static int update(String sql ,String...arr){ + try { + PreparedStatement pst = getConn().prepareStatement(sql); + for (int i = 0; i < arr.length; i++) { + pst.setString((i+1),arr[i]); + } + return pst.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} + +``` + + + +### jsp + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: NO.1 + Date: 2023/6/9 + Time: 16:16 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
租赁方式
租金
联系人
押金方式
房屋类型 + +
详细地址
+
+ + + +``` + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: NO.1 + Date: 2023/6/9 + Time: 15:37 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + $Title$ + + + + + + + + + + + + + + + + + + + + + + + + + +
编号租赁方式租金联系人押金方式房屋类型详细地址
${h.id}${h.lease_mode}${h.rent}${h.contacts}${h.deposit_method}${h.house_type_id}${h.address}
+ + + +``` + + + -- Gitee From 48b72636b4b50ec35a71538a07076166ce0b1f37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=88=E7=86=99=E7=A5=A5?= <1817589159@qq.com> Date: Fri, 9 Jun 2023 16:58:05 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230609 \344\275\234\344\270\232.md" | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git "a/15 \346\262\210\347\206\231\347\245\245/20230609 \344\275\234\344\270\232.md" "b/15 \346\262\210\347\206\231\347\245\245/20230609 \344\275\234\344\270\232.md" index 9f0d6ab..d8e6b9c 100644 --- "a/15 \346\262\210\347\206\231\347\245\245/20230609 \344\275\234\344\270\232.md" +++ "b/15 \346\262\210\347\206\231\347\245\245/20230609 \344\275\234\344\270\232.md" @@ -287,15 +287,9 @@ public class Jdbc { Title -
- +
@@ -350,15 +344,9 @@ public class Jdbc { $Title$ - -
租赁方式
+
-- Gitee
编号 租赁方式