From d80865795c721120635e835cd63bfad3e127b923 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E4=BA=A8=E4=BC=9F?= <529310475@qq.com> Date: Fri, 9 Jun 2023 12:47:14 +0800 Subject: [PATCH 1/2] =?UTF-8?q?0607=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Day0609.md" | 289 ++++++++++++++++++ 1 file changed, 289 insertions(+) create mode 100644 "11 \351\202\271\344\272\250\344\274\237/Day0609.md" diff --git "a/11 \351\202\271\344\272\250\344\274\237/Day0609.md" "b/11 \351\202\271\344\272\250\344\274\237/Day0609.md" new file mode 100644 index 0000000..c8bec62 --- /dev/null +++ "b/11 \351\202\271\344\272\250\344\274\237/Day0609.md" @@ -0,0 +1,289 @@ +``` +package bean; + +public class Dept { + int DeptID; + String DeptName; + + public Dept() { + } + + public Dept(int deptID, String deptName) { + DeptID = deptID; + DeptName = deptName; + } + + public int getDeptID() { + return DeptID; + } + + public void setDeptID(int deptID) { + DeptID = deptID; + } + + public String getDeptName() { + return DeptName; + } + + public void setDeptName(String deptName) { + DeptName = deptName; + } +} +``` + +``` +package bean; + +public class Emp { + int empId; + String empName; + String Sex; + int Age; + String Tel; + String passWord; + int deptId; + + public Emp() { + } + + public Emp(int empId, String empName, String sex, int age, String tel, String passWord, int deptId) { + this.empId = empId; + this.empName = empName; + Sex = sex; + Age = age; + Tel = tel; + this.passWord = passWord; + this.deptId = deptId; + } + + + public int getEmpId() { + return empId; + } + + public void setEmpId(int empId) { + this.empId = empId; + } + + public String getEmpName() { + return empName; + } + + public void setEmpName(String empName) { + this.empName = empName; + } + + public String getSex() { + return Sex; + } + + public void setSex(String sex) { + Sex = sex; + } + + public int getAge() { + return Age; + } + + public void setAge(int age) { + Age = age; + } + + public String getTel() { + return Tel; + } + + public void setTel(String tel) { + Tel = tel; + } + + public String getPassWord() { + return passWord; + } + + public void setPassWord(String passWord) { + this.passWord = passWord; + } + + public int getDeptId() { + return deptId; + } + + public void setDeptId(int deptId) { + this.deptId = deptId; + } +} +``` + +``` +package Servelet; + +import bean.Emp; +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("/list") +public class Servlet2 extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String sql="select * from dept d,emp e where d.DeptID=e.DeptID"; + ResultSet rs = DBUtil.query(sql); + ArrayList list = new ArrayList<>(); + try { + while(rs.next()){ + int empId = rs.getInt(1); + String empName = rs.getString(2); + String sex = rs.getString(3); + int age = rs.getInt(4); + String tel = rs.getString(5); + String passWord = rs.getString(6); + int deptId = rs.getInt(7); + Emp emp = new Emp(empId,empName,sex,age,tel,passWord,deptId); + list.add(emp); + // int deptId = rs.getInt(1); + // String deptName =rs.getNString(2); + // int empId = rs.getInt(3); + // String empName =rs.getString(4); + // String Sex = rs.getString(5); + // int Age = rs.getInt(6); + // String Tel = rs.getString(7); + // String passWord = rs.getString(8); + } + } catch (SQLException e) { + e.printStackTrace(); + } + request.setAttribute("list",list); + request.getRequestDispatcher("web/list.jsp ").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} +``` + +``` +package utils; + +import java.sql.*; + +public class DBUtil { + static String url="jdbc:mysql:///companyManager?characterEncoding=utf8"; + static String user="root"; + static String passWord="root"; + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } + public static Connection getConn(){ + Connection coon = null; + try { + coon = DriverManager.getConnection(url,user,passWord); + } catch (SQLException e) { + e.printStackTrace(); + } + return coon; + } + 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 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]); + } + num = pst.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + return num; + } +} +``` + +``` +package utils; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; + +@WebServlet(name = "Servlet", value = "/Servlet") +public class Servlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} +``` + +``` +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-08 + Time: 19:41 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +${list} + + + + + + + + + + + + + + + + + + +
编号姓名性别年龄电话所属部门
${emp.empId}${emp.empName}${emp.sex}${emp.age}${emp.tel}${emp.passWord}${emp.deptId}
+ + + +``` \ No newline at end of file -- Gitee From 8608514401610c7f37d68c3a7be83395d67c41f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E4=BA=A8=E4=BC=9F?= <529310475@qq.com> Date: Mon, 12 Jun 2023 20:30:25 +0800 Subject: [PATCH 2/2] =?UTF-8?q?0612=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Day0612.md" | 523 ++++++++++++++++++ 1 file changed, 523 insertions(+) create mode 100644 "11 \351\202\271\344\272\250\344\274\237/Day0612.md" diff --git "a/11 \351\202\271\344\272\250\344\274\237/Day0612.md" "b/11 \351\202\271\344\272\250\344\274\237/Day0612.md" new file mode 100644 index 0000000..138a4df --- /dev/null +++ "b/11 \351\202\271\344\272\250\344\274\237/Day0612.md" @@ -0,0 +1,523 @@ +``` +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显示 + // 1 编写SQL + String sql = "select * from house_type"; + // 2 将SQL传给工具类的查询方法,得到结果集 + ResultSet rs = DBUtil.query(sql); + // 3 设置一个房源的集合,遍历结果集,将结果封装成房源对象,再将对象添加到集合 + //设置一个房源的集合 + 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(); + } + // 4 把集合添加到request域中 + request.setAttribute("list",list); + // 5 将请求转发给jsp + request.getRequestDispatcher("/WEB-INF/add.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // 1 处理乱码 + request.setCharacterEncoding("utf-8"); + // 2 接收表单数据 + 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 values (?,?,?,?,?,?,?)"; + // 4 调用工具类执行SQL,得到影响的行数 + int i = DBUtil.update(sql, null, leaseMode, rent, contacts, depositMethod, type, address); + // 5 根据影响的行数,做判断提示 + if (i>0){ + // 使用响应的重定向可以直接刷新列表 + response.sendRedirect("/list"); + }else{ + // 在request域中添加失败的提示信息 + request.setAttribute("msg","添加失败"); + // 5 将请求转发给jsp + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + } + } +} +package servlet; + +import utils.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; + +@WebServlet("/delete") +public class DeleteServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // 根据id删除对应的房源 + // 1获取ID的值 + String id = request.getParameter("id"); + // 3 编写sql + String sql = "delete from house_info where id=?"; + // 4 调用工具类执行SQL,得到影响的行数 + int i = DBUtil.update(sql, id); + // 5 根据影响的行数,做判断提示 + if (i>0){ + // 使用响应的重定向可以直接刷新列表 + response.sendRedirect("/list"); + }else{ + // 在request域中添加失败的提示信息 + request.setAttribute("msg","删除失败"); + // 5 将请求转发给jsp + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + } + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} +package servlet; + +import bean.HouseInfo; +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("/list") +public class ListServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // * 想办法从数据库查询所有房源信息,将用jsp显示 + // 1 编写SQL + String sql = "select * from house_info i,house_type t where i.house_type_id=t.id"; + // 2 将SQL传给工具类的查询方法,得到结果集 + ResultSet rs = DBUtil.query(sql); + // 3 设置一个房源的集合,遍历结果集,将结果封装成房源对象,再将对象添加到集合 + //设置一个房源的集合 + ArrayList list = new ArrayList<>(); + //遍历结果集 + try { + while (rs.next()){ + // 将结果封装成房源对象 + int id = rs.getInt(1); + String mode = rs.getString(2); + double rent = rs.getDouble(3); + String contacts = rs.getString(4); + String method = rs.getString(5); + int typeId = rs.getInt(6); + String address = rs.getString(7); + String typeName = rs.getString(9); + HouseInfo house = new HouseInfo(id, mode, rent, contacts, method, typeId, address, typeName); + //再将对象添加到集合 + list.add(house); + } + } catch (SQLException e) { + e.printStackTrace(); + } + // 4 把集合添加到request域中 + request.setAttribute("list",list); + // 5 将请求转发给jsp + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response); + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // 根据姓名搜索对应的房源 + // 1 处理乱码 + request.setCharacterEncoding("utf-8"); + // 1获取姓名的值 + String user = request.getParameter("user"); + // 1 编写SQL + String sql = "select * from house_info i,house_type t where i.house_type_id=t.id and contacts like ? "; + // 2 将SQL传给工具类的查询方法,得到结果集 + ResultSet rs = DBUtil.query(sql,"%"+user+"%"); + // 3 设置一个房源的集合,遍历结果集,将结果封装成房源对象,再将对象添加到集合 + //设置一个房源的集合 + ArrayList list = new ArrayList<>(); + //遍历结果集 + try { + while (rs.next()){ + // 将结果封装成房源对象 + int id = rs.getInt(1); + String mode = rs.getString(2); + double rent = rs.getDouble(3); + String contacts = rs.getString(4); + String method = rs.getString(5); + int typeId = rs.getInt(6); + String address = rs.getString(7); + String typeName = rs.getString(9); + HouseInfo house = new HouseInfo(id, mode, rent, contacts, method, typeId, address, typeName); + //再将对象添加到集合 + list.add(house); + } + } catch (SQLException e) { + e.printStackTrace(); + } + // 4 把集合添加到request域中 + request.setAttribute("list",list); + // 5 将请求转发给jsp + request.getRequestDispatcher("/WEB-INF/list.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; + } +} +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-09 + Time: 15:09 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
租赁方式
租金(元)
联系人
押金方式
房屋类型 + +
详细地址
+
+ + +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-09 + Time: 14:43 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + + + + + + + + + + + + + + + + + + + + + + + + + +
编号租赁方式租金(元)联系人押金方式房屋类型详细地址操作
${house.id}${house.leaseMode}${house.rent}${house.contacts}${house.depositMethod}${house.type}${house.address} + 删除 +
+
+ 姓名: +
+ + +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-09 + Time: 15:30 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +${msg} +返回列表 + + +``` \ No newline at end of file -- Gitee