From 910e8fc7c55bb94c3f2606a7991898a7ff75ccb9 Mon Sep 17 00:00:00 2001 From: unknown <1211012311@qq.com> Date: Tue, 13 Jun 2023 12:29:40 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=A2=E9=BE=99=E5=B3=B0=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230606 \344\275\234\344\270\232.md" | 0 .../20230610 \344\275\234\344\270\232.md" | 678 ++++++++++++++++++ 2 files changed, 678 insertions(+) rename "15 \350\260\242\351\276\231\345\263\260/20230606\344\275\234\344\270\232.md" => "15 \350\260\242\351\276\231\345\263\260/20230606 \344\275\234\344\270\232.md" (100%) create mode 100644 "15 \350\260\242\351\276\231\345\263\260/20230610 \344\275\234\344\270\232.md" diff --git "a/15 \350\260\242\351\276\231\345\263\260/20230606\344\275\234\344\270\232.md" "b/15 \350\260\242\351\276\231\345\263\260/20230606 \344\275\234\344\270\232.md" similarity index 100% rename from "15 \350\260\242\351\276\231\345\263\260/20230606\344\275\234\344\270\232.md" rename to "15 \350\260\242\351\276\231\345\263\260/20230606 \344\275\234\344\270\232.md" diff --git "a/15 \350\260\242\351\276\231\345\263\260/20230610 \344\275\234\344\270\232.md" "b/15 \350\260\242\351\276\231\345\263\260/20230610 \344\275\234\344\270\232.md" new file mode 100644 index 0000000..d81750b --- /dev/null +++ "b/15 \350\260\242\351\276\231\345\263\260/20230610 \344\275\234\344\270\232.md" @@ -0,0 +1,678 @@ +```java +//封装类 +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() { + + } + + 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; + } + + @Override + public String toString() { + return "houseInfo{" + + "id=" + id + + ", leaseMode='" + leaseMode + '\'' + + ", rent=" + rent + + ", contacts='" + contacts + '\'' + + ", depositMethod='" + depositMethod + '\'' + + ", typeID=" + typeID + + ", 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 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; + } +} + + +package bean; + +public class houseType { + private int id; + private String type; + + public houseType() { + + } + + public houseType(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 "houseType{" + + "id=" + id + + ", type='" + type + '\'' + + '}'; + } +} + +//servlet类 +//添加 +package servlet; + +import bean.houseType; +import utlis.DBUtlis; + +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 add extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + String sql = "select * from house_type"; + ResultSet rs = DBUtlis.select(sql); + ArrayList list = new ArrayList<>(); + try { + while (rs.next()){ + int id = rs.getInt("id"); + String type = rs.getString("type"); + houseType houseType = new houseType(id, type); + list.add(houseType); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + req.setAttribute("list",list); + req.getRequestDispatcher("/WEB-INF/add.jsp").forward(req,resp); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + req.setCharacterEncoding("utf-8"); + + String leaseMode = req.getParameter("leaseMode"); + String rent = req.getParameter("rent"); + String contacts = req.getParameter("contacts"); + String depositMethod = req.getParameter("depositMethod"); + String typeID = req.getParameter("typeID"); + String address = req.getParameter("address"); + + String sql = "insert into house_info values (?,?,?,?,?,?,?)"; + int i = DBUtlis.update(sql, 0, leaseMode, rent, contacts, depositMethod, typeID, address); + if (i > 0){ + resp.sendRedirect("/test"); + }else { + req.setAttribute("msg","添加失败"); + req.getRequestDispatcher("/WEB-INF/msg.jsp").forward(req,resp); + } + } +} + +//删除 +package servlet; + +import utlis.DBUtlis; + +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; +@WebServlet("/delete/*") +public class Delete extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + String path = req.getPathInfo(); + System.out.println(path); + int id = Integer.parseInt(path.substring(1)); + System.out.println(id); + String sql = "delete from house_info where id = ?"; + int i = DBUtlis.update(sql, id); + if (i > 0){ + resp.sendRedirect("/test"); + }else{ + req.setAttribute("msg","删除失败"); + req.getRequestDispatcher("/WEB-INF/msg.jsp").forward(req,resp); + } + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPost(req, resp); + } +} + +//查询 +package servlet; + +import bean.houseInfo; +import bean.houseType; +import com.sun.xml.internal.ws.api.ha.HaInfo; +import utlis.DBUtlis; + +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("/test") +public class Select extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + String sql = "select * from house_info i,house_type t where i.house_type_id = t.id"; + ResultSet rs = DBUtlis.select(sql); + ArrayList list = new ArrayList<>(); + try { + while (rs.next()){ + int id = rs.getInt("i.id"); + String leaseMode = rs.getString("lease_mode"); + double rent = rs.getDouble("rent"); + String contacts = rs.getString("contacts"); + String depositMethod = rs.getString("deposit_method"); + int typeID = rs.getInt("house_type_id"); + String address = rs.getString("address"); + String type = rs.getString("type"); + houseInfo houseInfo = new houseInfo(id, leaseMode, rent, contacts, depositMethod, typeID, address, type); + list.add(houseInfo); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + req.setAttribute("list",list); + req.getRequestDispatcher("/WEB-INF/test.jsp").forward(req,resp); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + req.setCharacterEncoding("utf-8"); + String name = req.getParameter("name"); + System.out.println(name); + String sql = "select * from house_info i,house_type t where i.house_type_id = t.id and contacts like ?"; + ResultSet rs = DBUtlis.select(sql,"%" + name + "%"); + ArrayList list = new ArrayList<>(); + try { + while (rs.next()){ + int id = rs.getInt("i.id"); + String leaseMode = rs.getString("lease_mode"); + double rent = rs.getDouble("rent"); + String contacts = rs.getString("contacts"); + String depositMethod = rs.getString("deposit_method"); + int typeID = rs.getInt("house_type_id"); + String address = rs.getString("address"); + String type = rs.getString("type"); + houseInfo houseInfo = new houseInfo(id, leaseMode, rent, contacts, depositMethod, typeID, address, type); + list.add(houseInfo); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + req.setAttribute("list",list); + req.getRequestDispatcher("/WEB-INF/test.jsp").forward(req,resp); + } +} + + +//修改 +package servlet; + +import bean.houseInfo; +import utlis.DBUtlis; + +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("/update/*") +public class update extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + String path = req.getPathInfo(); + System.out.println(path); + int id1 = Integer.parseInt(path.substring(1)); + System.out.println(id1); + String sql = "select * from house_info i,house_type t where i.house_type_id = t.id and i.id = ?"; + ResultSet rs = DBUtlis.select(sql,id1); + ArrayList list = new ArrayList<>(); + try { + while (rs.next()){ + int id = rs.getInt("i.id"); + String leaseMode = rs.getString("lease_mode"); + double rent = rs.getDouble("rent"); + String contacts = rs.getString("contacts"); + String depositMethod = rs.getString("deposit_method"); + int typeID = rs.getInt("house_type_id"); + String address = rs.getString("address"); + String type = rs.getString("type"); + houseInfo houseInfo = new houseInfo(id, leaseMode, rent, contacts, depositMethod, typeID, address, type); + list.add(houseInfo); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + req.setAttribute("list",list); + req.getRequestDispatcher("/WEB-INF/update.jsp").forward(req,resp); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + req.setCharacterEncoding("utf-8"); + String path = req.getPathInfo(); + String id = path.substring(1); + + String leaseMode = req.getParameter("leaseMode"); + String rent = req.getParameter("rent"); + String contacts = req.getParameter("contacts"); + String depositMethod = req.getParameter("depositMethod"); + String typeID = req.getParameter("typeID"); + String address = req.getParameter("address"); + + String sql = "update house_info set lease_mode = ?,rent = ?,contacts = ?,deposit_method = ?,address = ? where id = ?"; + int i = DBUtlis.update(sql, leaseMode, rent,contacts, depositMethod, address, id); + if (i > 0) { + resp.sendRedirect("/test"); + }else { + req.setAttribute("msg","修改失败"); + req.getRequestDispatcher("/WEB-INF/msg.jsp").forward(req,resp); + } + } +} + + +//工具类 +package utlis; + +import java.sql.*; + +public class DBUtlis { + //1.获取数据库连接和用户密码 + private static final String url = "jdbc:mysql:///test?characterEncoding=utf8"; + private static final String username = "root"; + private static final String password = "root"; + + //2.注册驱动 + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + + //3.连接 + public static Connection getConn(){ + Connection conn = null; + try { + conn = DriverManager.getConnection(url, username, password); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return conn; + } + + //4.通用的查询语音 + public static ResultSet select(String sql,Object ...more){ + Connection conn = getConn(); + ResultSet rs = null; + try { + PreparedStatement st = conn.prepareStatement(sql); + for (int i = 0; i < more.length; i++) { + st.setObject((i + 1),more[i]); + } + rs = st.executeQuery(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return rs; + } + + //5.通用的update方法 + public static int update(String sql,Object ...more){ + Connection conn = getConn(); + int num = 0; + try { + PreparedStatement st = conn.prepareStatement(sql); + for (int i = 0; i < more.length; i++) { + st.setObject((i + 1),more[i]); + } + + num = st.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return num; + } + + //6.关闭资源 + public static void close(Connection conn,PreparedStatement st,ResultSet rs){ + try { + if (rs != null){ + rs.close(); + } + if (st != null){ + st.close(); + } + if (conn != null){ + conn.close(); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} + +``` + +```jsp +//添加 +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-10 + Time: 09:56 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 添加信息 + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
租赁方式
租金
联系人
押金方式
房屋类型 + +
详细信息
+
+ + + +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-10 + Time: 10:11 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +${msg} +
+返回主页 + + + +//首页 +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-10 + Time: 09:43 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 所有信息 + + + + +

+ +

姓名:

+ + + + + + + + + + + + + + + + + + + + + + + +
编号租赁方式租金(元)联系人押金方式房屋类型详细地址操作
${info.id}${info.leaseMode}${info.rent}${info.contacts}${info.depositMethod}${info.type}${info.address} + 删除 + 修改 +
+ + + + +//修改 +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-10 + Time: 10:53 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 修改信息 + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
租赁方式
租金
联系人
押金方式
房屋类型
详细地址
+
+
+ + + +``` + -- Gitee