diff --git "a/41 \347\224\230\345\260\217\345\274\272/20230611.md" "b/41 \347\224\230\345\260\217\345\274\272/20230611.md" new file mode 100644 index 0000000000000000000000000000000000000000..66ec8630a37e1255f216cc3e896cab44e564f469 --- /dev/null +++ "b/41 \347\224\230\345\260\217\345\274\272/20230611.md" @@ -0,0 +1,476 @@ +```java +package utils; + +import java.sql.*; + +public class DBUtil { + static String url = "jdbc:mysql:///CompanyManager?useSSL=false&useUnicode=true&characterEncoding=utf8"; + static String user = "root"; + static String pwd = "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, pwd); + return conn; + } + + // 查询 + public static ResultSet query(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; + } + + // update + public static int update(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) { + e.printStackTrace(); + } + return rs; + } + +} + +``` + +```java +package bean; + +public class Dept { + private int deptId; + private String deptName; + + @Override + public String toString() { + return "Dept{" + + "deptId=" + deptId + + ", 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() { + } + + public Dept(int deptId, String deptName) { + this.deptId = deptId; + this.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; + } + + +} + +``` + +```java +package servlet; + +import bean.Dept; +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("/add") +public class AddServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String sql="select * from emp"; + ResultSet rs = DBUtil.query(sql); + ArrayList list = new ArrayList<>(); + try { + while (rs.next()){ + int id = rs.getInt("id"); + String deptName = rs.getString("deptName"); + Dept dept = new Dept(id, deptName); + list.add(dept); + } + } catch (SQLException e) { + e.printStackTrace(); + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/from.jsp").forward(request,response); + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + String empName = request.getParameter("empName"); + String sex = request.getParameter("sex"); + int age = Integer.parseInt(request.getParameter("age")); + String tel = request.getParameter("tel"); + String passWord = request.getParameter("passWord"); + String deptId = request.getParameter("deptId"); + String sql="insert into emp values (?,?,?,?,?,?,?)"; + int i = DBUtil.update(sql, 0, empName, sex, age, tel, passWord, deptId); + if (i>0){ + response.sendRedirect("/list"); + }else{ + request.setAttribute("msg","添加失败"); + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + } + + + } +} + +``` + +```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 ListServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String sql="select * from dept d ,emp e where d.DeptID=e.DeptID"; + ResultSet rs = DBUtil.query(sql); + ArrayList list = new ArrayList<>(); + 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.add(emp); + } + } 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 { + request.setCharacterEncoding("utf-8"); + String user = request.getParameter("user"); + String sql="select * from dept d ,emp e where d.DeptID=e.DeptID and empName like ?"; + ResultSet rs = DBUtil.query(sql,"%"+user+"%"); + ArrayList list = new ArrayList<>(); + 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.add(emp); + } + } catch (SQLException e) { + e.printStackTrace(); + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response); + + } +} + +``` + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-10 + Time: 16:22 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 添加 + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
姓名
性别
年龄
电话
所属部门 + +
密码 + +
+
+ + + + +``` + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-10 + Time: 15:42 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +
+ 姓名: + +
+ + + + + + + + + + + + + + + + + + + + + +
编号姓名性别年龄电话所属部门
${emp.empId}${emp.empName}${emp.sex}${emp.age}${emp.tel}${emp.deptName}
+ + + + + +``` + +```jsp +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-10 + Time: 16:39 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +${msg} +返回列表 + + + +``` +