From 92251335f35e3d8d0708bb5929ac786f312e2a2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=A9=E4=B8=9C?= <3187628460@qq.com> Date: Sun, 11 Jun 2023 22:06:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=88=BF=E5=B1=8B=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...30\345\267\245\347\256\241\347\220\206.md" | 373 ++++++++++++++++++ 1 file changed, 373 insertions(+) create mode 100644 "23 \351\273\204\346\265\251\344\270\234/20230610 \345\221\230\345\267\245\347\256\241\347\220\206.md" diff --git "a/23 \351\273\204\346\265\251\344\270\234/20230610 \345\221\230\345\267\245\347\256\241\347\220\206.md" "b/23 \351\273\204\346\265\251\344\270\234/20230610 \345\221\230\345\267\245\347\256\241\347\220\206.md" new file mode 100644 index 0000000..ca2b11e --- /dev/null +++ "b/23 \351\273\204\346\265\251\344\270\234/20230610 \345\221\230\345\267\245\347\256\241\347\220\206.md" @@ -0,0 +1,373 @@ + + +```mysql +//数据库代码 +create database CompanyManager character set utf8; + +use CompanyManager; + +create table Dept( +DeptID int primary key auto_increment, # 部门编号 +DeptName varchar(20) not null #部门名称 + ); + +insert into dept values + (null,'开发部'), + (null,'后勤部'), + (null,'测试部'); +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) not null unique, # 联系电话 + PassWord varchar(12) not null, #密码 + DeptID int, # 部门编号 + foreign key (DeptID) references Dept (DeptID));insert into emp values (null,'张三','男',20,10001,041727,1), + (null,'李四','女',19,10002,041727,2), (null,'王五','男',22,10003,041727,3); +``` + +```java +//部门信息 +javascriptpackage 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) { + this.deptName = deptName; + } + public Dept() {} + public Dept(int deptID, String deptName) { + this.deptID = deptID; + this.deptName = deptName; + } +``` + +```java +// 员工信息 +package bean; +public class Emp { + int empID; + String empName; + String sex; + int age; + String tel; + String passWord; + int deptID; + String deptName; + @Override + public String toString() {return "Emp{" +"empID=" + empID +", empName='" + empName+", sex='" +sex +", age=" + age +", tel='" + tel + ", passWord='" + passWord +", 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 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; + }} +``` + +```java +//搜索数据 +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 d where e.DeptID=d.DeptID"; + ResultSet rs = DBUtil.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 deptID = rs.getInt(6); + String deptName = rs.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 emp e ,dept d where e.DeptID=d.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(6); + String deptName = rs.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 +//添加数据处理 +import bean.Dept; +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("/add") +public class AddServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String sql = "select * from dept"; + ArrayList list = new ArrayList<>(); + ResultSet rs = DBUtil.query(sql); + try { + while (rs.next()){ + int deptID = rs.getInt("deptID"); + String deptName = rs.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 { + request.setCharacterEncoding("utf-8"); + 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"); + String deptID = request.getParameter("deptID"); + String sql ="insert into emp values (?,?,?,?,?,?,?)"; + int update = DBUtil.update(sql, null, empName, sex, age, tel, passWord, deptID); + if (update>0){ + response.sendRedirect("/list"); + }else { + request.setAttribute("mgs","添加失败"); + request.getRequestDispatcher"/WEB-INF/list.jsp").forward(request,response); +``` + +```java +//工具类 +package utils; +import java.sql.*; +public class DBUtil{ + static String url = "jdbc:mysql:///CompanyManager?characterEncoding=utf8"; + static String user ="root"; + 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 ret = null; + try { + Connection conn = getConn(); + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + } + ret = pst.executeQuery(); + } catch (SQLException e) { + e.printStackTrace(); + } + return ret; + } + public static int update(String sql,Object ...keys){ + int ret = 0; + try { + Connection conn = getConn(); + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + } + ret = pst.executeUpdate(); + } + catch (SQLException e) { + e.printStackTrace(); + } + return ret; } +} +``` + +```jsp +//添加数据网页 +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +
+ + + + + + + + + + + + + +
编号
部门名称 + +
+
+
+ + +``` + +```jsp +//查看数据网页 +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +
+ 姓名: +
+ + + + + + + + + + + + + + + + + + + + + + +
编号姓名性别年龄电话所属部门
${emp.empID}${emp.empName}${emp.sex}${emp.age}${emp.tel}${emp.deptName}
+ + + + + +``` + -- Gitee