From 0b455981a05c5207ee2daee24e24bd6a1b87cdac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E9=9B=A8=E6=99=B4?= <2137161545@qq.com> Date: Fri, 9 Jun 2023 16:05:02 +0800 Subject: [PATCH] =?UTF-8?q?JSP=E6=B5=8B=E9=AA=8C=E2=80=94=E2=80=94?= =?UTF-8?q?=E6=88=BF=E5=B1=8B=E4=BF=A1=E6=81=AF=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...41\346\201\257\347\256\241\347\220\206.md" | 496 ++++++++++++++++++ 1 file changed, 496 insertions(+) create mode 100644 "03 \345\276\220\351\233\250\346\231\264/20230607 JSP\346\265\213\351\252\214\342\200\224\342\200\224\346\210\277\345\261\213\344\277\241\346\201\257\347\256\241\347\220\206.md" diff --git "a/03 \345\276\220\351\233\250\346\231\264/20230607 JSP\346\265\213\351\252\214\342\200\224\342\200\224\346\210\277\345\261\213\344\277\241\346\201\257\347\256\241\347\220\206.md" "b/03 \345\276\220\351\233\250\346\231\264/20230607 JSP\346\265\213\351\252\214\342\200\224\342\200\224\346\210\277\345\261\213\344\277\241\346\201\257\347\256\241\347\220\206.md" new file mode 100644 index 0000000..de68815 --- /dev/null +++ "b/03 \345\276\220\351\233\250\346\231\264/20230607 JSP\346\265\213\351\252\214\342\200\224\342\200\224\346\210\277\345\261\213\344\277\241\346\201\257\347\256\241\347\220\206.md" @@ -0,0 +1,496 @@ +```mysql +create database test charset utf8; +use test; +create table house_type( + id int auto_increment primary key, + type varchar(50) not null +); +create table house_info( + id int auto_increment primary key, + lease_mode varchar(50) null, + rent double not null, + contacts varchar(20) null, + deposit_method varchar(20) null, + house_type_id int null, + address varchar(200) not null, + constraint house_info_ibfk_1 + foreign key (house_type_id) references house_type (id) +); +create index house_type_id on house_info (house_type_id); +``` + +```java +package bean; + +public class HouseInfo { + 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 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; + } + + public HouseInfo() { + } + + public HouseInfo(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; + } + + @Override + public String toString() { + return "HouseInfo{" + + "id=" + id + + ", mode='" + mode + '\'' + + ", rent=" + rent + + ", contacts='" + contacts + '\'' + + ", method='" + method + '\'' + + ", typeId=" + typeId + + ", address='" + address + '\'' + + ", type='" + type + '\'' + + '}'; + } +} +``` + +```java +package bean; + +public class HouseType { + private int id; + private String type; + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public HouseType() { + } + + public HouseType(int id, String type) { + this.id = id; + this.type = type; + } + + @Override + public String toString() { + return "HouseType{" + + "id=" + id + + ", type='" + type + '\'' + + '}'; + } +} +``` + +```java +package servlet; + +import bean.HouseType; +import util.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 Add extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + response.setContentType("text/html;charset=utf-8"); + String sql="select * from house_type"; + ResultSet rst = DBUtil.query(sql); + ArrayList list = new ArrayList<>(); + try { + while (rst.next()) { + int id = rst.getInt(1); + String type = rst.getString(2); + HouseType houseType = new HouseType(id, type); + list.add(houseType); + } + request.setAttribute("list",list); + request.getRequestDispatcher("WEB-INF/form.jsp").forward(request,response); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} +``` + +```java +package servlet; + +import bean.HouseInfo; +import util.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("/list") +public class List extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + //1 编写SQL语句,联表查询 + String sql="select * from house_info i,house_type t where i.house_type_id=t.id"; + //2 调用工具类,执行SQ得到L结果集 + ResultSet rst = DBUtil.query(sql); + //3 将结果集封装到集合 + ArrayList list = new ArrayList<>(); + try { + while (rst.next()) { + int id = rst.getInt("i.id"); + String mode = rst.getString(2); + double rent = rst.getDouble(3); + String contacts = rst.getString(4); + String method = rst.getString(5); + int typeId = rst.getInt(6); + String address = rst.getString(7); + String type = rst.getString(9); + HouseInfo info = new HouseInfo(id, mode, rent, contacts, method, typeId, address, type); + list.add(info); + } + } catch (SQLException e) { + e.printStackTrace(); + } + //4 将集合添加到request请求域中 + request.setAttribute("list",list); + //5 将请求转发到list.jsp + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} +``` + +```java +package servlet; + +import util.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.SQLException; + +@WebServlet("/save") +public class Save extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf8"); + response.setContentType("text/html;charset=utf8"); + String id = request.getParameter("id"); + String mode = request.getParameter("mode"); + String rent = request.getParameter("rent"); + String contacts = request.getParameter("contacts"); + String method = request.getParameter("method"); + int type = Integer.parseInt(request.getParameter("type")); + String address = request.getParameter("address"); + String sql = "insert into house_info values (0,?,?,?,?,?,?)"; + int i = 0; + i = DBUtil.update(sql,mode, rent, contacts, method, type, address); + if (i>0){ + response.getWriter().write(""); +// resp.getWriter().write(""); + }else { + response.getWriter().write("添加失败"); + } + } +} +``` + +```java +package util; + +import java.sql.*; + +public class DBUtil { + private static final String url="jdbc:mysql:///test?characterEncoding=utf8"; + private static final String user="root"; + private static final 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 rst = null; + try { + Connection conn = getConn(); + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + } + rst = pst.executeQuery(); + } catch (SQLException e) { + e.printStackTrace(); + } + return rst; + } + + public static int update(String sql,Object... keys){ + int rst = 0; + try { + Connection conn = getConn(); + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + } + rst = pst.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + return rst; + } + + public static void close(Connection conn,PreparedStatement pst,ResultSet rst){ + try { + if (rst != null) { + rst.close(); + } + if (pst != null) { + pst.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-07 + Time: 12:14 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 添加房源 + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
租赁方式 + +
租金 + +
联系人 + +
押金方式 + +
房屋类型 + +
详细地址 + +
+ +
+
+ + +``` + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-07 + Time: 11:16 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 房源列表 + + +
+<%-- --%> + +<%-- --%> + + + + + + + + + + + + + + + + + + + + + + +
编号租赁方式租金联系人押金方式房屋类型详细地址
${h.id}${h.mode}${h.rent}${h.contacts}${h.method}${h.typeId}${h.address}
+
+ + +``` + -- Gitee