From c8b67dbed5b6b7ad1de2a8f25da9791333703e45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B0=B8=E6=B7=B3?= <2678158018@qq.com> Date: Sun, 11 Jun 2023 22:10:39 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=83=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...41\347\220\206\347\263\273\347\273\237.md" | 564 ++++++++++++++++++ 1 file changed, 564 insertions(+) create mode 100644 "18 \345\276\220\346\260\270\346\267\263/20230611 jsp\346\210\277\345\261\213\347\256\241\347\220\206\347\263\273\347\273\237.md" diff --git "a/18 \345\276\220\346\260\270\346\267\263/20230611 jsp\346\210\277\345\261\213\347\256\241\347\220\206\347\263\273\347\273\237.md" "b/18 \345\276\220\346\260\270\346\267\263/20230611 jsp\346\210\277\345\261\213\347\256\241\347\220\206\347\263\273\347\273\237.md" new file mode 100644 index 0000000..131a45c --- /dev/null +++ "b/18 \345\276\220\346\260\270\346\267\263/20230611 jsp\346\210\277\345\261\213\347\256\241\347\220\206\347\263\273\347\273\237.md" @@ -0,0 +1,564 @@ +```mysql +create database test charset utf8; +use test; +create table house_type +( + id int auto_increment primary key, # 编号 主键,自动增长列 + type varchar(50) # 房屋类型 不允许为空 +); + +create table house_info +( + id int auto_increment primary key, # 编号 主键,自动增长列 + lease_mode varchar(50), # 租赁方式 可以为空 + rent double not null, # 租金 不允许为空 + contacts varchar(20), # 联系人 可以为空 + deposit_method varchar(20), # 押金方式 可以为空 + house_type_id int, # 房屋类型 外键 + address varchar(200) not null, # 详细地址 不允许为空 + foreign key (house_type_id) references house_type (type) +); + +insert into house_type +values (null,'一室一厅'), + (null,'一室两厅'), + (null,'别墅'); +insert into house_info values (null,null,2342,null,null,1,'是否打开'), + (null,null,1241,null,null,2,'是否打开'), + (null,null,50000000,null,'一次性付款',3,'是否打开'); +``` + + + +```java +bean包 +house_info类 +package bean; + +public class HouseInfo { + private int id; + private String leaseMode; + private double rent; + private String contacts; + private String depositMethod; + private int houseTypeId; + private String address; + private String type; + + @Override + public String toString() { + return "HouseInfo{" + + "id=" + id + + ", leaseMode='" + leaseMode + '\'' + + ", rent=" + rent + + ", contacts='" + contacts + '\'' + + ", depositMethod='" + depositMethod + '\'' + + ", houseTypeId=" + houseTypeId + + ", 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 getHouseTypeId() { + return houseTypeId; + } + + public void setHouseTypeId(int houseTypeId) { + this.houseTypeId = houseTypeId; + } + + 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; + } + + public HouseInfo() { + } + + public HouseInfo(int id, String leaseMode, double rent, String contacts, String depositMethod, int houseTypeId, String address, String type) { + this.id = id; + this.leaseMode = leaseMode; + this.rent = rent; + this.contacts = contacts; + this.depositMethod = depositMethod; + this.houseTypeId = houseTypeId; + this.address = address; + this.type = type; + } +} + +``` + + + +```java +housetype类 +package bean; + +public class HouseType { + private int id; + private String type; + + @Override + public String toString() { + return "HouseType{" + + "id=" + id + + ", 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; + } + + public HouseType() { + } + + public HouseType(int id, String type) { + this.id = id; + this.type = type; + } +} + +``` + + + +```java +DBUtil包 +工具类 +package bean; + +public class HouseType { + private int id; + private String type; + + @Override + public String toString() { + return "HouseType{" + + "id=" + id + + ", 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; + } + + public HouseType() { + } + + public HouseType(int id, String type) { + this.id = id; + this.type = type; + } +} + +``` + + + +```java +servlect包 +HouseInfoservlet类 +package servlect; + +import DBUtil.DBUtil; +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("/select") +public class HouseInfoServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String sql = "select * from house_info i,house_type t where t.id=i.house_type_id"; + ResultSet query = DBUtil.query(sql); + ArrayList list = new ArrayList<>(); + try { + while (query.next()) { + int id = query.getInt(1); + double rent = query.getDouble(3); + int typeId = query.getInt(6); + String leaseMode = query.getString(2); + String contacts = query.getString(4); + String depositMethod = query.getString(5); + String address = query.getString(7); + String type = query.getString(9); + HouseInfo houseInfo = new HouseInfo(id,leaseMode,rent,contacts,depositMethod,typeId,address,type); + list.add(houseInfo); + } + } catch (SQLException e) { + e.printStackTrace(); + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/house.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + String username = request.getParameter("username"); + String sql = "select * from house_info i,house_type t where t.id=i.house_type_id and contacts like ?"; + ResultSet query = DBUtil.query(sql,"%"+username+"%"); + ArrayList list = new ArrayList<>(); + try { + while (query.next()) { + int id = query.getInt(1); + double rent = query.getDouble(3); + int typeId = query.getInt(6); + String leaseMode = query.getString(2); + String contacts = query.getString(4); + String depositMethod = query.getString(5); + String address = query.getString(7); + String type = query.getString(9); + HouseInfo houseInfo = new HouseInfo(id,leaseMode,rent,contacts,depositMethod,typeId,address,type); + list.add(houseInfo); + } + } catch (SQLException e) { + e.printStackTrace(); + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/house.jsp").forward(request,response); + } +} + + +``` + + + +```java +AddServlet类 +package servlect; + +import DBUtil.DBUtil; +import bean.HouseType; + +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("/add") +public class AddServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String sql = "select * from house_type "; + ResultSet query = DBUtil.query(sql); + ArrayList list = new ArrayList<>(); + try { + while (query.next()) { + int id = query.getInt(1); + String type = query.getString(2); + HouseType houseType = new HouseType(id, type); + list.add(houseType); + } + } catch (SQLException e) { + e.printStackTrace(); + } + 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("leaseMode"); + String rent = (request.getParameter("rent")); + String contacts = request.getParameter("contacts"); + String depositMethod = request.getParameter("depositMethod"); + String type = request.getParameter("type"); + String address = request.getParameter("address"); + String sql = "insert into house_info values (?,?,?,?,?,?,?)"; + int i = DBUtil.update(sql, null, leaseMode, rent, contacts, depositMethod, type, address); + System.out.println(i); + if (i > 0) { + response.sendRedirect("/select"); + } else { + request.setAttribute("msg", "添加失败"); + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request, response); + } + } +} +``` + + + +```java +delete类 +package servlect; + +import DBUtil.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; + +@WebServlet("/delete") +public class DeleteServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String id = request.getParameter("id"); + String sql = "delete from house_info where id=?"; + int i = DBUtil.update(sql, id); + System.out.println(i); + if (i > 0) { + response.sendRedirect("/select"); + } else { + request.setAttribute("msg", "失败"); + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request, response); + } + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +``` + + + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: 86173 + Date: 2023/6/11 + Time: 19:38 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + +
+姓名: +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
编号租赁方式租金(元)联系人押金方式房屋类型详细地址操作
${house.id}${house.leaseMode}${house.rent}${house.contacts}${house.depositMethod}${house.type}${house.address} + 删除 +
+ + +``` + + + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: 86173 + Date: 2023/6/11 + Time: 20:07 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
租赁方式 + +
租金(元) + +
联系人 + +
押金方式 + +
房屋类型 + +
详细地址
+ +
+
+ + + +``` + + + +```jsp +<%-- + Created by IntelliJ IDEA. + User: 86173 + Date: 2023/6/11 + Time: 20:43 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +${msg} 返回列表 + + + +``` + -- Gitee