From ac95d427f2ba2ebe5db947fd92e20100ab3c93ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E6=94=BF=E4=B8=9C?= <1067242830@qq.com> Date: Tue, 13 Jun 2023 13:29:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...41\347\220\206\347\263\273\347\273\237.md" | 438 +++++++++++++++++ ...77\345\261\213\347\256\241\347\220\206.md" | 462 ++++++++++++++++++ 2 files changed, 900 insertions(+) create mode 100644 "17 \346\236\227\346\224\277\344\270\234/20230609 \345\221\230\345\267\245\347\256\241\347\220\206\347\263\273\347\273\237.md" create mode 100644 "17 \346\236\227\346\224\277\344\270\234/20230611 \346\210\277\345\261\213\347\256\241\347\220\206.md" diff --git "a/17 \346\236\227\346\224\277\344\270\234/20230609 \345\221\230\345\267\245\347\256\241\347\220\206\347\263\273\347\273\237.md" "b/17 \346\236\227\346\224\277\344\270\234/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 0000000..bc51d66 --- /dev/null +++ "b/17 \346\236\227\346\224\277\344\270\234/20230609 \345\221\230\345\267\245\347\256\241\347\220\206\347\263\273\347\273\237.md" @@ -0,0 +1,438 @@ +# 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/17 \346\236\227\346\224\277\344\270\234/20230611 \346\210\277\345\261\213\347\256\241\347\220\206.md" "b/17 \346\236\227\346\224\277\344\270\234/20230611 \346\210\277\345\261\213\347\256\241\347\220\206.md" new file mode 100644 index 0000000..ae18d45 --- /dev/null +++ "b/17 \346\236\227\346\224\277\344\270\234/20230611 \346\210\277\345\261\213\347\256\241\347\220\206.md" @@ -0,0 +1,462 @@ +# 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 + (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 + (null,'整租',1145,'张三','押一付一',1,'福山郊区'), + (null,'合租',1133,'张三','押二付一',2,'福山郊区'), + (null,'整租',2332,'张三','押一付一',1,'福山区'), + (null,'整租',11451,'张三','押一付三',3,'福山'); +``` + +# bean + +```java +package bean; + +public class Info { + private int id; + private String mode; + private double rent; + private String contacts; + private String method; + private int house_type_id; + private String address; + private String type; + + @Override + public String toString() { + return "Info{" + + "id=" + id + + ", mode='" + mode + '\'' + + ", rent=" + rent + + ", contacts='" + contacts + '\'' + + ", method='" + method + '\'' + + ", house_type_id=" + house_type_id + + ", 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 getHouse_type_id() { + return house_type_id; + } + + public void setHouse_type_id(int house_type_id) { + this.house_type_id = house_type_id; + } + + 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 Info() { + } + + public Info(int id, String mode, double rent, String contacts, String method, int house_type_id, String address, String type) { + this.id = id; + this.mode = mode; + this.rent = rent; + this.contacts = contacts; + this.method = method; + this.house_type_id = house_type_id; + this.address = address; + this.type = type; + } +} +``` + +```java +package bean; + +public class type { + private int id; + private String type; + + @Override + public String toString() { + return "type{" + + "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 type() { + } + + public type(int id, String type) { + this.id = id; + this.type = type; + } +} +``` + +# servtler + +```java +package servtler; + +import bean.Info; +import bean.type; +import utils.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.lang.reflect.Type; +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 { + String sql = "select * from house_type"; + + ResultSet rs = DBUtil.qu(sql); + + ArrayList list = new ArrayList<>(); + try { + while (rs.next()) { + int id = rs.getInt("id"); + String type = rs.getString("type"); + type type1 = new type(id, type); + list.add(type1); + } + } catch (SQLException e) { + e.printStackTrace(); + } + request.setAttribute("list", list); + request.getRequestDispatcher("/WEB-INF/add.jsp").forward(request, response); + + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + String leaseMode = request.getParameter("leaseMode"); + String rent = request.getParameter("rent"); + String contacts = request.getParameter("contacts"); + String depositMiethod = request.getParameter("method"); + String typeId = request.getParameter("typeId"); + String address = request.getParameter("address"); + String sql = "insert into house_info values(?,?,?,?,?,?,?)"; + int i = DBUtil.up(sql, null, leaseMode, rent, contacts, depositMiethod, typeId, address); + + if (i > 0) { + response.sendRedirect("/list"); + } else { + request.setAttribute("msg", "添加失败"); + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request, response); + } + } +} +``` + +```java +package servtler; + +import bean.Info; +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 { + String sql = "select * from house_info i,house_type t where i.house_type_id=t.id"; + ResultSet rs = DBUtil.qu(sql); + ArrayList list = new ArrayList<>(); + try { + while (rs.next()){ + int id = rs.getInt("i.id"); + String mode = rs.getString("lease_mode"); + int rent = rs.getInt("rent"); + String contacts = rs.getString("contacts"); + String method = rs.getString("deposit_method"); + int tid = rs.getInt("t.id"); + String address = rs.getString("address"); + String type = rs.getString("type"); + Info info = new Info(id, mode, rent, contacts, method, tid, address, type); + list.add(info); + } + } catch (SQLException e) { + e.printStackTrace(); + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} +``` + +# utils + +```java +package utils; + +import java.sql.*; + +public class DBUtil { + static String url = "jdbc:mysql:///test?characterEncoding=utf8"; + static String uh = "root"; + static String pwa = "root"; + + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } + + public static Connection getConn() throws SQLException { + Connection conn = DriverManager.getConnection(url, uh, pwa); + return conn; + } + + public static ResultSet qu(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 up(String sql, Object... keys) { + int rs = 0; + 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.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return rs; + } +} +``` + +# jsp + +``` +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 房源信息 + + + + + + + + + + + + + + + + + + + + + + + + +
编号租赁方式租金联系人押金方式房屋类型详细地址
${a.id}${a.mode}${a.rent}${a.contacts}${a.method}${a.type}${a.address}
+ + +``` + +``` +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: 蔡庆辉 + Date: 2023/6/11 + Time: 18:55 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 添加信息 + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
租赁方式
租金
联系人
押金方式
房屋类型
详细地址
+
+ + +``` + +``` +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +${msg} +返回列表 + + +``` \ No newline at end of file -- Gitee