From 002ad74bddffe015bf12999bd6aa5aed216523b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=AC=A3?= <3188923799@qq.com> Date: Sun, 11 Jun 2023 22:58:47 +0800 Subject: [PATCH] tree --- .../20230611 \344\275\234\344\270\232.md" | 476 ++++++++++++++++++ 1 file changed, 476 insertions(+) create mode 100644 "31 \346\235\216\346\254\243/20230611 \344\275\234\344\270\232.md" diff --git "a/31 \346\235\216\346\254\243/20230611 \344\275\234\344\270\232.md" "b/31 \346\235\216\346\254\243/20230611 \344\275\234\344\270\232.md" new file mode 100644 index 0000000..64d37a1 --- /dev/null +++ "b/31 \346\235\216\346\254\243/20230611 \344\275\234\344\270\232.md" @@ -0,0 +1,476 @@ +```java +package 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 pwd; + int deptid; + 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() { + } + + 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; + } +} + +``` + +```java +package dbutil; + +import java.sql.*; + +public class DBUtil { + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + public static Connection getConn() throws SQLException { + Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/CompanyManager?useSSL=false&useUnicode=true&characterEncoding=utf8", "root", "1234"); + return conn; + } + public static ResultSet query(String sql,Object... keys) throws SQLException { + Connection conn = getConn(); + PreparedStatement pre = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pre.setObject((i+1),keys[i]); + } + ResultSet re = pre.executeQuery(); + return re; + } + public static int update(String sql,Object... keys) throws SQLException { + Connection conn = getConn(); + PreparedStatement pre = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pre.setObject((i+1),keys[i]); + } + int i = pre.executeUpdate(); + return i; + } + public static void close(Connection conn,PreparedStatement pre,ResultSet re){ + try { + if(conn!=null){ + conn.close(); + } + if(pre!=null){ + pre.close(); + } + if(re!=null) { + re.close(); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + + } + +} + +``` + +```java +package servlet; + +import bean.Dept; +import dbutil.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.Array; +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 query = null; + try { + query = DBUtil.query(sql); + while (query.next()){ + int deptid = query.getInt(1); + String deptname = query.getString(2); + Dept dept = new Dept(deptid,deptname); + list.add(dept); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + DBUtil.close(null,null,query); + 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 pwd = request.getParameter("pwd"); + String deptid = request.getParameter("deptid"); + + String sql ="insert into emp values (0,?,?,?,?,?,?)"; + int i = 0; + try { + i = DBUtil.update(sql, empname, sex, age, tel, pwd, deptid); + } catch (SQLException e) { + throw new RuntimeException(e); + } + if (i>0){ + request.setAttribute("list","添加成功"); + request.getRequestDispatcher("/WEB-INF/save.jsp"); + } else { + request.setAttribute("list","添加失败"); + request.getRequestDispatcher("/WEB-INF/save.jsp"); + } + } +} + +``` + +```java +package servlet; + +import bean.Emp; +import dbutil.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 d.DeptID=e.DeptID order by EmpID"; + ArrayList list = new ArrayList<>(); + ResultSet re = null; + try { + re = DBUtil.query(sql); + while (re.next()){ + int empid = re.getInt(1); + String empname = re.getString(2); + String sex = re.getString(3); + int age = re.getInt(4); + String tel = re.getString(5); + String pwd = re.getString(6); + int deptid = re.getInt(7); + String deptname = re.getString(9); + Emp emp = new Emp(empid,empname,sex,age,tel,pwd,deptid,deptname); + list.add(emp); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + DBUtil.close(null,null,re); + 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 empname1 = request.getParameter("empname"); + String sql ="select * from dept d,emp e where d.DeptID=e.DeptID and empname like ?"; + ArrayList list = new ArrayList<>(); + ResultSet re = null; + try { + re = DBUtil.query(sql,"%"+empname1+"%"); + while (re.next()){ + int empid = re.getInt(3); + String empname = re.getString(4); + String sex = re.getString(5); + int age = re.getInt(6); + String tel = re.getString(7); + String pwd = re.getString(8); + int deptid = re.getInt(9); + String deptname = re.getString(2); + Emp emp = new Emp(empid,empname,sex,age,tel,pwd,deptid,deptname); + list.add(emp); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + DBUtil.close(null,null,re); + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response); + + } +} + +``` + +```html +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-10 + Time: 16:15 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
姓名
性别
年龄
电话
密码
部门名称 + +
+
+ + + + +``` + +```html +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-10 + Time: 15:38 + 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}
+ + + +``` + +```html +<%-- + Created by IntelliJ IDEA. + User: leafrosey + Date: 2023/6/11 + Time: 20:55 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +${add} +返回 + + + +``` + -- Gitee