diff --git "a/37 \347\216\213\346\231\264/20230609 \344\275\234\344\270\232.md" "b/37 \347\216\213\346\231\264/20230609 \344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..f003b49adcc2ecec42a709b8fb60e307dbc9e255 --- /dev/null +++ "b/37 \347\216\213\346\231\264/20230609 \344\275\234\344\270\232.md" @@ -0,0 +1,512 @@ +工具包 + +```java +package Util; + +import java.sql.*; + +public class DBUtil { + static String url = "jdbc:mysql:///text?characterEncoding=utf8"; + static String user = "root"; + static String pwd = "1234"; + + 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 rs = null; + try { + Connection conn = getConn(); + 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) { + e.printStackTrace(); + } + return rs; + } + + public static int update(String sql,Object ...keys){ + int i1 = 0; + try { + Connection conn = getConn(); + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + } + i1 = pst.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + return i1; + } + + public static void close(Connection conn,PreparedStatement pst,ResultSet rs){ + try { + if (conn!=null){ + conn.close(); + } + if (pst!=null){ + pst.close(); + } + if (rs!=null){ + rs.close(); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } + +} + +``` + + + +bean类 + +```java +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 +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 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; + } + + 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 "HouseInfo{" + + "id=" + id + + ", mode='" + mode + '\'' + + ", rent=" + rent + + ", contacts='" + contacts + '\'' + + ", method='" + method + '\'' + + ", typeId=" + typeId + + ", address='" + address + '\'' + + ", type='" + type + '\'' + + '}'; + } +} + +``` + +list + +```java +package servlet; + +import Util.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 List 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"; + ArrayList list = new ArrayList<>(); + ResultSet rs = null; + try { + rs = DBUtil.query(sql); + while (rs.next()){ + int id = rs.getInt(1); + String mode = rs.getString(2); + int rent = rs.getInt(3); + String contacts = rs.getString(4); + String method = rs.getString(5); + int typeId = rs.getInt(6); + String address = rs.getString(7); + String type = rs.getString(9); + HouseInfo houseInfo = new HouseInfo(id,mode,rent,contacts,method,typeId,address,type); + list.add(houseInfo); + } + } catch (SQLException e) { + e.printStackTrace(); + } + DBUtil.close(null,null,rs); + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response); + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +``` + +add + +```java +package servlet; + +import bean.HouseType; +import dbutil.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("/AddServlet") +public class AddServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + //language=MySQL + String sql = "select * from house_type"; + ArrayList list = new ArrayList<>(); + try { + ResultSet rs = DBUtil.query(sql); + while (rs.next()){ + int id = rs.getInt(1); + String type = rs.getString(2); + HouseType houseType = new HouseType(id, type); + list.add(houseType); + } + } catch (SQLException e) { + e.printStackTrace(); + } + DBUtil.close(null,null,null); + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/add.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +``` + +save + +```java +package servlet; + +import dbutil.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.SQLException; + +@WebServlet("/SaveServlet") +public class SaveServlet 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("utf-8"); + String mode = request.getParameter("mode"); + String rent = request.getParameter("rent"); + String contacts = request.getParameter("contacts"); + String method = request.getParameter("method"); + String id = request.getParameter("id"); + String address = request.getParameter("address"); + System.out.println(mode); + System.out.println(rent); + + String sql ="insert into house_info values (?,?,?,?,?,?,?)"; + + int i = 0; + i = DBUtil.update(sql, "0",mode, rent, contacts, method, id, address); + DBUtil.close(null,null,null); + if (i > 0) { + // response.sendRedirect("/list"); + request.setAttribute("SaveServlet","添加成功"); + request.getRequestDispatcher("/WEB-INF/save.jsp").forward(request,response); + } else { + request.setAttribute("SaveServlet","添加失败"); + request.getRequestDispatcher("/WEB-INF/save.jsp").forward(request,response); + } + } +} + +``` + + + +```html +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Lenovo + Date: 2023/6/8 + Time: 15:36 + 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.type}
+ + + + + +``` + + + +```html +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-07 + Time: 11:35 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
租赁方式
租金
联系人
押金方式
房屋类型
详细地址
+
+ + + + +``` + + + + + +```html +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-07 + Time: 12:36 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +${SaveServlet} +
+返回 + + + +``` +