From f42278b9289891d6c31f5230e82241dc7a7ffef1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?18=20=E9=99=88=E9=9B=85=E5=A9=B7?= <1754342529@qq.com> Date: Mon, 12 Jun 2023 19:44:58 +0800 Subject: [PATCH] 20230610 --- .../20230609.md" | 375 ++++++++++++++ .../20230610.md" | 465 ++++++++++++++++++ 2 files changed, 840 insertions(+) create mode 100644 "18 \351\231\210\351\233\205\345\251\267/20230609.md" create mode 100644 "18 \351\231\210\351\233\205\345\251\267/20230610.md" diff --git "a/18 \351\231\210\351\233\205\345\251\267/20230609.md" "b/18 \351\231\210\351\233\205\345\251\267/20230609.md" new file mode 100644 index 0000000..6409e16 --- /dev/null +++ "b/18 \351\231\210\351\233\205\345\251\267/20230609.md" @@ -0,0 +1,375 @@ +```mysql +# 数据库名称:CompanyManager +create database CompanyManager char set 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,'10086','123',1), + (0,'李四','女',19,'10087','123',2), + (0,'王五','男',17,'10088','123',3); +``` + +bean + +```java +package bean; + +public class Dept { + private int deptId; + private String deptName; + + 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; + } + + public Dept() { + } + + @Override + public String toString() { + return "Dept{" + + "deptId=" + deptId + + ", deptName='" + deptName + '\'' + + '}'; + } +} + +``` + +```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; + + @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() { + } + + 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; + } +} + +``` + +untils + +```java +package utils; + +import javax.swing.*; +import java.net.ConnectException; +import java.sql.*; + +public class DBUtil { + //定义主机、用户名、密码、字符集 + public static final String url="jdbc:mysql:///companymanager?characterEncoding=utf8"; + public static final String user="root"; + public static final String password="root"; + //1.注册驱动 + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + //2.获取连接对象 + public static Connection getConn() throws SQLException { + Connection conn = DriverManager.getConnection(url, user, password); + return conn; + } + //通用查询方法 + public static ResultSet select(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) { + throw new RuntimeException(e); + } + return rs; + } + //修改查询方法 + public static int upadte(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) { + throw new RuntimeException(e); + } + return num; + } + //释放资源 + public static void close(PreparedStatement pst,ResultSet rs,Connection conn){ + try { + if (conn!=null){ + conn.close(); + } + if (rs!=null){ + rs.close(); + } + if (conn!=null){ + conn.close(); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} + +``` + +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.Array; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +@WebServlet("/One") +public class OneServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + String sql="select * from Emp e,Dept d where e.deptId=d.deptId"; + ArrayListlist=new ArrayList<>(); + ResultSet rs= DBUtil.select(sql); + 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); + String deptName=rs.getString(8); + Emp emp = new Emp(empId, empName, sex, age, tel, passWord, deptId, deptName); + list.add(emp); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/allemp.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +``` + +```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; + +import static utils.DBUtil.user; + +@WebServlet("/user") +//要与表单的name同名 +public class TwoServlet 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("utf8"); + String sql="select * from Emp e,Dept d where e.deptId=d.deptId and empName like ?"; + ArrayList list=new ArrayList<>(); + //获取表单数据,要与 + String user = request.getParameter("user"); + ResultSet rs= DBUtil.select(sql,"%"+user+"%"); + 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); + String deptName=rs.getString(8); + Emp emp = new Emp(empId, empName, sex, age, tel, passWord, deptId, deptName); + list.add(emp); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/allemp.jsp").forward(request,response); + } +} + +``` + diff --git "a/18 \351\231\210\351\233\205\345\251\267/20230610.md" "b/18 \351\231\210\351\233\205\345\251\267/20230610.md" new file mode 100644 index 0000000..337356a --- /dev/null +++ "b/18 \351\231\210\351\233\205\345\251\267/20230610.md" @@ -0,0 +1,465 @@ +```mysql +# 数据库名称:test +create database test char set 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 (null, '两室一厅'), + (null, '三室两厅'), + (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_info values + (0,'合租',800,'张三','押一付三',1,'龙岩地球村1号楼'), + (0,'整租',1800,'李四','押一付一',2,'龙岩地球村2号楼'), + (0,'整租',2000,'王五','押三付四',3,'龙岩地球村3号楼'); + + +``` + +bean + +```java +package bean; + +public class HouseInfo { + private int id; + private String lease_mode; + private double rent; + private String contacts; + private String method; + private int typeId; + private String address; + private String type1; + + public HouseInfo(int id, String lease_mode, double rent, String contacts, String method, int typeId, String address, String type1) { + this.id = id; + this.lease_mode = lease_mode; + this.rent = rent; + this.contacts = contacts; + this.method = method; + this.typeId = typeId; + this.address = address; + this.type1 = type1; + } + + @Override + public String toString() { + return "HouseInfo{" + + "id=" + id + + ", lease_mode='" + lease_mode + '\'' + + ", rent=" + rent + + ", contacts='" + contacts + '\'' + + ", method='" + method + '\'' + + ", typeId=" + typeId + + ", address='" + address + '\'' + + ", type1='" + type1 + '\'' + + '}'; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getLease_mode() { + return lease_mode; + } + + public void setLease_mode(String lease_mode) { + this.lease_mode = lease_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 getType1() { + return type1; + } + + public void setType1(String type1) { + this.type1 = type1; + } + + public HouseInfo() { + } +} + +``` + +```java +package bean; + +public class HouseType { + private int id; + private String type; + + public HouseType(int id, String type) { + this.id = id; + this.type = type; + } + + @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() { + } +} + +``` + +until + +```java +package utils; + +import java.sql.*; + +public class DBUtil { + private static final String url="jdbc:mysql:///test?characterEncoding=utf8"; + private static final String user="root"; + private static final String psw="root"; + + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + + public static Connection getConn() throws SQLException { + Connection conn = DriverManager.getConnection(url, user, psw); + return conn; + } + + public static ResultSet select(String sql,Object...keys){ + try { + Connection conn = getConn(); + PreparedStatement pst= conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + } + ResultSet rs= pst.executeQuery(); + return rs; + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + public static int update(String sql,Object...keys){ + try { + Connection conn = getConn(); + PreparedStatement pst= conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + } + int rs= pst.executeUpdate(); + return rs; + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + +} + +``` + +servlet + +```java +package servlet; + +import bean.HouseInfo; +import bean.HouseType; +import utils.DBUtil; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +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 req, HttpServletResponse resp) throws ServletException, IOException { + String sql="select * from test.house_type"; + ResultSet rs = DBUtil.select(sql); + ArrayListlist=new ArrayList<>(); + try { + while (rs.next()){ + int id= rs.getInt("id"); + String type= rs.getString("type"); + HouseType a = new HouseType(id, type); + list.add(a); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + req.setAttribute("list",list); + req.getRequestDispatcher("/WEB-INF/All.jsp").forward(req,resp); + + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + req.setCharacterEncoding("utf-8"); + //获取表单数据 + String lease_mode=req.getParameter("lease_mode"); + String rent=req.getParameter("rent"); + String contacts=req.getParameter("contacts"); + String method=req.getParameter("method"); + String typeId=req.getParameter("typeId"); + String address=req.getParameter("address"); + String sql="insert into house_info values (?,?,?,?,?,?,?)"; + int i = DBUtil.update(sql,null,lease_mode,rent,contacts,method,typeId,address); + if (i>0){ + resp.sendRedirect("/list"); + }else { + resp.getWriter().write("添加失败"); + } + } +} + +``` + +```java +package servlet; + +import bean.HouseInfo; +import utils.DBUtil; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +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 req, HttpServletResponse resp) throws ServletException, IOException { + String sql="select * from house_info i,house_type t where i.house_type_id=t.id"; + ResultSet rs = DBUtil.select(sql); + ArrayListlist=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); + int tid= rs.getInt(8); + String type1= rs.getString(9); + HouseInfo info = new HouseInfo(id, mode, rent, contacts, method, typeId, address,type1); + list.add(info); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + req.setAttribute("list",list); + req.getRequestDispatcher("/WEB-INF/list.jsp").forward(req,resp); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + + } +} + +``` + +add + +```html +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: 17543 + Date: 2023/6/11 + Time: 22:05 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + +'789 + Title + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
租赁方式
租金
联系人
押金方式
房屋类型 +
详细地址
+ + + +``` + +All + +```java +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: 17543 + Date: 2023/6/11 + Time: 21:31 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + + + + + + + + + + + + + + + + + + + + + + +
编号租赁方式租金联系人押金方式房屋类型详细地址
${list.id}${list.lease_mode}${list.rent}${list.contacts}${list.method}${list.type1}${list.address}
+ + + + +``` + -- Gitee