From 5cc75312e50ef57d345cb25f79d1cd7c00d18f40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E4=BF=8A=E4=BC=9F?= <2421084001@qq.com> Date: Mon, 12 Jun 2023 17:30:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\346\254\241\344\275\234\344\270\232.md" | 260 ++++++++++++++++++ 1 file changed, 260 insertions(+) create mode 100644 "12 \346\236\227\344\277\212\344\274\237/0610 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232.md" diff --git "a/12 \346\236\227\344\277\212\344\274\237/0610 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232.md" "b/12 \346\236\227\344\277\212\344\274\237/0610 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232.md" new file mode 100644 index 0000000..cfdedba --- /dev/null +++ "b/12 \346\236\227\344\277\212\344\274\237/0610 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232.md" @@ -0,0 +1,260 @@ +``` +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; + } +} +package bean; + +public class HouseType { + private int id; + + 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; + } + + private String type; + + @Override + public String toString() { + return "HouseType{" + + "id=" + id + + ", type='" + type + '\'' + + '}'; + } +} +package servlet; + +import bean.HouseInfo; +import bean.HouseType; +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; +import java.util.Enumeration; + +@WebServlet("/add") +public class AddServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { +// * 想办法从数据库查询所有房源信息,将用jsp显示 + // 编写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 type = rs.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); + if (i>0){ + response.sendRedirect("/list"); + }else{ + request.setAttribute("msg","添加失败"); + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + } + } +} +package utils; + +import java.sql.*; + +public class DBUtil { + // 所有全员都是static修饰的 + // 1 定义好 url,user,pwd + static String url="jdbc:mysql:///test?characterEncoding=utf8";// 主机,端口,数据库,编码 + static String user = "root"; + static String pwd = "root"; + // 2 注册驱动 + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } + // 3 获取连接的方法 + public static Connection getConn() throws SQLException { + Connection conn = DriverManager.getConnection(url, user, pwd); + return conn; + } + // 4 通用的查询方法,接收SQL,返回结果集 + public static ResultSet query(String sql,Object... keys){ + ResultSet rs = null; + try { + // 1 获取链接 + Connection conn = getConn(); + // 2 获取执行SQL的pst + PreparedStatement pst = conn.prepareStatement(sql); + // 3 遍历keys给?号赋值 + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + } + // 4 pst执行sql得到结果集 + rs = pst.executeQuery(); + } catch (SQLException e) { + e.printStackTrace(); + } + // 5 返回结果集 + return rs; + } + // 5 通用的update方法,接收SQL,返回影响的行数(整数) + public static int update(String sql,Object... keys){ + int rs = 0; + try { + // 1 获取链接 + Connection conn = getConn(); + // 2 获取执行SQL的pst + PreparedStatement pst = conn.prepareStatement(sql); + // 3 遍历keys给?号赋值 + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + } + // 4 pst执行sql得到结果集 + rs = pst.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + // 5 返回结果集 + return rs; + } +} + +``` \ No newline at end of file -- Gitee