From 686bb7427a6756c05e24041dd725710bbf11bbaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=A1=E6=B3=BD=E9=92=A6?= <2978477807@qq.com> Date: Sun, 11 Jun 2023 23:43:30 +0800 Subject: [PATCH] 0610 --- .../20230610 \344\275\234\344\270\232.md" | 366 ++++++++++++++++++ 1 file changed, 366 insertions(+) create mode 100644 "08 \350\224\241\346\263\275\351\222\246/20230610 \344\275\234\344\270\232.md" diff --git "a/08 \350\224\241\346\263\275\351\222\246/20230610 \344\275\234\344\270\232.md" "b/08 \350\224\241\346\263\275\351\222\246/20230610 \344\275\234\344\270\232.md" new file mode 100644 index 0000000..b5c585b --- /dev/null +++ "b/08 \350\224\241\346\263\275\351\222\246/20230610 \344\275\234\344\270\232.md" @@ -0,0 +1,366 @@ +# util + +```java +package Util; + +import java.sql.*; + +public class DBUtil { + final static String url = "jdbc:mysql:///CompanyManager?characterEncoding=utf8"; + final static String user = "root"; + final static String psw = "root"; + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } + public static Connection getConn() { + Connection conn; + try { + conn = DriverManager.getConnection(url, user, psw); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return conn; + } + public static ResultSet Query(String sql,Object...keys) { + Connection conn = getConn(); + ResultSet rs; + try { + 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) { + throw new RuntimeException(e); + } + return rs; + } + public static void close(Connection conn,PreparedStatement pst,ResultSet rs) throws SQLException { + if (conn != null){ + conn.close(); + } + if (pst != null){ + pst.close(); + } + if (rs != null){ + rs.close(); + } + } +} +``` + +# bean + +```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) { + DeptName = deptName; + } + + public Dept(int deptId, String deptName) { + this.deptId = deptId; + DeptName = deptName; + } + + public Dept() { + } +} +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 deftId; + private String deftName; + + @Override + public String toString() { + return "Emp{" + + "empId=" + empId + + ", empName='" + empName + '\'' + + ", sex='" + sex + '\'' + + ", age=" + age + + ", tel='" + tel + '\'' + + ", passWord='" + passWord + '\'' + + ", deftId=" + deftId + + ", deftName='" + deftName + '\'' + + '}'; + } + + 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 getDeftId() { + return deftId; + } + + public void setDeftId(int deftId) { + this.deftId = deftId; + } + + public String getDeftName() { + return deftName; + } + + public void setDeftName(String deftName) { + this.deftName = deftName; + } + + public Emp(int empId, String empName, String sex, int age, String tel, String passWord, int deftId, String deftName) { + this.empId = empId; + this.empName = empName; + this.sex = sex; + this.age = age; + this.tel = tel; + this.passWord = passWord; + this.deftId = deftId; + this.deftName = deftName; + } + + public Emp() { + } +} +``` + +# dao + +```java +package Dao; + +import Util.DBUtil; +import bean.Emp; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +public class EmpDao { + public List getAllEmp(){ + ArrayList list =new ArrayList<>(); + String sql = "select * from Dept D,Emp E where D.DeptID = E.DeptID"; + ResultSet rs = DBUtil.Query(sql); + 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("Tel"); + String passWord = rs.getString("PassWord"); + int deptID = rs.getInt("DeptID"); + String deptName = rs.getString("DeptName"); + Emp emp = new Emp(empID, empName, sex, age, tel, passWord, deptID, deptName); + list.add(emp); + } + DBUtil.close(null,null,rs); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return list; + } + public List getNameEmp(String user){ + Emp emp = null; + 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 empID = rs.getInt("EmpID"); + String empName = rs.getString("EmpName"); + String sex = rs.getString("Sex"); + int age = rs.getInt("Age"); + String tel = rs.getString("Tel"); + String passWord = rs.getString("PassWord"); + int deptID = rs.getInt("DeptID"); + String deptName = rs.getString("DeptName"); + emp = new Emp(empID, empName, sex, age, tel, passWord, deptID, deptName); + list.add(emp); + } + DBUtil.close(null,null,rs); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return list; + } + public void addEmp(){ + + String dsql = "insert into Dept values (null , ?)"; + String esql = "insert into Emp values (null,?,?,?,?,?,?)"; + + } +} +``` + +# servlet + +```java +package Servlet; + +import Dao.EmpDao; +import bean.Emp; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.util.List; + +@WebServlet("/list") +public class list extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("UTF-8"); + response.setContentType("text/html;charset=utf-8"); + String pathInfo = request.getPathInfo(); + System.out.println("pathInfo = " + pathInfo); + EmpDao dao = new EmpDao(); + List list = dao.getAllEmp(); + 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"); + response.setContentType("text/html;charset=utf-8"); + EmpDao dao = new EmpDao(); + String user = request.getParameter("user"); + String pathInfo = request.getPathInfo(); + System.out.println("pathInfo = " + pathInfo); + List list = dao.getNameEmp(user); + request.setAttribute("list", list); + request.getRequestDispatcher("WEB-INF/list.jsp").forward(request, response); + } +} +``` + +# list + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-10 + Time: 15:54 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 查看 + + + +
+ 姓名: +
+ + + + + + + + + + + + + + + + + + + +
编号姓名性别年龄电话所属部门
${emp.empId}${emp.empName}${emp.sex}${emp.age}${emp.tel}${emp.deftName}
+ + +``` + + + -- Gitee