diff --git "a/29 \350\267\257\347\216\262/20230609 \346\210\277\345\261\213\347\256\241\347\220\206.md" "b/29 \350\267\257\347\216\262/20230609 \346\210\277\345\261\213\347\256\241\347\220\206.md" new file mode 100644 index 0000000000000000000000000000000000000000..faf49349897f38f9cefe47963f3a2066bc5b5e0f --- /dev/null +++ "b/29 \350\267\257\347\216\262/20230609 \346\210\277\345\261\213\347\256\241\347\220\206.md" @@ -0,0 +1,550 @@ +## 数据库 + +```java +create database test charset utf8; +use test; +create table house_type +( + id int primary key auto_increment, # 编号主键,自动增长列 + type varchar(50) not null # 房屋类型 不允许为空 + +); + +#插入数据 +insert into house_type values + (null,'三室一厅'), + (null,'三室二厅'), + (null,'一室一厅'), + (null,'二室一厅'), + (null,'一室二厅'); + +create table house_info +( + id int primary key auto_increment, # 编号主键,自动增长列 + 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 (id) + + +); + +#插入数据 +insert into house_info values + (null,'合租',14654,'张安','现金',1,'北京'), + (null,'自己租',45654564,'张三','微信支付',2,'北京'), + (null,'合租',4985554,'张死','现金',5,'上海'), + (null,'自己租',654,'张强','微信支付',4,'深圳'), + (null,'自己租',1415,'李安','现金',2,'厦门'), + (null,'合租',14654415,'安安','微信支付',3,'广州'); +``` + +## bean + +### info + +```java +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; // 房屋类型 + + //无参构造器 + + 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; + } + + 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; + } + + @Override + public String toString() { + return "HouseInfo{" + + "id=" + id + + ", leasemode='" + leasemode + '\'' + + ", rent=" + rent + + ", contacts='" + contacts + '\'' + + ", depositmethod='" + depositmethod + '\'' + + ", housetypeid=" + housetypeid + + ", address='" + address + '\'' + + ", type='" + type + '\'' + + '}'; + } +} + +``` + +## type + +```java +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 + '\'' + + '}'; + } +} + +``` + +## 工具类 + +```java +package Tils; + +import java.sql.*; + +public class DBUtil { + //定义url,user,pwd + static String url="jdbc:mysql:///test?uesSSL=false&useUnicode+true&characterEncoding=utf8"; + static String user="root"; + static String pwd="root"; + + //创造驱动 + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + e.printStackTrace();//打印错误 + } + } + + //获取连接 + public static Connection getConn() throws SQLException { + Connection conn = DriverManager.getConnection(url, user, pwd); + return conn; + } + + //通用查询方法 +public static ResultSet Query(String sql,Object... keys){ + ResultSet re =null; + //获取连接方法 + try { + Connection conn = getConn(); + //建立预编译对象 + PreparedStatement pst = conn.prepareStatement(sql); + //遍历 + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + } + //执行sql + re = pst.executeQuery(); + } catch (SQLException e) { + e.printStackTrace();//打印错误 + } + return re; +} + + //update方法 + public static int Update(String sql,Object... keys){ + int num =0; + //获取连接方法 + try { + Connection conn = getConn(); + //建立预编译对象 + PreparedStatement pst = conn.prepareStatement(sql); + //遍历 + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + } + //执行sql + num = pst.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace();//打印错误 + } + return num; + } + +} + +``` + +## serviet + +### list + +```java +package serviet; + +import Tils.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("/list") +public class ListServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + //定义sql1语句 + String sql="select * from house_info n,house_type t where n.house_type_id=t.id "; + + //调用工具类 + ResultSet re = DBUtil.Query(sql); + + //创建集合 + ArrayList list = new ArrayList<>(); + + //遍历 + try { + while(re.next()){ + int id = re.getInt(1); + String mode = re.getString(2); + int rent = re.getInt(3); + String contacts = re.getString(4); + String method = re.getString(5); + int typeid = re.getInt(6); + String address = re.getString(7); + String type = re.getString(9); + //一个新对象 + HouseInfo info = new HouseInfo(id, mode, rent, contacts, method, typeid, address, type); + //放入集合中 + list.add(info); + } + } catch (SQLException e) { + e.printStackTrace();//打印错误 + } + //江集合放在requset中 + request.setAttribute("list",list); + + //将数据插入jsp中 + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} +``` + +## add + +```java +package serviet; + +import Tils.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 { + // 编写sql语句 + String sql="select * from house_type"; + //将生气了语句传给工具类 + ResultSet rs = DBUtil.Query(sql); + //设立一个集合额 + ArrayList list = new ArrayList<>(); + + //遍历 + try { + while(rs.next()){ + int id = rs.getInt(1); + String ttype = rs.getString(2); + HouseType type = new HouseType(id, ttype); + list.add(type); + } + } catch (SQLException e) { + e.printStackTrace();//打印错误 + } + //将集合添加到requset域中 + request.setAttribute("list",list); + //将请求转发给JSP中 + request.getRequestDispatcher("").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + //1 处理乱码 + 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"); + //3.编写sql + String sql ="insert into house_info vaules (?,?,?,?,?,?,?)"; + //调用工具类执行sql,得到影响的行数 + int i=DBUtil.Update(sql,leaseMode,rent,contacts,depositmethod,type,address); + //根据影响的行数,做判断提示 + if (i>0) { + // 使用响应的重定向可以直接刷新列表 + response.sendRedirect("/list"); + }else{ + //在request域中添加失败的提示信息 + request.setAttribute("msg","添加失败"); + //将请求转发给JSP中 + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + } + + } +} + +``` + +## JSP + +## list + +```jsp +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + + + + + + + + + + + + + + + + + + + + + + + +
编号租赁方式租金联系人押金方式房屋类型详细地址
${sel.id}${sel.lease_mode}${sel.rent}${sel.contacts}${sel.deposit_method}${sel.type}${sel.address} + 删除 +
+
+ 姓名: + +
+ + + +``` + +## add + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
租赁方式
租金
联系人
押金方式
房屋类型
详细地址
+
+ + +``` + +## delete + +```jsp + +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +

${list}


+ + + +``` + +### msg + +```jsp +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +${msg} + + + +``` + + +