diff --git "a/05 \346\236\227\344\274\237\345\275\254/20230610 \345\244\247\350\200\203\347\273\203\344\271\240\345\233\233\357\274\210\345\221\230\345\267\245\347\256\241\347\220\206\347\263\273\347\273\237\357\274\211.md" "b/05 \346\236\227\344\274\237\345\275\254/20230610 \345\244\247\350\200\203\347\273\203\344\271\240\345\233\233\357\274\210\345\221\230\345\267\245\347\256\241\347\220\206\347\263\273\347\273\237\357\274\211.md" new file mode 100644 index 0000000000000000000000000000000000000000..49fcab3cfcf4762cf53697a726f680a58ec8b1b5 --- /dev/null +++ "b/05 \346\236\227\344\274\237\345\275\254/20230610 \345\244\247\350\200\203\347\273\203\344\271\240\345\233\233\357\274\210\345\221\230\345\267\245\347\256\241\347\220\206\347\263\273\347\273\237\357\274\211.md" @@ -0,0 +1,641 @@ +# 作业 + +## bean + +### dept + +```java +package bean; + +public class Dept { + private int deptId; + private String depName; + + public int getDeptId() { + return deptId; + } + + public void setDeptId(int deptId) { + this.deptId = deptId; + } + + public String getDepName() { + return depName; + } + + public void setDepName(String depName) { + this.depName = depName; + } + + public Dept(int deptId, String depName) { + this.deptId = deptId; + this.depName = depName; + } + + public Dept() { + } +} + +``` + +### emp + +```java +package bean; + +public class Emp { + private int empId; //员工编号 + private String empName; //员工姓名 + private String sex; //员工性别 + private int age; //员工年龄 + private String tel; //员工电话 + private String pwd; + private int deptId; //所属部门编号 + private String deptName; //所属部门名称 + + @Override + public String toString() { + return "Emp{" + + "empId=" + empId + + ", empName='" + empName + '\'' + + ", sex='" + sex + '\'' + + ", age=" + age + + ", tel='" + tel + '\'' + + ", pwd='" + pwd + '\'' + + ", 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 getPwd() { + return pwd; + } + + public void setPwd(String pwd) { + this.pwd = pwd; + } + + 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 pwd, int deptId, String deptName) { + this.empId = empId; + this.empName = empName; + this.sex = sex; + this.age = age; + this.tel = tel; + this.pwd = pwd; + this.deptId = deptId; + this.deptName = deptName; + } + + public Emp() { + } +} + + +``` + +## servlet + +### ListServlet + +```java +package servlet; + +import UIils.DBUtils; +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 = "/list") +public class ListServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + //编写sql从数据库联表查询所有员工信息及部门 + String sql ="select * from emp e,dept d where e.DeptID=d.DeptID"; + ResultSet rs = DBUtils.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("tel"); + String passWord = rs.getString("password"); + int depId = rs.getInt("depid"); + String depName = rs.getString("depname"); + Emp emp = new Emp(empId, empName, sex, age, tel, passWord, depId, depName); + 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 { + + } +} +``` + +### AddEmp + +```java +package servlet; + +import Util.DBUtil; +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("/addEmp/*") +public class AddEmp 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 sql="select * from emp e,dept d where d.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 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/addEmp.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"); + String pathInfo = request.getPathInfo(); + if (pathInfo==null || pathInfo.equals("/")){ + String empName = request.getParameter("empName"); + String sex = request.getParameter("sex"); + String age = request.getParameter("age"); + String tel = request.getParameter("tel"); + String passWord = request.getParameter("passWord"); + Integer deptId = Integer.parseInt(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("/EmpList"); + }else{ + request.setAttribute("msg","添加失败"); + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + } + }else if(pathInfo.matches("/\\d+")){ + String empid = request.getParameter("empid"); + String empName = request.getParameter("empName"); + String sex = request.getParameter("sex"); + String age = request.getParameter("age"); + String tel = request.getParameter("tel"); + String passWord = request.getParameter("passWord"); + Integer deptId = Integer.parseInt(request.getParameter("deptId")); + String sql="update emp set empName=?,sex=?,age=?,tel=?,passWord=?,deptId=? where empid=?"; + int i = DBUtil.update(sql,empName,sex,age,tel,passWord,deptId,empid); + if(i>0){ + response.sendRedirect("/EmpList"); + }else{ + request.setAttribute("msg","修改失败"); + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + } + } + } +} +``` + +### DelEmp + +```java +package servlet; + +import Util.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; + +@WebServlet( "/") +public class DelEmp 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 empid = request.getParameter("id"); + String sql="delete from emp where empid=?"; + int i = DBUtil.update(sql, empid); + if(i>0){ + response.sendRedirect("/EmpList"); + }else{ + request.setAttribute("msg","删除失败"); + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + } + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} +``` + +### EmpList + +```java +package servlet; + +import Util.DBUtil; +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( "/EmpList") +public class EmpList 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 sql="select * from emp e,dept d where d.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 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"); + response.setContentType("text/html;charset=utf-8"); + String user = request.getParameter("user"); + String sql="select * from emp e,dept d 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(1); + String empName = rs.getString(2); + String sex = rs.getString(3); + int age = rs.getInt(4); + 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); + } +} +``` + + + +## Ulils + +### DBUtil + +```java +package UIils; + +import java.sql.*; + +public class DBUtils { + //定义主机,数据库,编码,用户名,密码 + static String url ="jdbc:mysql:///CompanyManager?characterEncoding=utf8"; + static String user ="root"; + static String pwd ="123456"; + //注册 + static{ + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } + //获取连接 + public static Connection getConn() throws SQLException { + Connection conn = DriverManager.getConnection(url, user, pwd); + return conn; + } + //查询 ,接sql,返回结果集 + public static ResultSet query(String sql,Object... keys){ + ResultSet rs = null; + try { + //连接 + Connection conn = getConn(); + //pst + PreparedStatement pst = conn.prepareStatement(sql); + //keys.fori + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + } + //rs + 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(); + //pst + PreparedStatement pst = conn.prepareStatement(sql); + //keys.fori + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + } + //rs + rs = pst.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + return rs; + + } +} + +``` + +## jsp + +### list + +```html +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-10 + Time: 16:02 + 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.deptName}修改删除
+ + + +``` + +### addEmp + +```html +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-10 + Time: 16:31 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 添加员工信息 + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
姓名:
性别:
年龄:
电话:
密码:
部门名称: + +
+ + + +
+ + + +``` + +### delete + +```html +<%-- + Created by IntelliJ IDEA. + User: sqh + Date: 2023/6/11 + Time: 20:22 + 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}删除
+ + +``` + +### msg + +```html +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-10 + Time: 17:19 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +${msg} +返回列表 + + +``` \ No newline at end of file