From 1b109b177b368ce56efd2d01932639ea2e4bbf85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=B5=B7=E7=91=9E?= <3148024859@qq.com> Date: Mon, 12 Jun 2023 23:55:51 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E5=85=AB=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...77\345\261\213\347\256\241\347\220\206.md" | 467 ++++++++++++++++++ 1 file changed, 467 insertions(+) create mode 100644 "50 \345\274\240\350\265\267\347\221\236/20230609 \346\210\277\345\261\213\347\256\241\347\220\206.md" diff --git "a/50 \345\274\240\350\265\267\347\221\236/20230609 \346\210\277\345\261\213\347\256\241\347\220\206.md" "b/50 \345\274\240\350\265\267\347\221\236/20230609 \346\210\277\345\261\213\347\256\241\347\220\206.md" new file mode 100644 index 0000000..96b60fe --- /dev/null +++ "b/50 \345\274\240\350\265\267\347\221\236/20230609 \346\210\277\345\261\213\347\256\241\347\220\206.md" @@ -0,0 +1,467 @@ +建库建表 +create database test charset utf8; +use test; +create table house_type +( + id int primary key auto_increment, # 编号 主键,自动增长列 + type varchar(50) not 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) not null, # 押金方式 可以为空 + house_type_id int, # 房屋类型 外键 + address varchar(200) not null, # 详细地址 不允许为空 + foreign key (house_type_id) references house_type (id) +); +insert into house_type values + (0,'2室1厅1卫'), + (0,'3室1厅1卫'), + (0,'3室2厅1卫'); +insert into house_info values + (0,'整租',2300,null,'押一付一',1,'沙湖港北路166号,距离地铁4号线园林路站1452米 沙湖港湾B区'), + (0,'整租',3700,'王晓明','押一付三',1,'武昌·傅家坡 紫阳东路6号省国资委小区'), + (0,'整租',2500,null,'押一付三',3,'汉阳王家湾,距离地铁4号线玉龙路站491米王家湾中央生活区'), + (0,'合租',850,'张晓菲','押一付一',3,'兴业路后湖公寓'), + (0,'合租',590,null,'押一付一',2,'长虹桥58号南湖花园'); + +``` + +# bean +package bean; + +public class Info { + private int id; + private String mode; + private double rent; + private String contacts; + private String method; + private int typeid; + private String address; + private String type; + + public Info(int id, String mode, double rent, String contacts, String method, int typeid, String address, String type) { + this.id = id; + this.mode = mode; + this.rent = rent; + this.contacts = contacts; + this.method = method; + this.typeid = typeid; + this.address = address; + this.type = type; + } + + public Info() { + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getMode() { + return mode; + } + + public void setMode(String mode) { + this.mode = 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 getMethod() { + return method; + } + + public void setMethod(String method) { + this.method = method; + } + + 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; + } + + @Override + public String toString() { + return "Info{" + + "id=" + id + + ", mode='" + mode + '\'' + + ", rent=" + rent + + ", contacts='" + contacts + '\'' + + ", method='" + method + '\'' + + ", typeid=" + typeid + + ", address='" + address + '\'' + + ", type='" + type + '\'' + + '}'; + } +} + +``` + +```java +package bean; + +public class Type { + private int id; + private String type; + + public Type(int id, String type) { + this.id = id; + this.type = type; + } + + public 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 "Type{" + + "id=" + id + + ", type='" + type + '\'' + + '}'; + } +} + +``` + +# utils + +```java +package utils; + +import java.sql.*; + +public class DBUtil { + static String url = "jdbc:mysql:///test?characterEncoding=utf8"; + static String use = "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, use, pwd); + return conn; + } + public static ResultSet qurey(String sql,Object ...keys){ + ResultSet re = null; + try { + Connection conn = getconn(); + PreparedStatement pre = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pre.setObject((i+1),keys[i]); + } + re = pre.executeQuery(); + } catch (SQLException e) { + e.printStackTrace(); + } + return re; + } + public static int update(String sql,Object ...keys){ + int re = 0; + try { + Connection conn = getconn(); + PreparedStatement pre = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pre.setObject((i+1),keys[i]); + } + re = pre.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + return re; + } +} + +``` + +# servlet + +```java +package servlet; + +import bean.Info; +import bean.Type; +import utils.DBUtil; + +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 re = DBUtil.qurey(sql); + ArrayList list = new ArrayList<>(); + try { + while (re.next()){ + int id = re.getInt(1); + String type = re.getString(2); + Type tp = new Type(id, type); + list.add(tp); + } + } 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 mode = request.getParameter("mode"); + String rent = request.getParameter("rent"); + String contacts = request.getParameter("contacts"); + String method = request.getParameter("method"); + String tp = request.getParameter("type"); + String address = request.getParameter("address"); + String sql = "insert into house_info values (?,?,?,?,?,?,?)"; + int update = DBUtil.update(sql, 0, mode, rent, contacts, method, tp, address); + if (update>0){ + response.sendRedirect("/all"); + } else { + request.setAttribute("nsg","添加失败"); + request.getRequestDispatcher("/WEB-INF/msg.jsp"); + } + } +} + +``` + +```java +package servlet; + +import bean.Info; +import utils.DBUtil; + +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("/all") +public class ListServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String sql = "select * from house_info i,house_type t where i.house_type_id =t.id"; + ResultSet re = DBUtil.qurey(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); + Info info = new Info(id, mode, rent, contacts, method, typeid, address, type); + list.add(info); + } + } catch (SQLException e) { + e.printStackTrace(); + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/all.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +``` + +# jsp + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-12 + Time: 20:09 + 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: Administrator + Date: 2023-06-12 + Time: 19:51 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + + + + + + + + + + + + + + + + + + + + + + + +
编号租赁方式租金(元)联系人押金方式房屋类型详细地址操作空间
${type.id}${type.mode}${type.rent}${type.contacts}${type.method}${type.type}${type.address}
+ + + +``` + +```jsp +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-12 + Time: 20:46 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +${msg} + + +``` + -- Gitee