diff --git "a/36 \351\231\210\347\207\225\345\274\272/20230609\345\221\230\345\267\245\347\256\241\347\220\206\347\263\273\347\273\237.md" "b/36 \351\231\210\347\207\225\345\274\272/20230609\345\221\230\345\267\245\347\256\241\347\220\206\347\263\273\347\273\237.md" new file mode 100644 index 0000000000000000000000000000000000000000..00b476051165cc7816de9ba03a657d2412ce3519 --- /dev/null +++ "b/36 \351\231\210\347\207\225\345\274\272/20230609\345\221\230\345\267\245\347\256\241\347\220\206\347\263\273\347\273\237.md" @@ -0,0 +1,439 @@ +# MySQL语句 + +```mysql +# 数据库名称:CompanyManager +create database if not exists CompanyManager character set utf8; +use CompanyManager; +# 表: Dept (部门信息表) +create table if not exists Dept +( + DeptID int primary key auto_increment, # 部门编号 主键,自动增长列 + DeptName varchar(20) not null # 部门名称 不允许为空 +); +# 插入部门信息 +insert into Dept +values (null, '财务部'), + (null, '技术部'), + (null, '研发部'); +# 表:Emp (员工信息表) +create table if not exists 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 values + (null,'张三','男',19,'12154444','132',1), + (null,'史蒂夫','男',21,'12154448','132',2), + (null,'历史','男',19,'12154442','132',3); + + +``` + +# java语句 + +## 工具类 + +```java +package utils; + +import java.sql.*; + +public class DBUtil { + // 1定义数据库地址 数据库名 设置字符集 2用户名 3密码 工具类都是 static 的方法 + public static final String url = "jdbc:mysql://localhost:3306/CompanyManager?useUnicode=true&characterEncoding=utf8&useSSL=false"; + public static final String user = "root"; + public static final String password = "root"; + + // 注册驱动 + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + System.out.println("驱动异常!"); + e.printStackTrace(); + } + } + + // 连接数据库 + public static Connection getConn() { + Connection conn = null; + try { + conn = DriverManager.getConnection(url, user, password); + } catch (SQLException e) { + System.out.println("连接异常!"); + e.printStackTrace(); + } + return conn; + } + + /** + * 通用查询语句 + * + * @param sql 查询语句 + * @param keys 查询条件 + * @return 结果集 + */ + public static ResultSet getSelect(String sql, Object... keys) { +// 获取连接对象 + Connection conn = getConn(); + ResultSet re = null; + try { +// 预编译sql语句 + PreparedStatement pr = conn.prepareStatement(sql); +// 遍历可变数组,获取条件 + for (int i = 0; i < keys.length; i++) { + pr.setObject((i + 1), keys[i]); + } + re = pr.executeQuery(); + } catch (SQLException e) { + System.out.println("sql查询语句异常!"); + e.printStackTrace(); + } + return re; + + } + + /** + * 通用增删改语句 + * + * @param sql 增删改语句 + * @param keys 增删改条件 + * @return 影响条数 + */ + public static int getUpdate(String sql, Object... keys) { +// 获取连接对象 + Connection conn = getConn(); + int re = 0; + try { +// 预编译sql语句 + PreparedStatement pr = conn.prepareStatement(sql); +// 遍历可变数组,获取条件 + for (int i = 0; i < keys.length; i++) { + pr.setObject((i + 1), keys[i]); + } + re = pr.executeUpdate(); + } catch (SQLException e) { + System.out.println("sql查询语句异常!"); + e.printStackTrace(); + } + return re; + + } + + /** + * 关闭资源 + * + * @param conn + * @param pr + * @param re + */ + public static void getClose(Connection conn, PreparedStatement pr, ResultSet re) { + try { + if (re != null) { + re.close(); + } + if (pr != null) { + pr.close(); + } + if (conn != null) { + conn.close(); + } + } catch (SQLException e) { + System.out.println("资源释放异常!"); + } + } +} + +``` + +## 测试类 + +```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("/test/*") +public class TestServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { +// 设置字符集,避免乱码 +// 获取浏览器数据的字符集 + request.setCharacterEncoding("utf-8"); +// 服务器的字符集 + response.setCharacterEncoding("utf-8"); + response.setContentType("text/html;charset=utf-8"); + String path = request.getPathInfo(); +// 创建集合,遍历结果集,封装成一个对象存到集合里 +// 创建集合 + ArrayList list = new ArrayList<>(); + DBUtil ut = new DBUtil(); + if (path == null || path == "/") { + // 获取mysql语句 + try { + String sql = "select * from emp a left join dept d on a.DeptID = d.DeptID"; + ResultSet se = ut.getSelect(sql); +// 遍历结果集 + while (se.next()) { + int empId = se.getInt("empId"); + String empName = se.getString("empName"); + String sex = se.getString("sex"); + int age = se.getInt("age"); + String tel = se.getString("tel"); + String password = se.getString("password"); + int deptId = se.getInt("deptId"); + String deptName = se.getString("deptName"); + Emp emp = new Emp(empId, empName, sex, age, tel, password, deptId, deptName); + list.add(emp); + } + } catch (SQLException e) { + System.out.println("sql语句异常!"); + e.printStackTrace(); + } + request.setAttribute("list", list); + request.getRequestDispatcher("/WEB-INF/test.jsp").forward(request, response); + + } else if (path.matches("/add")) { + String ti = request.getParameter("ti"); + System.out.println(ti); + try { + String sql = "select * from emp a left join dept d on a.DeptID = d.DeptID where empName like ?"; + ResultSet se = ut.getSelect(sql, "%" + ti + "%"); +// 遍历结果集 + while (se.next()) { + int empId = se.getInt("empId"); + String empName = se.getString("empName"); + String sex = se.getString("sex"); + int age = se.getInt("age"); + String tel = se.getString("tel"); + String password = se.getString("password"); + int deptId = se.getInt("deptId"); + String deptName = se.getString("deptName"); + Emp emp = new Emp(empId, empName, sex, age, tel, password, deptId, deptName); + list.add(emp); + } + } catch (SQLException e) { + e.printStackTrace(); + } + request.setAttribute("list", list); + request.getRequestDispatcher("/WEB-INF/test.jsp").forward(request, response); + } + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + doGet(request, response); + } +} + +``` + +## 封装 + +```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; + 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) { + 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 + '\'' + + '}'; + } +} + +``` + +```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 + '\'' + + '}'; + } +} + +``` + +# jsp代码 + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +
+ 姓名: + +
+ + + + + + + + + + + + + + + + + + + +
编号姓名性别年龄电话所属部门
${list.empId}${list.empName}${list.sex}${list.age}${list.tel}${list.deptName}
+ + + + +``` + diff --git "a/36 \351\231\210\347\207\225\345\274\272/20230610\346\210\277\345\261\213\347\256\241\347\220\206\347\263\273\347\273\237.md" "b/36 \351\231\210\347\207\225\345\274\272/20230610\346\210\277\345\261\213\347\256\241\347\220\206\347\263\273\347\273\237.md" new file mode 100644 index 0000000000000000000000000000000000000000..ce92ead94e14cf48d14b951acb7932c4096395f6 --- /dev/null +++ "b/36 \351\231\210\347\207\225\345\274\272/20230610\346\210\277\345\261\213\347\256\241\347\220\206\347\263\273\347\273\237.md" @@ -0,0 +1,555 @@ +# mysql代码 + +```mysql +# 数据库名称:test +create database if not exists test character set utf8; +use test; +# 表:house_type (房屋类型表) +create table if not exists 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 if not exists 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 (null, '合租', 1244, '张三', '押一付三', 1, '龙岩新罗一号'), + (null, '合租', 1244, '王五', '押二付三', 2, '龙岩新罗二号'), + (null, '合租', 1244, '李四', '押一付一', 3, '龙岩新罗三号'); +``` + + + +# java代码 + +## 1.工具类 + +```java +package utils; + +import java.sql.*; + +public class DBUtil { +//主机 数据库 数据库名 字符集 + private static final String url="jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf8"; +// 数据库用户 + private static final String user="root"; +// 数据库密码 + private static final String password="root"; + +// 注册驱动 + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + System.out.println("驱动异常!"); + e.printStackTrace(); + } + } + +// 连接数据库 + + public static Connection getConn() { + Connection conn = null; + try { + conn = DriverManager.getConnection(url, user, password); + } catch (SQLException e) { + System.out.println("连接异常!"); + e.printStackTrace(); + } + return conn; + } + + /** + * 通用查询方法 + * + * @param sql mysql通用查询语句 + * @param keys 查询条件 + * @return 结果集 + */ + public static ResultSet getSelect(String sql, Object... keys) { + Connection conn = getConn(); + ResultSet re = null; + try { + PreparedStatement pr = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pr.setObject((i + 1), keys[i]); + } + re = pr.executeQuery(); + } catch (SQLException e) { + System.out.println("sql查询语句异常!"); + } + return re; + } + + /** + * 通用增删改方法 + * + * @param sql mysql通用增删改语句 + * @param keys 增删改条件 + * @return 影响行数 + */ + public static int getUpdate(String sql, Object... keys) { + Connection conn = getConn(); + int i1 = 0; + try { + PreparedStatement pr = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pr.setObject((i + 1), keys[i]); + } + i1 = pr.executeUpdate(); + } catch (SQLException e) { + System.out.println("sql增删改语句异常!"); + e.printStackTrace(); + } + return i1; + } + + /** + * 关闭资源 + * @param conn + * @param pr + * @param re + */ + public static void getClose(Connection conn, PreparedStatement pr, ResultSet re) { + try { + if (re != null) { + re.close(); + } + if (pr != null) { + pr.close(); + } + if (conn != null) { + conn.close(); + } + } catch (SQLException e) { + System.out.println("资源释放异常!"); + e.printStackTrace(); + } + + } + + +} + +``` + +## 2.封装类 + +```java +package bean; + +public class HouseInfo { + private int id; //编号 主键,自动增长列 + private String mode; //租赁方式 可以为空 + private double rent; //租金 不允许为空 + private String contacts; //联系人 可以为空 + private String method; //押金方式 可以为空 + private int typeId; //房屋编号 + private String address; //详细地址 + private String type; //房屋类型 + + public HouseInfo() { + } + + public HouseInfo(int id, String mode, double rent, String contacts, String method, int typeId, String address, String type) { + this.id = id; + this.mode = mode; + this.rent = rent; + this.contacts = contacts; + this.method = method; + this.typeId = typeId; + this.address = address; + this.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 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 getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + @Override + public String toString() { + return "HouseInfo{" + + "id=" + id + + ", mode='" + mode + '\'' + + ", rent=" + rent + + ", contacts='" + contacts + '\'' + + ", method='" + method + '\'' + + ", typeId=" + typeId + + ", address='" + address + '\'' + + ", type='" + type + '\'' + + '}'; + } +} + +``` + +```java +package bean; + +public class HouseType { + private int typeId; //房屋编号 + private String type;//房屋类型 + + public HouseType() { + } + + public HouseType(int typeId, String type) { + this.typeId = typeId; + this.type = type; + } + + public int getTypeId() { + return typeId; + } + + public void setTypeId(int typeId) { + this.typeId = typeId; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + @Override + public String toString() { + return "HouseType{" + + "typeId=" + typeId + + ", type='" + type + '\'' + + '}'; + } +} + +``` + +## 3.测试类 + +```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("/test/*") +public class TestServlet extends HttpServlet { + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + // 设置字符集,避免乱码 +// 浏览器请求的字符集 + req.setCharacterEncoding("utf-8"); +// 响应的字符集 + resp.setCharacterEncoding("utf-8"); + resp.setContentType("text/html;charset=utf-8"); +// 获取工具类对象 + DBUtil ut = new DBUtil(); + + + String path = req.getPathInfo(); + if (path.matches("/add")) { +// 获取浏览器数据 + int id = Integer.parseInt(req.getParameter("id")); + String mode = req.getParameter("mode"); + double rent = Double.parseDouble(req.getParameter("rent")); + String contacts = req.getParameter("contacts"); + String method = req.getParameter("method"); + String typeId = req.getParameter("typeId"); + String address = req.getParameter("address"); +// 定义sql语句 + String sql = "insert into house_info values (?,?,?,?,?,?,?)"; + int ins = ut.getUpdate(sql, id, mode, rent, contacts, method, typeId, address); + if (ins > 0) { + resp.sendRedirect("/test"); + } else { + req.setAttribute("mag", "添加失败!"); + req.getRequestDispatcher("").forward(req, resp); + } + } + } + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { +// 设置字符集,避免乱码 +// 浏览器请求的字符集 + req.setCharacterEncoding("utf-8"); +// 响应的字符集 + resp.setCharacterEncoding("utf-8"); + resp.setContentType("text/html;charset=utf-8"); +// 获取工具类对象 + DBUtil ut = new DBUtil(); + String path = req.getPathInfo(); + + if (path == null || path.equals("/")) { + // 建立集合,遍历结果集,获取数据 ,封装成一个对象,存入集合 + ArrayList list = new ArrayList<>(); + try { + String sql = "select * from house_info i left join house_type ht on ht.id = i.house_type_id"; + ResultSet se = ut.getSelect(sql); +// 遍历结果集 + while (se.next()) { + // 获取数据 + int id = se.getInt("i.id"); + String mode = se.getString("lease_mode"); + double rent = se.getDouble("rent"); + String contacts = se.getString("contacts"); + String method = se.getString("deposit_method"); + int typeId = se.getInt("ht.id"); + String address = se.getString("address"); + String type = se.getString("type"); + // 封装成一个对象,存入集合 + HouseInfo info = new HouseInfo(id, mode, rent, contacts, method, typeId, address, type); + list.add(info); + } + } catch (SQLException e) { + System.out.println("sql语句异常!"); + e.printStackTrace(); + } + req.setAttribute("list", list); + req.getRequestDispatcher("/WEB-INF/test.jsp").forward(req, resp); + + + } else if (path.matches("/add")) { + // 建立集合,遍历结果集,获取数据 ,封装成一个对象,存入集合 + ArrayList list = new ArrayList<>(); + try { + String sql = "select *from house_type"; + ResultSet se = ut.getSelect(sql); + while (se.next()) { + int id = se.getInt("id"); + String type = se.getString("type"); + HouseType ty = new HouseType(id, type); + list.add(ty); + } + } catch (SQLException e) { + System.out.println("sql语句执行异常!"); + e.printStackTrace(); + } + + req.setAttribute("list", list); + req.getRequestDispatcher("/WEB-INF/add.jsp").forward(req, resp); + } + } + +} + +``` + + + +# jsp 代码 + +## 1.查看 + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + + + + 查看所有学生 + + + + + + + + + + + + + + + + + + + + + + + + +
编号租赁方式租金(元)联系人押金方式房屋类型详细地址
${list.id}${list.mode}${list.rent}${list.contacts}${list.method}${list.type}${list.address}
+ + + +``` + +## 2.添加 + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 添加学生 + + +
+ + + + + + + + + + + + + + + <%-- number 数字 required 必填--%> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
租赁方式
租金
联系人
押金方式
房屋类型 + +
详细地址
+ +
+ + + +``` + +```jsp +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +${mag} + + + +``` +