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}
+ + + + +``` +