From 473f7905b095c19c2051b9a0d20d32b18b7d670f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=A8=81=E6=96=8C?= <2016807083@qq.com> Date: Fri, 9 Jun 2023 21:25:03 +0800 Subject: [PATCH 1/2] =?UTF-8?q?20230609=20=E5=91=98=E5=B7=A5=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E7=B3=BB=E7=BB=9F=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...73\347\273\237\344\275\234\344\270\232.md" | 606 ++++++++++++++++++ 1 file changed, 606 insertions(+) create mode 100644 "09 \347\216\213\345\250\201\346\226\214/20230609 \345\221\230\345\267\245\347\256\241\347\220\206\347\263\273\347\273\237\344\275\234\344\270\232.md" diff --git "a/09 \347\216\213\345\250\201\346\226\214/20230609 \345\221\230\345\267\245\347\256\241\347\220\206\347\263\273\347\273\237\344\275\234\344\270\232.md" "b/09 \347\216\213\345\250\201\346\226\214/20230609 \345\221\230\345\267\245\347\256\241\347\220\206\347\263\273\347\273\237\344\275\234\344\270\232.md" new file mode 100644 index 0000000..787851a --- /dev/null +++ "b/09 \347\216\213\345\250\201\346\226\214/20230609 \345\221\230\345\267\245\347\256\241\347\220\206\347\263\273\347\273\237\344\275\234\344\270\232.md" @@ -0,0 +1,606 @@ +# 作业 + +建库建表 + +```mysql +# 数据库名称:CompanyManager +create database CompanyManager charset utf8; +use CompanyManager; +# 表: Dept (部门信息表) +# 字段显示 字段名 数据类型 默认值 备注和说明 +create table Dept +( + DeptID int primary key auto_increment, # 部门编号 主键,自动增长列 + DeptName varchar(20) not null # 部门名称 不允许为空 +); +insert into dept values + (0,'除草部'), + (0,'摆烂部'), + (0,'狗叫部'); + +# 表:Emp (员工信息表) +# 字段显示 字段名 数据类型 默认值 备注和说明 +create table Emp +( + EmpID int primary key auto_increment, # 员工编号 主键,自动增长列 + EmpName varchar(20) not null, # 员工姓名 不允许为空 + Sex char(2) not null, # 性别 不允许为空 + Age int not null, # 员工年龄 不允许为空 + Tel varchar(20) not null unique, # 联系电话 不允许为空(唯一约束) + PassWord varchar(12) not null, # 密码 不允许为空 + DeptID int, # 部门编号 外键,关联部门表DeptID字段 + foreign key (DeptID) references dept (DeptID) +); +insert into emp values + (0,'宋宋','男',18,'1234577777','123',1), + (0,'居居明','男',16,'1234566666','333',2), + (0,'妯娌杭','女',20,'1234588888','222',3); + + +``` + +工具类 + +```java +package utils; + +import java.sql.*; + +public class DBUtil { + private static final String url="jdbc:mysql:///attdb?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String username="root"; + private static final String password="root"; + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + System.out.println("注册驱动异常"); + } + } + public static Connection getConn(){ + Connection conn = null; + try { + conn = DriverManager.getConnection(url, username, password); + } catch (SQLException e) { + System.out.println("获取连接异常"); + } + return conn; + } + public static ResultSet select(String sql,Object ...bb){ + Connection conn = getConn(); + ResultSet rs = null; + try { + PreparedStatement ps = conn.prepareStatement(sql); + for (int i = 0; i < bb.length; i++) { + ps.setObject((i+1),bb[i]); + } + rs = ps.executeQuery(); + } catch (SQLException e) { + System.out.println("查询SQL执行异常"); + } + return rs; + } + public static int update(String sql,Object ...bb){ + Connection conn = getConn(); + int num = 0; + try { + PreparedStatement ps = conn.prepareStatement(sql); + for (int i = 0; i < bb.length; i++) { + ps.setObject((i+1),bb[i]); + } + num = ps.executeUpdate(); + } catch (SQLException e) { + System.out.println("增删改SQL执行异常"); + } + return num; + } + public static void close(Connection conn,PreparedStatement ps,ResultSet rs){ + try { + if (rs!=null){ + rs.close(); + } + if (ps!=null){ + ps.close(); + } + if (conn!=null){ + conn.close(); + } + } catch (SQLException e) { + System.out.println("资源释放异常"); + } + } +} + +``` + +封装类 + +```java + + package bean; + +public class Dept { + private int deptID; + private String deptName; + + public Dept() { + } + + public Dept(int deptID, String deptName) { + this.deptID = deptID; + this.deptName = deptName; + } + + public int getDeptID() { + return deptID; + } + + public void setDeptID(int deptID) { + this.deptID = deptID; + } + + public String getDeptName() { + return deptName; + } + + public void setDeptName(String deptName) { + this.deptName = deptName; + } + + @Override + public String toString() { + return "Dept{" + + "deptID=" + deptID + + ", deptName='" + deptName + '\'' + + '}'; + } +} + +package bean; + +public class Emp { + private int empID; + private String empName; + private String sex; + private int age; + private String tel; + private String passWord; + private int deptID; + private String deptName; + + public Emp() { + } + + @Override + public String toString() { + return "Emp{" + + "empID=" + empID + + ", empName='" + empName + '\'' + + ", sex='" + sex + '\'' + + ", age=" + age + + ", tel='" + tel + '\'' + + ", passWord='" + passWord + '\'' + + ", deptID=" + deptID + + ", deptName='" + deptName + '\'' + + '}'; + } + + 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) { + this.sex = sex; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getTel() { + return tel; + } + + public void setTel(String tel) { + this.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; + } + + public String getDeptName() { + return deptName; + } + + public void setDeptName(String deptName) { + this.deptName = deptName; + } + + public Emp(int empID, String empName, String sex, int age, String tel, String passWord, int deptID, String deptName) { + this.empID = empID; + this.empName = empName; + this.sex = sex; + this.age = age; + this.tel = tel; + this.passWord = passWord; + this.deptID = deptID; + this.deptName = deptName; + } +} + +``` + +Dao类 + +```java +package dao; + +import bean.Dept; +import utils.DBUtil; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +public class DeptDao { + //查看所有部门的信息 + public ArrayList getAllDept(){ + ArrayList list = new ArrayList<>(); + String sql="select * from dept"; + ResultSet rs = DBUtil.select(sql); + try { + while (rs.next()){ + int deptID = rs.getInt(1); + String deptName = rs.getString(2); + Dept dept = new Dept(deptID, deptName); + list.add(dept); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + DBUtil.close(null,null,rs); + } + return list; + } +} +package dao; + +import bean.Emp; +import utils.DBUtil; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +public class EmpDao { + //查询所有信息的方法 + public ArrayList getAllEmp(){ + //新建查询 + ArrayList list = new ArrayList<>(); + //编写SQL语句,把SQL语句传给工具类查询方法 + String sql="select * from emp e,dept d where e.deptID=d.deptID order by empID"; + //调用工具类方法 + ResultSet rs = DBUtil.select(sql); + //遍历结果集 + try { + while (rs.next()){ + int id = 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); + String deptName = rs.getString(9); + Emp emp = new Emp(id, empName, sex, age, tel, passWord, deptID, deptName); + //把结果集添加进集合里 + list.add(emp); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + DBUtil.close(null,null,rs); + } + return list; + } + + //模糊查询,根据姓名查询信息的方法 + public ArrayList getNameEmp(String name){ + //新建查询 + ArrayList list = new ArrayList<>(); + //编写SQL语句,把SQL语句传给工具类查询方法 + String sql="select * from emp e,dept d where e.deptID=d.deptID and empName like ? order by empID"; + //调用工具类方法 + ResultSet rs = DBUtil.select(sql,"%"+name+"%"); + //遍历结果集 + try { + while (rs.next()){ + int id = 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); + String deptName = rs.getString(9); + Emp emp = new Emp(id, empName, sex, age, tel, passWord, deptID, deptName); + //把结果集添加进集合里 + list.add(emp); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + DBUtil.close(null,null,rs); + } + return list; + } + //添加信息的方法 + public int addEmp(Emp emp){ + String sql="insert into emp values (?,?,?,?,?,?,?)"; + int i = DBUtil.update(sql,0,emp.getEmpName(),emp.getSex(),emp.getAge(),emp.getTel(),emp.getPassWord(),emp.getDeptID()); + return i; + } + //删除信息的方法 + public int deleteEmp(int id){ + String sql="delete from emp where empID = ?"; + int i = DBUtil.update(sql, id); + return i; + } + +} + +``` + +测试类 + +```java +package servlet; + +import bean.Dept; +import bean.Emp; +import dao.DeptDao; +import dao.EmpDao; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.util.ArrayList; + +@WebServlet("/emp/*") +public class EmpController extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + //1.设置字符集 + request.setCharacterEncoding("utf-8"); + //2.返回URL路径 + String path = request.getPathInfo(); + if (path==null || path.equals("/")){ + //3.调用EmpDao类中的查询所有信息的方法 + ArrayList list = new EmpDao().getAllEmp(); + //4.返回到request域中 + request.setAttribute("list",list); + //5.传给JSP页面 + request.getRequestDispatcher("/WEB-INF/all_emp.jsp").forward(request,response); + }else if (path.matches("/delete/\\d+")){ + int id = Integer.parseInt(path.substring(8)); + int i = new EmpDao().deleteEmp(id); + response.sendRedirect("/emp"); + }else if (path.equals("/add")){ + ArrayList list = new DeptDao().getAllDept(); + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/insert.jsp").forward(request,response); + } + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + //1.设置字符集 + request.setCharacterEncoding("utf-8"); + //2.返回URL路径 + String path = request.getPathInfo(); + if (path.equals("/select")){ + String empName = request.getParameter("empName"); + ArrayList list = new EmpDao().getNameEmp(empName); + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/all_emp.jsp").forward(request,response); + }else if (path.equals("/save")){ + String empName = request.getParameter("empName"); + String sex = request.getParameter("sex"); + int age = Integer.parseInt(request.getParameter("age")); + String tel = request.getParameter("tel"); + String passWord = request.getParameter("passWord"); + int deptID = Integer.parseInt(request.getParameter("deptID")); + Emp emp = new Emp(0, empName, sex, age, tel, passWord, deptID,null); + int i = new EmpDao().addEmp(emp); + if (i>0){ + request.setAttribute("msg","添加成功"); + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + }else { + request.setAttribute("msg","添加失败"); + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + } + } + } +} + +``` + +Jsp代码 + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-09 + Time: 19:28 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + +
+ 姓名: +
+ + + + + + + + + + + + + + + + + + + + + + +
编号姓名性别年龄电话所属部门操作
${list.empID}${list.empName}${list.sex}${list.age}${list.tel}${list.deptName} + +
+
+ + + + +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-09 + Time: 20:07 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
姓名
性别
年龄
电话号码
密码
部门名称 + +
+
+ + + +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-09 + Time: 20:20 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +

${msg}

+ + + + + +``` + -- Gitee From 7a9a3c163479a0d5e7da9ad14ec3c8c70de2259e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=A8=81=E6=96=8C?= <2016807083@qq.com> Date: Mon, 12 Jun 2023 20:38:55 +0800 Subject: [PATCH 2/2] =?UTF-8?q?20230610=20=E6=88=BF=E5=B1=8B=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E7=B3=BB=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...73\347\273\237\344\275\234\344\270\232.md" | 702 ++++++++++++++++++ 1 file changed, 702 insertions(+) create mode 100644 "09 \347\216\213\345\250\201\346\226\214/202306010 \346\210\277\345\261\213\347\256\241\347\220\206\347\263\273\347\273\237\344\275\234\344\270\232.md" diff --git "a/09 \347\216\213\345\250\201\346\226\214/202306010 \346\210\277\345\261\213\347\256\241\347\220\206\347\263\273\347\273\237\344\275\234\344\270\232.md" "b/09 \347\216\213\345\250\201\346\226\214/202306010 \346\210\277\345\261\213\347\256\241\347\220\206\347\263\273\347\273\237\344\275\234\344\270\232.md" new file mode 100644 index 0000000..551fde3 --- /dev/null +++ "b/09 \347\216\213\345\250\201\346\226\214/202306010 \346\210\277\345\261\213\347\256\241\347\220\206\347\263\273\347\273\237\344\275\234\344\270\232.md" @@ -0,0 +1,702 @@ +# 作业 + +建库建表 + +```mysql +# 数据库名称:test +create database test charset utf8; +use test; +# 表:house_type (房屋类型表) +# 字段显示 字段名 数据类型 默认值 备注和说明 +create table house_type +( + id int primary key auto_increment, # 编号 主键,自动增长列 + type varchar(50) not null # 房屋类型 不允许为空 +); + +# 表:house_info (房源信息表) +# 字段显示 字段名 数据类型 默认值 备注和说明 +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), # 押金方式可以为空 + house_type_id int, # 房屋类型 外键 + address varchar(200) not null, # 详细地址不允许为空 + foreign key (house_type_id) references house_type (id) +); +insert into house_type values +(0,'两室一厅'), +(0,'两室两厅'), +(0,'三室一厅'); +insert into house_info values +(0,'整租',6666,'居居明','押一付三',1,'花果山水帘洞'), +(0,'月租',6886,'周周','押三付二',2,'团结里14栋512'), +(0,'合租',77,'朱朱','押一付五',3,'龙岩天桥底下'); +``` + +工具类 + +```java +package utils; + +import java.sql.*; + +public class DBUtil { + private static final String url="jdbc:mysql:///attdb?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String username="root"; + private static final String password="root"; + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + System.out.println("注册驱动异常"); + } + } + public static Connection getConn(){ + Connection conn = null; + try { + conn = DriverManager.getConnection(url, username, password); + } catch (SQLException e) { + System.out.println("获取连接异常"); + } + return conn; + } + public static ResultSet select(String sql,Object ...bb){ + Connection conn = getConn(); + ResultSet rs = null; + try { + PreparedStatement ps = conn.prepareStatement(sql); + for (int i = 0; i < bb.length; i++) { + ps.setObject((i+1),bb[i]); + } + rs = ps.executeQuery(); + } catch (SQLException e) { + System.out.println("查询SQL执行异常"); + } + return rs; + } + public static int update(String sql,Object ...bb){ + Connection conn = getConn(); + int num = 0; + try { + PreparedStatement ps = conn.prepareStatement(sql); + for (int i = 0; i < bb.length; i++) { + ps.setObject((i+1),bb[i]); + } + num = ps.executeUpdate(); + } catch (SQLException e) { + System.out.println("增删改SQL执行异常"); + } + return num; + } + public static void close(Connection conn,PreparedStatement ps,ResultSet rs){ + try { + if (rs!=null){ + rs.close(); + } + if (ps!=null){ + ps.close(); + } + if (conn!=null){ + conn.close(); + } + } catch (SQLException e) { + System.out.println("资源释放异常"); + } + } +} + +``` + +封装类 + +```java +package bean; + +public class HouseInfo { + private int id; //编号 + private String mode; //租赁方式 + private double rent; //rent + private String contacts; //联系人 + private String method; //押金方式 + private int houseTypeID;//房屋类型编号,外建 + private String address; //详细地址 + private String type;//房屋类型 + + @Override + public String toString() { + return "HouseInfo{" + + "id=" + id + + ", mode='" + mode + '\'' + + ", rent=" + rent + + ", contacts='" + contacts + '\'' + + ", method='" + method + '\'' + + ", houseTypeID=" + houseTypeID + + ", address='" + address + '\'' + + ", 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 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(int id, String mode, double rent, String contacts, String method, int houseTypeID, String address, String type) { + this.id = id; + this.mode = mode; + this.rent = rent; + this.contacts = contacts; + this.method = method; + this.houseTypeID = houseTypeID; + this.address = address; + this.type = type; + } + + public HouseInfo() { + } +} + +package bean; + +public class HouseType { + private int id; + private String type; + + public HouseType() { + } + + @Override + public String toString() { + return "HouseType{" + + "id=" + id + + ", 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; + } + + public HouseType(int id, String type) { + this.id = id; + this.type = type; + } +} + +``` + +Dao类 + +```java +package dao; + +import bean.HouseInfo; +import utils.DBUtil; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +public class InfoDao { + //查询所有信息的方法 + public ArrayList getAllHouseInfo(){ + //1.编写SQL语句,返回给工具类的查询方法 + String sql="select * from house_info i,house_type t where i.house_type_id=t.id order by i.id"; + //2.调用工具类的查询方法 + ResultSet rs = DBUtil.select(sql); + //3.新建集合 + ArrayList list = new ArrayList<>(); + //4.遍历结果集 + try { + 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 houseTypeID = rs.getInt(6);//房屋类型----数字描述 + String address = rs.getString(7); //详细地址 + String type = rs.getString(9);//房屋类型----中文描述 + HouseInfo houseInfo = new HouseInfo(id, mode, rent, contacts, method, houseTypeID, address, type); + //5.把结果集添加进集合里 + list.add(houseInfo); + } + } catch (SQLException throwables) { + throwables.printStackTrace(); + } finally { + DBUtil.close(null,null,rs); + } + //6.返回集合 + return list; + } + + //添加房源信息 + public int addHouseInfo(HouseInfo info){ + //1.编写SQL语句,返回给工具类的update方法 + String sql="insert into house_info values (?,?,?,?,?,?,?)"; + //2.调用工具类的update方法 + int i = DBUtil.update(sql, 0, info.getMode(), info.getRent(), info.getContacts(), info.getMethod(), info.getHouseTypeID(), info.getAddress()); + //3.返回一个整数,也是数据库受影响的行数 + return i; + } + //根据ID删除房源信息 + public int deleteHouseInfo(int id){ + //1.编写SQL语句,返回给工具类的update方法 + String sql="delete from house_info where id=?"; + //2.调用工具类的update方法 + int i = DBUtil.update(sql,id); + //3.返回一个整数,也是数据库受影响的行数 + return i; + } + + //模糊查询,根据姓名查询 + public ArrayList getNameHouseInfo(String name){ + //1.编写SQL语句,返回给工具类的查询方法 + String sql="select * from house_info i,house_type t where i.house_type_id=t.id and contacts like ? order by i.id"; + //2.调用工具类的查询方法 + ResultSet rs = DBUtil.select(sql,"%"+name+"%"); + //3.新建集合 + ArrayList list = new ArrayList<>(); + //4.遍历结果集 + try { + 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 houseTypeID = rs.getInt(6);//房屋类型----数字描述 + String address = rs.getString(7); //详细地址 + String type = rs.getString(9);//房屋类型----中文描述 + HouseInfo houseInfo = new HouseInfo(id, mode, rent, contacts, method, houseTypeID, address, type); + //5.把结果集添加进集合里 + list.add(houseInfo); + } + } catch (SQLException throwables) { + throwables.printStackTrace(); + } finally { + DBUtil.close(null,null,rs); + } + //6.返回集合 + return list; + } + + //根据ID查询信息 + public HouseInfo getIdHouseInfo(int ID){ + //1.编写SQL语句,返回给工具类的查询方法 + String sql="select * from house_info i,house_type t where i.house_type_id=t.id and i.id=? order by i.id"; + //2.调用工具类的查询方法 + ResultSet rs = DBUtil.select(sql,ID); + HouseInfo houseInfo=null; + //3.遍历结果集 + try { + 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 houseTypeID = rs.getInt(6);//房屋类型----数字描述 + String address = rs.getString(7); //详细地址 + String type = rs.getString(9);//房屋类型----中文描述 + houseInfo = new HouseInfo(id, mode, rent, contacts, method, houseTypeID, address, type); + + } + } catch (SQLException throwables) { + throwables.printStackTrace(); + } finally { + DBUtil.close(null,null,rs); + } + //4.返回集合 + return houseInfo; + } +} + +package dao; + +import bean.HouseInfo; +import bean.HouseType; +import utils.DBUtil; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +public class TypeDao { + //查询所有房屋类型的方法 + public ArrayList getAllHouseType(){ + //1.编写SQL语句,返回给工具类的查询方法 + String sql="select * from house_type"; + //2.调用工具类的查询方法 + ResultSet rs = DBUtil.select(sql); + //3.新建集合 + ArrayList list = new ArrayList<>(); + //4.遍历结果集 + try { + while (rs.next()){ + int id = rs.getInt(1);//编号 + String type = rs.getString(2);//房屋类型----中文描述 + HouseType houseType = new HouseType(id, type); + //5.把结果集添加进集合里 + list.add(houseType); + } + } catch (SQLException throwables) { + throwables.printStackTrace(); + } finally { + DBUtil.close(null,null,rs); + } + //6.返回集合 + return list; + } +} + +``` + +测试类 + +```java +package servlet; + +import bean.HouseInfo; +import bean.HouseType; +import dao.InfoDao; +import dao.TypeDao; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.util.ArrayList; + +@WebServlet("/house/*") +public class HouseController extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + //1.设置字符集 + request.setCharacterEncoding("utf-8"); + //2,返回servlet的url路径 + String path = request.getPathInfo(); + if (path==null || path.equals("/")){ + //调用InfoDao类查询所有信息的方法 + ArrayList list = new InfoDao().getAllHouseInfo(); + //返回给request域中 + request.setAttribute("list",list); + //将请求转发给jsp页面 + request.getRequestDispatcher("/WEB-INF/all_house.jsp").forward(request,response); + }else if (path.equals("/add")){ + ArrayList list = new TypeDao().getAllHouseType(); + //返回给request域中 + request.setAttribute("list",list); + //将请求转发给jsp页面 + request.getRequestDispatcher("/WEB-INF/insert.jsp").forward(request,response); + }else if (path.matches("/delete/\\d+")){ + int id = Integer.parseInt(path.substring(8)); + new InfoDao().deleteHouseInfo(id); + response.sendRedirect("/house"); + }else if (path.matches("/\\d+")){ + int id = Integer.parseInt(path.substring(1)); + HouseInfo info = new InfoDao().getIdHouseInfo(id); + //返回给request域中 + request.setAttribute("info",info); + //将请求转发给jsp页面 + request.getRequestDispatcher("/WEB-INF/getID.jsp").forward(request,response); + } + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + //1.设置字符集 + request.setCharacterEncoding("utf-8"); + //2,返回servlet的url路径 + String path = request.getPathInfo(); + if (path.equals("/save")){ + String mode = request.getParameter("mode"); + double rent = Double.parseDouble(request.getParameter("rent")); + String contacts = request.getParameter("contacts"); + String method = request.getParameter("method"); + int houseTypeID = Integer.parseInt(request.getParameter("houseTypeID")); + String address = request.getParameter("address"); + HouseInfo houseInfo = new HouseInfo(0, mode, rent, contacts, method, houseTypeID, address, null); + int i = new InfoDao().addHouseInfo(houseInfo); + if (i>0){ + response.sendRedirect("/house"); + }else { + request.setAttribute("msg","添加成功"); + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + } + }else if (path.equals("/search")){ + String contacts = request.getParameter("contacts"); + ArrayList list = new InfoDao().getNameHouseInfo(contacts); + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/all_house.jsp").forward(request,response); + } + } +} + +``` + +Jsp代码 + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Lenovo + Date: 2023/6/12 + Time: 8:56 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + +
+ 联系人: +
+
+ + + + + + + + + + + + + + + + + + + <%--列表中“房屋类型”显示成中文描述--%> + + + + + +
编号租赁方式租金(元)联系人押金方式房屋类型详细地址操作
${list.id}${list.mode}${list.rent}${list.contacts}${list.method}${list.type}${list.address} + 删除 + 查看 +
+
+ + + +<%-- + Created by IntelliJ IDEA. + User: Lenovo + Date: 2023/6/12 + Time: 9:35 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + + +

正在查看${info.contacts}的信息

+
+ + + + + + + + + + + + + + + + + + + +
编号租赁方式租金(元)联系人押金方式房屋类型详细地址
${info.id}${info.mode}${info.rent}${info.contacts}${info.method}${info.type}${info.address}
+ + + +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Lenovo + Date: 2023/6/12 + Time: 9:11 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + +
+ + + + + + + + + + + + + + + + + + + + + + + +
租赁方式
租金(元)
联系人
押金方式
房屋类型 + +
详细地址
+
+ + +<%-- + Created by IntelliJ IDEA. + User: Lenovo + Date: 2023/6/12 + Time: 9:21 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 提示信息 + + +

${msg}

+ + + + +``` + -- Gitee