From dcc91132079e664551e47748229058761cc83106 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BE=B7=E5=BA=B7?= <1669327472@qq.com> Date: Sun, 11 Jun 2023 21:59:02 +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 --- .../20230610 \344\275\234\344\270\232.md" | 288 ++++++++++++++++++ 1 file changed, 288 insertions(+) create mode 100644 "13 \345\274\240\345\276\267\345\272\267/20230610 \344\275\234\344\270\232.md" diff --git "a/13 \345\274\240\345\276\267\345\272\267/20230610 \344\275\234\344\270\232.md" "b/13 \345\274\240\345\276\267\345\272\267/20230610 \344\275\234\344\270\232.md" new file mode 100644 index 0000000..a9e5143 --- /dev/null +++ "b/13 \345\274\240\345\276\267\345\272\267/20230610 \344\275\234\344\270\232.md" @@ -0,0 +1,288 @@ +```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) { + DeptID = deptID; + } + + public String getDeptName() { + return DeptName; + } + + public void setDeptName(String deptName) { + DeptName = deptName; + } + + public Dept() { + } + + public Dept(int deptID, String deptName) { + DeptID = deptID; + DeptName = deptName; + } +} +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(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; + } + + public Emp() { + } +} +package servlet; + +import bean.Emp; +import utls.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +@WebServlet("/list") +public class list 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"; + ResultSet rs = DBUtil.query(sql); + ArrayList list = new ArrayList<>(); + try { + while (rs.next()) { + int empID = rs.getInt("empid"); + String empName = rs.getString("empname"); + String sex = rs.getString("sex"); + int age = rs.getInt("age"); + String tel = rs.getString(5); + String passWord = rs.getString(6); + int deptId = rs.getInt(7); + String deptName = rs.getString(9); + 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 sql = "select * from emp e,dept d where e.DeptID=d.DeptID and EmpName like ?"; + String user = request.getParameter("user"); + ResultSet rs = DBUtil.query(sql,"%"+user+"%"); + ArrayList list = new ArrayList<>(); + try { + while (rs.next()) { + int empID = rs.getInt("empId"); + String empName = rs.getString("empName"); + String sex = rs.getString("sex"); + int age = rs.getInt("age"); + String tel = rs.getString(5); + String passWord = rs.getString(6); + int deptId = rs.getInt(7); + String deptName = rs.getString(9); + 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); + } +} +package utls; + +import java.sql.*; + +public class DBUtil { + static String url="jdbc:mysql:///CompanyManager?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; + } +} +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-10 + Time: 16:13 + 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}
+ + + +``` + -- Gitee