diff --git "a/15 \346\262\210\347\206\231\347\245\245/20230611 \344\275\234\344\270\232.md" "b/15 \346\262\210\347\206\231\347\245\245/20230611 \344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..88f33abb8b81f98f1ecd37bbda6c77c62de0f373 --- /dev/null +++ "b/15 \346\262\210\347\206\231\347\245\245/20230611 \344\275\234\344\270\232.md" @@ -0,0 +1,268 @@ +### bean + +```java +package bean; + +public class Emp { + private int empID; + private String empName; + private String sex; + private int age; + private String tel; + private String deptID; + + @Override + public String toString() { + return "Emp{" + + "empID=" + empID + + ", empName='" + empName + '\'' + + ", sex='" + sex + '\'' + + ", age=" + age + + ", tel='" + tel + '\'' + + ", deptID='" + deptID + '\'' + + '}'; + } + + 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 getDeptID() { + return deptID; + } + + public void setDeptID(String deptID) { + this.deptID = deptID; + } + + public Emp(int empID, String empName, String sex, int age, String tel, String deptID) { + this.empID = empID; + this.empName = empName; + this.sex = sex; + this.age = age; + this.tel = tel; + this.deptID = deptID; + } + + public Emp() { + } +} + +``` + + + +### servlet + +```java +package servlet; + +import Util.Jdbc; +import bean.Emp; + +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(name = "select", value = "/select") +public class select extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + ResultSet re = Jdbc.select("select * from Dept a,emp b where a.DeptID=b.DeptID"); + ArrayList list = new ArrayList<>(); + try { + while (re.next()) { + list.add(new Emp( + re.getInt(1), + re.getString(4), + re.getString(5), + re.getInt(6), + re.getString(7), + re.getString(2) + )); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + request.setAttribute("list", list); + request.getRequestDispatcher("WEB-INF/index.jsp").forward(request, response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + String name = request.getParameter("name"); + ResultSet re = Jdbc.select("select * from Dept a,emp b where a.DeptID=b.DeptID and EmpName like ?", "%"+name+"%"); + ArrayList list = new ArrayList<>(); + try { + while (re.next()) { + list.add(new Emp( + re.getInt(1), + re.getString(4), + re.getString(5), + re.getInt(6), + re.getString(7), + re.getString(2) + )); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + request.setAttribute("list", list); + request.getRequestDispatcher("WEB-INF/index.jsp").forward(request, response); + } +} + + +``` + + + +### util + +```java +package Util; + +import java.sql.*; + +public class Jdbc { + static + { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + private static Connection getConn(){ + String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8"; + try { + return DriverManager.getConnection(url,"root","sxxddytdn"); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + public static ResultSet select(String sql ,String...arr){ + try { + PreparedStatement pst = getConn().prepareStatement(sql); + for (int i = 0; i < arr.length; i++) { + pst.setString((i+1),arr[i]); + } + return pst.executeQuery(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + public static int update(String sql ,String...arr){ + try { + PreparedStatement pst = getConn().prepareStatement(sql); + for (int i = 0; i < arr.length; i++) { + pst.setString((i+1),arr[i]); + } + return pst.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} + +``` + + + +### jsp + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-10 + Time: 15:07 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + $Title$ + + + +
+ 姓名: + +
+ + + + + + + + + + + + + + + + + + + +
编号姓名性别年龄电话所属部门
${e.empID}${e.empName}${e.sex}${e.age}${e.tel}${e.deptID}
+ + + +``` + + + + +