diff --git "a/03 \345\276\220\351\233\250\346\231\264/20230610 JSP\346\265\213\351\252\214\342\200\224\342\200\224\345\221\230\345\267\245\344\277\241\346\201\257\347\256\241\347\220\206.md" "b/03 \345\276\220\351\233\250\346\231\264/20230610 JSP\346\265\213\351\252\214\342\200\224\342\200\224\345\221\230\345\267\245\344\277\241\346\201\257\347\256\241\347\220\206.md" new file mode 100644 index 0000000000000000000000000000000000000000..04034c9f7690e60c110e024aa4f6814306baa9f4 --- /dev/null +++ "b/03 \345\276\220\351\233\250\346\231\264/20230610 JSP\346\265\213\351\252\214\342\200\224\342\200\224\345\221\230\345\267\245\344\277\241\346\201\257\347\256\241\347\220\206.md" @@ -0,0 +1,721 @@ +```mysql +# 数据库名称:CompanyManager +create database CompanyManager charset utf8; +use CompanyManager; +# 表: Dept (部门信息表) +create table Dept +( + DeptID int primary key auto_increment, # 部门编号主键,自动增长列 + DeptName varchar(20) not null # 部门名称 不允许为空 +); +insert into Dept +values (0, '后勤部'), + (0, '法务部'), + (0, '开发部'), + (0, '人力部'), + (0, '财务部'); +# 表:Emp (员工信息表) +create table Emp +( + EmpID int primary key auto_increment, # 员工编号主键,自动增长列 + EmpName varchar(20) not null, # 员工姓名 不允许为空 + Sex char(2) not null, # 性别不允许为空 + Age int not null, # 员工年龄不允许为空 + Tel varchar(20) unique not null, # 联系电话 不允许为空(唯一约束) + PassWord varchar(12) not null, # 密码 不允许为空 + DeptID int, + foreign key (DeptID) references Dept (DeptID)# 部门编号外键,关联部门表DeptID字段 +); +insert into Emp +values (0, '季川', '男', 22, '1516816', '123', 1), + (0, '梓兰', '女', 23, '1515530', '456', 4), + (0, '华容', '女', 25, '1805862', '789', 2), + (0, '李铭', '男', 23, '1348028', '023', 5), + (0, '季静', '女', 21, '1516658', '112', 3); +``` + +```java +package bean; + +public class Dept { + private int deptId; + private String deptName; + + public Dept(int empId) { + } + + 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 Dept() { + } + + public Dept(int deptId, String deptName) { + this.deptId = deptId; + this.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(int empId) { + } + + 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() { + } + + 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; + } + + @Override + public String toString() { + return "Emp{" + + "empId=" + empId + + ", empName='" + empName + '\'' + + ", sex='" + sex + '\'' + + ", age=" + age + + ", tel='" + tel + '\'' + + ", passWord='" + passWord + '\'' + + ", deptId=" + deptId + + ", deptName='" + deptName + '\'' + + '}'; + } +} +``` + +```java +package servlet; + +import bean.Dept; +import bean.Emp; +import util.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("/add") +public class Add extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + String sql="select * from Dept"; + ResultSet rst = DBUtil.query(sql); + ArrayList list = new ArrayList<>(); + try { + while (rst.next()) { + int deptId = rst.getInt("deptId"); + String deptName = rst.getString("deptName"); + Dept dept = new Dept(deptId, deptName); + list.add(dept); + } + } catch (SQLException e) { + e.printStackTrace(); + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/add.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} +``` + +```java +package servlet; + +import bean.Dept; +import bean.Emp; +import util.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("/delete") +public class Delete extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String sql="select * from Emp"; + ResultSet rst = DBUtil.query(sql); + ArrayList list = new ArrayList<>(); + try { + while (rst.next()) { + int empId = rst.getInt("empId"); + Emp emp = new Emp(empId); + list.add(emp); + } + } catch (SQLException e) { + e.printStackTrace(); + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/delete.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 sql="delete from Emp where EmpID=?"; + int empId = Integer.parseInt(request.getParameter("empId")); + int i = DBUtil.update(sql, empId); + if(i>0){ + request.setAttribute("msg","删除成功!"); + response.sendRedirect("/list"); + }else{ + request.setAttribute("msg","删除失败!"); + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + } + } +} +``` + +```java +package servlet; + +import bean.Emp; +import util.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 List extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String sql="select * from Dept d,Emp e where d.DeptID=e.DeptID"; + ResultSet rst = DBUtil.query(sql); + ArrayList list = new ArrayList<>(); + try { + while (rst.next()) { + int empId = rst.getInt("empId"); + String empName = rst.getString("empName"); + String sex = rst.getString("sex"); + int age = rst.getInt("age"); + String tel = rst.getString("tel"); + String passWord = rst.getString("passWord"); + int deptId = rst.getInt("deptId"); + String deptName = rst.getString("deptName"); + 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 user = request.getParameter("user"); + String sql="select * from Dept d,Emp e where d.DeptID=e.DeptID and empName like ?"; + ResultSet rst = DBUtil.query(sql,"%"+user+"%"); + ArrayList list = new ArrayList<>(); + try { + while (rst.next()) { + int empId = rst.getInt("empId"); + String empName = rst.getString("empName"); + String sex = rst.getString("sex"); + int age = rst.getInt("age"); + String tel = rst.getString("tel"); + String passWord = rst.getString("passWord"); + int deptId = rst.getInt("deptId"); + String deptName = rst.getString("deptName"); + 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); + } +} +``` + +```java +package servlet; + +import util.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; + +@WebServlet("/save") +public class Save 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"); + response.setContentType("text/html;charset=utf-8"); + String empName = request.getParameter("empName"); + String sex = request.getParameter("sex"); + String age = request.getParameter("age"); + String tel = request.getParameter("tel"); + int deptName = Integer.parseInt(request.getParameter("deptName")); + String sql="insert into Emp values (0,?,?,?,?,0,?)"; + int i = DBUtil.update(sql,empName,sex,age,tel,deptName); + if(i>0){ + request.setAttribute("msg","添加成功!"); + response.sendRedirect("/list"); + }else{ + request.setAttribute("msg","添加失败!"); + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + } + + } +} +``` + +```java +package servlet; + +import bean.Dept; +import util.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("/update") +public class Update extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + String sql="select * from Dept"; + ResultSet rst = DBUtil.query(sql); + ArrayList list = new ArrayList<>(); + try { + while (rst.next()) { + int deptId = rst.getInt("deptId"); + String deptName = rst.getString("deptName"); + Dept dept = new Dept(deptId, deptName); + list.add(dept); + } + } catch (SQLException e) { + e.printStackTrace(); + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/update.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 empId = request.getParameter("empId"); + String empName = request.getParameter("empName"); + String sex = request.getParameter("sex"); + String age = request.getParameter("age"); + String tel = request.getParameter("tel"); + int deptName = Integer.parseInt(request.getParameter("deptName")); + String sql="update Emp set EmpName=?,Sex=?,Age=?,Tel=?,DeptID=? where EmpID=?"; + int i = DBUtil.update(sql, empName, sex, age, tel, deptName,empId); + if(i>0){ + request.setAttribute("msg","修改成功!"); + response.sendRedirect("list"); + }else{ + request.setAttribute("msg","修改失败!"); + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + } + } +} +``` + +```java +package util; + +import java.net.ConnectException; +import java.sql.*; + +public class DBUtil { + private static String url="jdbc:mysql:///CompanyManager?characterEncoding=utf8"; + private static String user="root"; + private static String pwd="root"; + + 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; + } + public static ResultSet query(String sql,Object... keys) { + ResultSet rst = null; + try { + Connection conn = getConn(); + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setObject((i + 1), keys[i]); + } + rst = pst.executeQuery(); + } catch (SQLException e) { + e.printStackTrace(); + } + return rst; + } + + public static int update(String sql,Object... keys){ + int rst = 0; + try { + Connection conn = getConn(); + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setObject((i + 1), keys[i]); + } + rst = pst.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + return rst; + } + + public static void close(Connection conn,PreparedStatement pst,ResultSet rst){ + try { + if (rst != null) { + rst.close(); + } + if (pst != null) { + pst.close(); + } + if (conn != null) { + conn.close(); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } +} +``` + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-10 + Time: 17:17 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 添加员工 + + +

添加员工

+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
姓名 + +
性别 + 男 + 女 +
年龄 + +
电话 + +
所属部门 + +
+ +
+ + +``` + +```jsp +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-11 + Time: 19:37 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 删除员工 + + +

删除员工

+
+ 编号: + +
+ + +``` + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-10 + Time: 15:37 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 员工信息 + + +
+ 姓名:      +
+ + + + + + + + + + + + + + + + + + + + + + +
编号姓名性别年龄电话所属部门
${e.empId}${e.empName}${e.sex}${e.age}${e.tel}${e.deptName}
+   +   + +
+ + +``` + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-11 + Time: 20:05 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 修改员工 + + +

修改员工

+

+ 编号: 
+ 姓名: 
+ 性别:  + 男 +
+ 年龄: 
+ 电话: 
+ 所属部门: +
+ +
+ + +``` + +```jsp +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-11 + Time: 19:09 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 友情提示 + + +${msg} +
+返回列表 + + +``` +