diff --git "a/01 \350\213\217\346\270\205\345\215\216/20230529 \344\277\256\346\224\271\345\210\240\351\231\244.md" "b/01 \350\213\217\346\270\205\345\215\216/20230529 \344\277\256\346\224\271\345\210\240\351\231\244.md" index a3f1caa69894fa6c7fac42bf79024cf7df343a17..f654b9daca7e1471dcf1ad48868d84685e87186e 100644 --- "a/01 \350\213\217\346\270\205\345\215\216/20230529 \344\277\256\346\224\271\345\210\240\351\231\244.md" +++ "b/01 \350\213\217\346\270\205\345\215\216/20230529 \344\277\256\346\224\271\345\210\240\351\231\244.md" @@ -1,12 +1,120 @@ -```mysql -create database student_db CHARSET utf8; -use student_db; -create table student(id int PRIMARY key, -name VARCHAR(10), -sex char(5) default '女'); -insert into student values -(1,"高坂穗乃果","女"), -(2,"园田海未","女"), -(3,"南琴梨","女"); +## 添加 + +```java +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.io.PrintWriter; +import java.sql.SQLException; + +@WebServlet("/addStu") +public class addStudent extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + response.setContentType("text/html;charset=utf-8"); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + response.setContentType("text/html;charset=utf-8"); + PrintWriter writer = response.getWriter(); + String sql = "insert into stu values (?,?,?)"; + String id = request.getParameter("id"); + String name = request.getParameter("name"); + String sex = request.getParameter("sex"); + try { + int i = DBUtil.Update(sql, id, name, sex); + if (i>0){ + writer.write("添加成功"); + }else { + writer.write("添加失败"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +``` + +## 删除 + +```java +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.io.PrintWriter; +import java.sql.SQLException; + +@WebServlet("/deleteStu") +public class deleteStudent 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"); + PrintWriter writer = response.getWriter(); + String sql = "delete from stu where id = ?"; + String id = request.getParameter("id"); + try { + int i = DBUtil.Update(sql, id); + if (i>0){ + writer.write("删除成功"); + }else { + writer.write("删除失败"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} + ``` +## 修改 + +```java +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.io.PrintWriter; +import java.sql.SQLException; + +@WebServlet("/updateStu") +public class updateStudent 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=utf8"); + PrintWriter writer = response.getWriter(); + String sql = "update stu set name=?,sex=? where id=?"; + String id = request.getParameter("id"); + String name = request.getParameter("name"); + String sex = request.getParameter("sex"); + try { + int i = DBUtil.Update(sql, name, sex, id); + if (i>0){ + writer.write("修改成功"); + }else { + writer.write("修改失败"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} + +``` diff --git "a/01 \350\213\217\346\270\205\345\215\216/20230610 \345\221\230\345\267\245.md" "b/01 \350\213\217\346\270\205\345\215\216/20230610 \345\221\230\345\267\245.md" new file mode 100644 index 0000000000000000000000000000000000000000..1bd12fdf286c0654c485f42d418e968dc0bbec71 --- /dev/null +++ "b/01 \350\213\217\346\270\205\345\215\216/20230610 \345\221\230\345\267\245.md" @@ -0,0 +1,569 @@ +# 数据库 + +```mys +# 数据库名称:test +create database test charset utf8; +use test; +# 表:house_type (房屋类型表) +# 字段显示 字段名 数据类型 默认值 备注和说明 +create table house_type +( + id int primary key auto_increment, # 编号 + type varchar(50) not null # 房屋类型 +); +insert into house_type value + (1, '2室1厅1卫'), + (2, '3室2厅1卫'), + (3, '3室1厅1卫'); +# 表:house_info (房源信息表) +# 字段显示 字段名 数据类型 默认值 备注和说明 +create table house_info +( + id int primary key auto_increment, # 编号 + lease_mode varchar(50), # 租赁方式 + rent double not null, # 租金 + contacts varchar(20), # 联系人 + deposit_method varchar(20), # 押金方式 + house_type_id int, # 房屋类型 + address varchar(200) not null, # 详细地址 + foreign key (house_type_id) references house_type (id) +); + +insert into house_info value + (1, '整租', 2300, null, '押一付一', 1, '沙湖港北路166号'), + (2, '整租', 3700, '王晓明', '押一付三', 1, '武昌傅家坡紫阳东路6号'), + (3, '整租', 2500, null, '押一付三', 2, '汉阳王家湾'); + + +``` + +# bean + +## Dept + +```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; + } +} + +``` + +## Emp + +```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; + + @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; + } +} + +``` + + + +# servlet + +## 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); + } +} + +``` + +# jsp + +## addEmp + +```java +<%@ 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 + +```java +<%-- + 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}删除
+ + + +``` + +## list + +```java +<%@ 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}修改删除
+ + + + +``` + +## msg + +```java +<%-- + 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} +返回列表 + + + +``` +