diff --git "a/50 \345\274\240\350\265\267\347\221\236/20230608 \345\221\230\345\267\245\347\256\241\347\220\206.md" "b/50 \345\274\240\350\265\267\347\221\236/20230608 \345\221\230\345\267\245\347\256\241\347\220\206.md" new file mode 100644 index 0000000000000000000000000000000000000000..83f806c9b23fedfa46f0c8e8ca22638722081621 --- /dev/null +++ "b/50 \345\274\240\350\265\267\347\221\236/20230608 \345\221\230\345\267\245\347\256\241\347\220\206.md" @@ -0,0 +1,368 @@ +```java +package bean; + +public class Dept { + private int DeptId; + private String Deptname; + + public Dept() { + } + + public Dept(int deptId, String deptname) { + 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; + } + + @Override + public String toString() { + return "Dept{" + + "DeptId=" + DeptId + + ", 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; + public Emp() { + } + + public Emp(int empID, String empName, String sex, int age, String tel, String password, int deptID, String deptname) { + EmpID = empID; + EmpName = empName; + this.sex = sex; + this.age = age; + this.tel = tel; + Password = password; + DeptID = deptID; + Deptname = deptname; + } + + public int getEmpID() { + return EmpID; + } + + public void setEmpID(int empID) { + EmpID = empID; + } + + public String getEmpName() { + return EmpName; + } + + public void setEmpName(String empName) { + 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) { + Password = password; + } + + public int getDeptID() { + return DeptID; + } + + public void setDeptID(int deptID) { + DeptID = deptID; + } + + public String getDeptname() { + return Deptname; + } + + public void setDeptname(String deptname) { + Deptname = deptname; + } +} + +``` + +```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 Emp e, Dept a where a.DeptID=e.DeptID"; + ResultSet rs = DBUtil.query(sql); + //设置一个集合 + ArrayList list = new ArrayList<>(); + //将结果封装到集合 + try { + while (rs.next()){ + int EmpID=rs.getInt(1); + String EmpName = rs.getString(2); + String Sex = rs.getString(3); + int Age = rs.getInt(4); + String Tel = rs.getString(5); + String pwd = rs.getString(6); + int DeptID = rs.getInt(7); + String DeptName = rs.getString(8); + Emp emp = new Emp(EmpID, EmpName, Sex, Age, Tel, pwd, DeptID, DeptName); + list.add(emp); + + } + } catch (SQLException e) { + e.printStackTrace(); + } + //将集合添加到request域中 + request.setAttribute("list",list); + //请求转发给一个jsp + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response); + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +``` + +```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("/select") +public class selectServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + //拒绝乱码 + request.setCharacterEncoding("UTF-8"); + //接收表单信息 + String name = request.getParameter("name"); + //查询数据库 + String sql="select * from Emp e, Dept a where a.DeptID=e.DeptID and Empname like ?"; + ResultSet rs = DBUtil.query(sql,"%"+name+'%'); + //设置一个集合 + ArrayList list = new ArrayList<>(); + //将结果封装到集合 + try { + while (rs.next()){ + int EmpID=rs.getInt(1); + String EmpName = rs.getString(2); + String Sex = rs.getString(3); + int Age = rs.getInt(4); + String Tel = rs.getString(5); + String pwd = rs.getString(6); + int DeptID = rs.getInt(7); + String DeptName = rs.getString(8); + Emp emp = new Emp(EmpID, EmpName, Sex, Age, Tel, pwd, DeptID, DeptName); + list.add(emp); + + } + } catch (SQLException e) { + e.printStackTrace(); + } + //将集合添加到request域中 + request.setAttribute("list",list); + //请求转发给一个jsp + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response); + + } + +} + +``` + +```java +package utils; + +import java.sql.*; + +public class DBUtil { + /* + 工具类中的所有成员变量都是静态的 + */ + /* +工具类中的所有成员变量都是静态的 + */ + //1.数据库地址和用户名,密码,字符集 + static String url="jdbc:mysql:///companymanager?characterEncoding=utf8"; + static String user="root"; + static String pwd="root"; + //2.注册驱动 + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } + //3.获取连接对象的方法 + public static Connection getConn() throws SQLException { + Connection conn = DriverManager.getConnection(url, user, pwd); + return conn; + } + //4.通用的查询,接收sql语句,返回结果集 + public static ResultSet query(String sql, Object... keys){ + ResultSet rs = null; + try { + //先获取连接对象 + Connection conn = getConn(); + //获取执行sql的对象 + PreparedStatement pst=conn.prepareStatement(sql); + //2.5 遍历参数,将参数赋值给?号 + for (int i = 0; i < keys.length; i++) { + try { + pst.setObject((i+1),keys[i]); + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + } + //3.执行sql,执行得到一个结果值 + rs = pst.executeQuery(); + } catch (SQLException e) { + e.printStackTrace(); + } + return rs; + } + //5.通用的UPdate,更新添加删除 + public static int update(String sql,Object... keys){ + int num=0; + try { + //先获取连接对象 + Connection conn = getConn(); + //获取执行sql的对象 + PreparedStatement pst=conn.prepareStatement(sql); + //2.5 遍历参数,将参数赋值给?号 + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + } + //3.执行sql,执行得到一个结果值 + num= pst.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + return num; + } +} + + + +``` + +```java +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +
+ 姓名: +
+<%--${list}--%> + + + + + + + + + +<%-- --%> + + + + + + + + + + +
编号姓名性别年龄电话所属部门
${a.empID}${a.empName}${a.sex}${a.age}${a.tel}${a.deptname}
+ + + +``` +