From 3b3678be736db0d347b048435047eb738b344da3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E5=8E=9A=E8=BE=B0?= Date: Sat, 10 Jun 2023 23:29:38 +0800 Subject: [PATCH] eighth --- ...\270\232 House\350\257\225\345\215\267.md" | 473 ++++++++++++++++++ 1 file changed, 473 insertions(+) create mode 100644 "53 \345\221\250\345\216\232\350\276\260/20230610 \347\254\254\344\270\203\345\205\253\346\254\241\344\275\234\344\270\232 House\350\257\225\345\215\267.md" diff --git "a/53 \345\221\250\345\216\232\350\276\260/20230610 \347\254\254\344\270\203\345\205\253\346\254\241\344\275\234\344\270\232 House\350\257\225\345\215\267.md" "b/53 \345\221\250\345\216\232\350\276\260/20230610 \347\254\254\344\270\203\345\205\253\346\254\241\344\275\234\344\270\232 House\350\257\225\345\215\267.md" new file mode 100644 index 0000000..6a06023 --- /dev/null +++ "b/53 \345\221\250\345\216\232\350\276\260/20230610 \347\254\254\344\270\203\345\205\253\346\254\241\344\275\234\344\270\232 House\350\257\225\345\215\267.md" @@ -0,0 +1,473 @@ +# 20230610 第七八次作业 House试卷 + +## bean包 + +### **houseType**类 + +```java +package Bean; + +public class houseType { + private String id, type; + + public String getId() { + return id; + } + + public String getType() { + return type; + } + + public houseType() { + } + + public houseType(String id, String type) { + this.id = id; + this.type = type; + } +} + +``` + +### houseInfo类 + +```java +package Bean; + +public class houseInfo { + private String id, lease_mode, rent, contacts, deposit_method, house_type_id, address, type; + + @Override + public String toString() { + return "houseInfo{" + + "id='" + id + '\'' + + ", lease_mode='" + lease_mode + '\'' + + ", rent='" + rent + '\'' + + ", contacts='" + contacts + '\'' + + ", deposit_method='" + deposit_method + '\'' + + ", house_type_id='" + house_type_id + '\'' + + ", address='" + address + '\'' + + ", type='" + type + '\'' + + '}'; + } + + public houseInfo(String id, String lease_mode, String 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 String getId() { + return id; + } + + public String getLease_mode() { + return lease_mode; + } + + public String getRent() { + return rent; + } + + public String getContacts() { + return contacts; + } + + public String getDeposit_method() { + return deposit_method; + } + + public String getHouse_type_id() { + return house_type_id; + } + + public String getAddress() { + return address; + } + + public String getType() { + return type; + } + + public houseInfo(String id, String lease_mode, String 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; + } +} + +``` + +## utils包 + +### DBUtil类 + +```java +package Utils; + +import com.mysql.jdbc.Connection; + +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +public class DBUtil { + private static final String Driver = "com.mysql.jdbc.Driver"; + private static final String URL = "jdbc:mysql:///test?useSSL=false&useUnicode=true&Encoding=utf8"; + private static final String Username = "root"; + private static final String Paw = "root"; + + static { + try { + Class.forName(Driver); + } catch (ClassNotFoundException e) { + System.out.println("注册驱动失败"); + e.printStackTrace(); + } + } + + public static Connection conn() { + Connection connection = null; + try { + connection = (Connection) DriverManager.getConnection(URL, Username, Paw); + } catch (SQLException e) { + System.out.println("连接失败"); + e.printStackTrace(); + } + return connection; + } + + public static ResultSet query(String sql, Object... keys) { + ResultSet rs = null; + try { + PreparedStatement pst = conn().prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setObject((i + 1), keys[i]); + } + rs = pst.executeQuery(); + } catch (SQLException e) { + System.out.println("查询异常"); + e.printStackTrace(); + } + return rs; + } + + public static int update(String sql, Object... keys) { + int num = 0; + try { + PreparedStatement pst = conn().prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setObject((i + 1), keys[i]); + } + num = pst.executeUpdate(); + } catch (SQLException e) { + System.out.println("更新异常"); + e.printStackTrace(); + } + return num; + } +} + +``` + +## dao包 + +### Dao类 + +```java +package Dao; + +import Bean.houseInfo; +import Bean.houseType; +import Utils.DBUtil; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +public class Dao { + private static String sql; + + public static ArrayList query() { + sql = "select * from houseInfo i ,housetype t where house_type_id=t.id"; + ResultSet rs = DBUtil.query(sql); + ArrayList list = new ArrayList<>(); + try { + while (rs.next()) { + String id = rs.getString("id"); + String leaseMode = rs.getString("lease_mode"); + String rent = rs.getString("rent"); + String contacts = rs.getString("contacts"); + String depositMethod = rs.getString("deposit_method"); + String houseTypeId = rs.getString("house_type_id"); + String address = rs.getString("address"); + String type = rs.getString("type"); + list.add(new houseInfo(id, leaseMode, rent, contacts, depositMethod, houseTypeId, address, type)); + System.out.println(list); + } + } catch (SQLException e) { + System.out.println("打印异常"); + e.printStackTrace(); + } + return list; + } + + public static ArrayList query2() { + sql = "select * from houseType"; + ResultSet rs = DBUtil.query(sql); + ArrayList list = new ArrayList<>(); + try { + while (rs.next()) { + String id = rs.getString("id"); + String type = rs.getString("type"); + list.add(new houseType(id, type)); + } + } catch (SQLException e) { + System.out.println("打印异常"); + e.printStackTrace(); + } + return list; + } + + public static int add(String... keys) { + sql = "insert into houseinfo values (?,?,?,?,?,?,?)"; + int i = DBUtil.update(sql, keys); + return i; + } +} + +``` + +## servlet包 + +### list类 + +```java +package Servlet; + +import Bean.houseInfo; +import Bean.houseType; +import Dao.Dao; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.ArrayList; + +@WebServlet(name = "list", value = "/house/*") +public class list extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String path = request.getPathInfo(); + if (path == null || path.equals("/list")) { + ArrayList list = Dao.query(); + request.setAttribute("list", list); + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request, response); + System.out.println("传值成功"); + } else if (path.equals("/add")) { + ArrayList list = Dao.query2(); + request.setAttribute("list", list); + request.getRequestDispatcher("/WEB-INF/add.jsp").forward(request, response); + } + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String path = request.getPathInfo(); + request.setCharacterEncoding("utf-8"); + response.setContentType("text/html"); + response.setCharacterEncoding("utf-8"); + PrintWriter out = response.getWriter(); + if (path.equals("/save")) { + String leaseMode = request.getParameter("lease_mode"); + String rent = request.getParameter("rent"); + String contacts = request.getParameter("contacts"); + String depositMethod = request.getParameter("deposit_method"); + String house_type_id = request.getParameter("house_type_id"); + String address = request.getParameter("address"); + + int i = Dao.add(null, leaseMode, rent, contacts, depositMethod, house_type_id, address); + if (i > 0) { + request.setAttribute("msg", "添加成功"); + } else { + request.setAttribute("msg", "添加失败"); + } + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request, response); + System.out.println("传值成功"); + } + } +} + +``` + +## 网页 + +### 显示页面list + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-08 + Time: 15:08 + To change this template use File | Settings | File Templates. +--%> +<%@ 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}
+ + + +``` + +### Add页面 + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-08 + Time: 15:40 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 添加房源 + + + +

添加房源

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
租凭方式
租金
联系人
押金方式
房屋类型 + +
详细地址
+ +
+
+ + + +``` + +### Msg页面 + +```jsp +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-08 + Time: 16:04 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 提示 + + +

${msg}

+ + + +``` + -- Gitee