From 92a869e14adc37590233c297fe5152dde53c6b78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=8F=AD=E9=98=B3=E4=B8=BD?= <2431466589@qq.com> Date: Fri, 9 Jun 2023 13:32:14 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...30\345\267\245\347\256\241\347\220\206.md" | 440 ++++++++++++++++++ 1 file changed, 440 insertions(+) create mode 100644 "37 \346\217\255\351\230\263\344\270\275/\345\221\230\345\267\245\347\256\241\347\220\206.md" diff --git "a/37 \346\217\255\351\230\263\344\270\275/\345\221\230\345\267\245\347\256\241\347\220\206.md" "b/37 \346\217\255\351\230\263\344\270\275/\345\221\230\345\267\245\347\256\241\347\220\206.md" new file mode 100644 index 0000000..bb1aa1c --- /dev/null +++ "b/37 \346\217\255\351\230\263\344\270\275/\345\221\230\345\267\245\347\256\241\347\220\206.md" @@ -0,0 +1,440 @@ +# 1.mysql + +```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 value +(1,'测试部'), +(2,'实施部'), +(3,'开发部'), +(4,'财务部'); +# 表: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, # 部门编号 + foreign key (DeptID) references Dept (DeptID) #外键,关联部门表DeptID字段 +); +#插入数据 +insert into Emp value +(1,'张三','男',23,'13238890','2122',1), +(2,'李四','男',45,'15456545','2343',2), +(3,'王五','女',23,'13456657','5465',3), +(4,'老六','女',32,'15556776','7853',4); + +select * from Dept; +select * from Emp; +``` + +# 2.封装类bean + +### 部门信息表Dept + +```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 + '\'' + + '}'; + } +} +``` + +### 员工信息表Emp + +```java +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() { + } + + 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; + } + + 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; + } + + @Override + public String toString() { + return "Emp{" + + "empId=" + empId + + ", empName='" + empName + '\'' + + ", sex='" + sex + '\'' + + ", age=" + age + + ", tel='" + tel + '\'' + + ", passWord='" + passWord + '\'' + + ", deptId=" + deptId + + ", deptName='" + deptName + '\'' + + '}'; + } +} +``` + +# 3.servlet + +### servlet + +```java +package servlet; + +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 Servlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // 1 编写查询的SQL + String sql = "select * from dept d,emp e where d.DeptID=e.DeptID"; + // 2 调用工具类,执行SQL得到结果集 + ResultSet rs = DBUtil.query(sql); + // 3 把结果集变成一个集合 + ArrayList list = new ArrayList<>(); + // 4 遍历结果集,将元素封装成对象,添加到list集合 + try { + while (rs.next()){ + int deptId = rs.getInt(1); + String deptName=rs.getString(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); + //将元素封装成对象 + Emp emp = new Emp(empId, empName, sex, age, tel, passWord, deptId, deptName); + //添加到list集合 + list.add(emp); + } + } catch (SQLException e) { + System.out.println("出现错误"); + e.printStackTrace(); + } + // 5 把集合放入request域 + request.setAttribute("list",list); + // 6 把请求转发给jsp + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +``` + +### Servlet2 + +```java +package servlet; + +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("/search") +public class Servlet2 extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + //接收表单数据,到数据库查询,将结果返回给jsp显示 + // 0 处理乱码 + request.setCharacterEncoding("utf-8"); + // 获取表单数据 + String username = request.getParameter("username"); + // 1 编写查询的SQL + String sql = "select * from dept d,emp e where d.DeptID=e.DeptID and empname like ?"; + // 2 调用工具类,执行SQL得到结果集 + ResultSet rs = DBUtil.query(sql, "%" + username + "%"); + // 3 把结果集变成一个集合 + ArrayList list = new ArrayList<>(); + // 4 遍历结果集,将元素封装成对象,添加到list集合 + try { + while (rs.next()){ + int deptId = rs.getInt(1); + String deptName=rs.getString(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); + //将元素封装成对象 + Emp emp = new Emp(empId, empName, sex, age, tel, passWord, deptId, deptName); + //添加到list集合 + list.add(emp); + } + } catch (SQLException e) { + System.out.println("出现错误"); + e.printStackTrace(); + } + // 5 把集合放入request域 + request.setAttribute("list",list); + // 6 把请求转发给jsp + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response); + } +} + +``` + +# 4.工具类包utils + +### 工具类DBUtil + +```java +package utils; + +import java.sql.*; + +public class DBUtil { + static String url="jdbc:mysql:///companyManager?characterEncoding=utf8";//定义好数据库主机 + static String user="root"; + static String pwd="root"; + // 2 注册驱动 + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + System.out.println("注册驱动失败"); + 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 获取pst + PreparedStatement pst = conn.prepareStatement(sql); + // 3 遍历keys + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + } + // 4 执行sql + rs = pst.executeQuery(); + } catch (SQLException e) { + System.out.println("执行查询失败"); + e.printStackTrace(); + } + // 5 返回rs + return rs; + } + // 5 通用update + public static int update(String sql,Object...keys){ + int rs = 0; + try { + // 1 获取连接 + Connection conn = getConn(); + // 2 获取pst + PreparedStatement pst = conn.prepareStatement(sql); + // 3 遍历keys + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + } + // 4 执行sql + rs = pst.executeUpdate(); + } catch (SQLException e) { + System.out.println("执行修改失败"); + e.printStackTrace(); + } + // 5 返回rs + return rs; + } +} + +``` + +# 5.HTML + +```java +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +
+ 姓名: +
+ + + + + + + + + + + + + + + + + + + +
编号姓名性别年龄电话所属部门
${emp.empId}${emp.empName}${emp.sex}${emp.age}${emp.tel}${emp.deptName}
+ + + +``` + + + + + -- Gitee From bb17a539b36e399d3756eba9663a9bfa7a1c66ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=8F=AD=E9=98=B3=E4=B8=BD?= <2431466589@qq.com> Date: Mon, 12 Jun 2023 19:41:46 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=88=BF=E5=B1=8B=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...77\345\261\213\347\256\241\347\220\206.md" | 560 ++++++++++++++++++ 1 file changed, 560 insertions(+) create mode 100644 "37 \346\217\255\351\230\263\344\270\275/\346\210\277\345\261\213\347\256\241\347\220\206.md" diff --git "a/37 \346\217\255\351\230\263\344\270\275/\346\210\277\345\261\213\347\256\241\347\220\206.md" "b/37 \346\217\255\351\230\263\344\270\275/\346\210\277\345\261\213\347\256\241\347\220\206.md" new file mode 100644 index 0000000..cc3b994 --- /dev/null +++ "b/37 \346\217\255\351\230\263\344\270\275/\346\210\277\345\261\213\347\256\241\347\220\206.md" @@ -0,0 +1,560 @@ +# mysql + +```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 # 房屋类型 不允许为空 +); + +#插入数据 +insert into house_type values + (1, '一室一厅'), + (2, '两室两厅'), + (3, '三室一厅'), + (4, '四室两厅'); + +# 表: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), # 详细地址 不允许为空 + foreign key (house_type_id) references house_type (id) +); + +#插入数据 +insert into house_info values + (1,'整租',1500,'李斯','押一付一',1,'北京'), + (2,'整租',2100,'王舞','押一付三',2,'上海'), + (3,'合租',2800,'张三','押一付二',3,'深圳'), + (4,'合租',3500,'张三','押一付三',4,'杭州'); + +select * from house_type; +select * from house_info; +``` + +# bean包 + +### HouseType + +```java +package bean; + +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 + '\'' + + '}'; + } +} + +``` + +### HouseInfo + +```java +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; + + 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; + } + + 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; + } + + @Override + public String toString() { + return "HouseInfo{" + + "id=" + id + + ", leaseMode='" + leaseMode + '\'' + + ", rent=" + rent + + ", contacts='" + contacts + '\'' + + ", depositMethod='" + depositMethod + '\'' + + ", houseTypeId=" + houseTypeId + + ", address='" + address + '\'' + + ", type='" + type + '\'' + + '}'; + } +} + +``` + +# 工具类utils + +### DBUtil + +```java +package utils; + +import java.sql.*; + +public class DBUtil { + //所有成员都是static修饰的 + //1 定义好url,user,pwd + private static final String url="jdbc:mysql:///test?characterEncoding=utf8"; + private static final String user="root"; + private static final String pwd="root"; + // 2 注册驱动 + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + System.out.println("注册驱动失败"); + 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) { + System.out.println("查询失败"); + 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) { + System.out.println("操作失败"); + e.printStackTrace(); + } + //5.返回结果集 + return rs; + } +} + +``` + +# servlet包 + +### ListServlet类 + +```java +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 leaseMode = rs.getString(2); + double rent = rs.getDouble(3); + String contacts = rs.getString(4); + String depositMethod = rs.getString(5); + int houseTypeId = rs.getInt(6); + String address = rs.getString(7); + String type = rs.getString(8); + HouseInfo house = new HouseInfo(id, leaseMode, rent, contacts, depositMethod, houseTypeId, address, type); + //再将对象添加到集合 + 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 { + + } +} + +``` + +### AddServlet + +```java +package servlet; + +import bean.HouseInfo; +import bean.HouseType; +import org.apache.taglibs.standard.extra.spath.Step; +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("/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 typ = new HouseType(id,type); + //再将对象添加到集合 + list.add(typ); + } + } 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"); + double rent = Double.parseDouble(request.getParameter("rent")); + String contacts = request.getParameter("contacts"); + String depositMethod = request.getParameter("depositMethod"); + String type = request.getParameter("type"); + String address = request.getParameter("address"); + // 编写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.setAttribute("msg","添加失败"); + // 将请求转发给jsp + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + } + } +} + +``` + + + +# HTML + +### list.jsp + +```html +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-12 + Time: 15:50 + 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}
+ + +``` + +### add.jsp + +```html +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-12 + Time: 17:27 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +${add} +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
租赁方式
租金
联系人
押金方式
房屋类型 + +
详细地址
+
+ + +``` + +### msg.jsp + +```html +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-12 + Time: 18:32 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +${msg} + + + +``` + -- Gitee