diff --git "a/25 \346\234\261\346\203\240\345\277\240/20230609 jsp\346\210\277\345\261\213\347\256\241\347\220\206.md" "b/25 \346\234\261\346\203\240\345\277\240/20230609 jsp\346\210\277\345\261\213\347\256\241\347\220\206.md" new file mode 100644 index 0000000000000000000000000000000000000000..1fcc638e48fa1336b46a9eb262bc5846797f950e --- /dev/null +++ "b/25 \346\234\261\346\203\240\345\277\240/20230609 jsp\346\210\277\345\261\213\347\256\241\347\220\206.md" @@ -0,0 +1,440 @@ +```JAVA +package gongju; + +import java.sql.*; + +public class DBUtil { + static String url="jdbc:mysql:///test?characterEncoding=utf8"; + static String user = "root"; + static String password = "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,password); + return conn; +} +public static ResultSet query(String sql , Object...tj) throws SQLException { + Connection conn = getconn(); + PreparedStatement pre = conn.prepareStatement(sql); + ResultSet re = null; + for (int i = 0; i < tj.length; i++) { + pre.setObject((i+1),tj[i]); + } + re = pre.executeQuery(); + return re; +} + public static int update(String sql , Object...tj) throws SQLException { + Connection conn = getconn(); + PreparedStatement pre = conn.prepareStatement(sql); + int re = 0; + for (int i = 0; i < tj.length; i++) { + pre.setObject((i+1),tj[i]); + } + re = pre.executeUpdate(); + return re; + } + public static void close(ResultSet re , PreparedStatement pre , Connection conn) throws SQLException { + if (re!=null){ + re.close(); + } + if (pre!=null){ + pre.close(); + } + if (conn!=null){ + conn.close(); + } + } +} +``` + +```JAVA +package pickaging; + +public class HouseInfo { + private int id; + private String mode; + private double rent; + private String cont; + private String method; + private int house; + private String addr; + + public HouseInfo() { + } + + public HouseInfo(int id, String mode, double rent, String cont, String method, int house, String addr) { + this.id = id; + this.mode = mode; + this.rent = rent; + this.cont = cont; + this.method = method; + this.house = house; + this.addr = addr; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getMode() { + return mode; + } + + public void setMode(String mode) { + this.mode = mode; + } + + public double getRent() { + return rent; + } + + public void setRent(double rent) { + this.rent = rent; + } + + public String getCont() { + return cont; + } + + public void setCont(String cont) { + this.cont = cont; + } + + public String getMethod() { + return method; + } + + public void setMethod(String method) { + this.method = method; + } + + public int getHouse() { + return house; + } + + public void setHouse(int house) { + this.house = house; + } + + public String getAddr() { + return addr; + } + + public void setAddr(String addr) { + this.addr = addr; + } + + @Override + public String toString() { + return "HouseInfo{" + + "id=" + id + + ", mode='" + mode + '\'' + + ", rent=" + rent + + ", cont='" + cont + '\'' + + ", method='" + method + '\'' + + ", house=" + house + + ", addr='" + addr + '\'' + + '}'; + } +} +``` + +```JAVA +package pickaging; + +public class HouseType { + private int id; + private String type; + + public HouseType() { + } + + public HouseType(int id, String type) { + this.id = id; + this.type = type; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + @Override + public String toString() { + return "HouseType{" + + "id=" + id + + ", type='" + type + '\'' + + '}'; + } +} +``` + +```JAVA +package yingyong; + +import gongju.DBUtil; +import pickaging.HouseInfo; +import pickaging.HouseType; + +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("utf8"); + response.setContentType("text/html;charset=utf8"); + String sql = "select * from house_type;"; + try { + ResultSet re = DBUtil.query(sql); + ArrayList list = new ArrayList<>(); + while (re.next()){ + int id = re.getInt("id"); + String type = re.getString("type"); + HouseType ht = new HouseType(id, type); + list.add(ht); + } + request.setAttribute("list",list); + request.getRequestDispatcher("WEB-INF/add.jsp").forward(request,response); + } catch (SQLException e) { + e.printStackTrace(); + } + + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} +``` + +```JAVA +package yingyong; + +import gongju.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.SQLException; + +@WebServlet("/insert") +public class Insert extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + doPost(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf8"); + response.setContentType("text/html;charset=utf8"); + String id = request.getParameter("id"); + String mode = request.getParameter("mode"); + String rent = request.getParameter("rent"); + String cont = request.getParameter("cont"); + String method = request.getParameter("method"); + int house = Integer.parseInt(request.getParameter("type")); + String addr = request.getParameter("addr"); + String sql = "insert into house_info values (0,?,?,?,?,?,?)"; + int i = 0; + try { + i = DBUtil.update(sql,mode, rent, cont, method, house, addr); + } catch (SQLException e) { + throw new RuntimeException(e); + } + if (i>0){ + response.getWriter().write(""); +// resp.getWriter().write(""); + }else { + response.getWriter().write("添加失败"); + } + } +} +``` + +```JAVA +package yingyong; + +import gongju.DBUtil; +import pickaging.HouseInfo; + +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("/select") +public class Select extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf8"); + response.setContentType("text/html;charset=utf8"); + String sql = "select * from house_info i left join house_type t on i.house_type_id = t.id"; + try { + ResultSet re = DBUtil.query(sql); + ArrayList list = new ArrayList<>(); + while (re.next()){ + int id = re.getInt(1); + int typeid = re.getInt(6); + String mode = re.getString(2); + double rent = re.getDouble(3); + String cont = re.getString(4); + String depo = re.getString(5); + String addr = re.getString(7); + int tid = re.getInt(8); + String type = re.getString(9); + HouseInfo hi = new HouseInfo(id, mode, rent, cont, depo, tid, addr); + list.add(hi); + } + request.setAttribute("list",list); + request.getRequestDispatcher("WEB-INF/list.jsp").forward(request,response); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} +``` + +```JSP +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-07 + Time: 11:22 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
租赁方式
租金
联系人
押金方式
房屋类型 + +
详细地址
+
+ + +``` + +```JSP +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-07 + Time: 10:59 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +
+ +
+ + + + + + + + + + + + + + + + + + + + +
编号租赁方式租金(元)联系人押金方式房屋类型详细地址
${fz.id}${fz.mode}${fz.rent}${fz.cont}${fz.method}${fz.house}${fz.addr}
+ + +``` \ No newline at end of file diff --git "a/25 \346\234\261\346\203\240\345\277\240/20230610 jsp\345\221\230\345\267\245\347\256\241\347\220\206.md" "b/25 \346\234\261\346\203\240\345\277\240/20230610 jsp\345\221\230\345\267\245\347\256\241\347\220\206.md" new file mode 100644 index 0000000000000000000000000000000000000000..94af4c1aab0e09770967d25b81edc89038cd36a4 --- /dev/null +++ "b/25 \346\234\261\346\203\240\345\277\240/20230610 jsp\345\221\230\345\267\245\347\256\241\347\220\206.md" @@ -0,0 +1,504 @@ +```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 + (null,'技术部'), + (null,'财务部'), + (null,'运营部'), + (null,'销售部'); +# 表: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) not null unique, #联系电话 不允许为空(唯一约束) + PassWord varchar(12) not null, #密码 不允许为空 + DeptID int, #部门编号外键,关联部门表DeptID字段 + foreign key (DeptID) references Dept (DeptID) +); +insert into Emp values + (null,'谢榕峰','不详','2','666','123',1), + (null,'程y','女生','20','1666','456',2), + (null,'龙飞','男','21','16666','789',3), + (null,'姜少','富哥','22','1666666','101112',4); +``` + +```java +package util; + +import java.sql.*; + +public class DBUtil { + 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; + } + public static ResultSet query(String sql,Object... keys){ + ResultSet rs = null; + try { + Connection conn = getConn(); + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + } + rs = pst.executeQuery(); + } catch (SQLException e) { + e.printStackTrace(); + } + return rs; + } + public static int update(String sql,Object... keys){ + int rs = 0; + try { + Connection conn = getConn(); + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + } + rs = pst.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + return rs; + } + public static void close(Connection conn,PreparedStatement pst,ResultSet rs) throws SQLException { + if (rs !=null){ + rs.close(); + }if (pst !=null){ + pst.close(); + }if (conn !=null){ + conn.close(); + } + } +} +package bean; + +public class Dept { + private int deptId; //部门编号 + private String 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; + } + + @Override + public String toString() { + return "Dept{" + + "deptId=" + deptId + + ", deptName='" + deptName + '\'' + + '}'; + } + + public Dept() { + } + + public Dept(int deptId, String deptName) { + this.deptId = deptId; + this.deptName = deptName; + } +} +package bean; + +public class Emp { + int empId; //员工编号 + String empName; //员工姓名 + String sex; //员工性别 + int age; //员工年龄 + String tel; //员工电话 + String passWord; //员工密码 + int deptId; //部门编号 + String 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; + } + + @Override + public String toString() { + return "Emp{" + + "empId=" + empId + + ", empName='" + empName + '\'' + + ", sex='" + sex + '\'' + + ", age=" + age + + ", tel='" + tel + '\'' + + ", passWord='" + passWord + '\'' + + ", deptId=" + deptId + + ", 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; + } +} +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("/add") +public class Add extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + ArrayList list = new ArrayList<>(); + String sql = "select * from dept"; + ResultSet rs = null; + try { + rs = DBUtil.query(sql); + while (rs.next()) { + int eId = rs.getInt(1); + String eName = rs.getString(2); + list.add(new Dept(eId, eName)); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + try { + DBUtil.close(null, null, rs); + } 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 name = request.getParameter("name"); + String sex = request.getParameter("sex"); + if (sex.equals("male")){ + sex="男"; + } else if (sex.equals("female")) { + sex="女"; + } + int age = Integer.parseInt(request.getParameter("age")); + String tel = request.getParameter("tel"); + String pass = request.getParameter("pass"); + String dId = request.getParameter("dId"); + String sql ="insert into emp values (?,?,?,?,?,?,?)"; + int i = 0; + i = DBUtil.update(sql, 0, name, sex, age, tel, pass, dId); + if (i>0){ + request.setAttribute("msg","添加成功"); + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + }else { + request.setAttribute("msg","添加成功"); + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + } + } +} +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 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("deptId"); + 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(); + } finally { + try { + DBUtil.close(null,null,rs); + } 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("deptId"); + 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(); + } finally { + try { + DBUtil.close(null,null,rs); + } catch (SQLException e) { + e.printStackTrace(); + } + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response); + } +} +``` + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: y + Date: 2023/6/11 + Time: 22:17 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="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" %> + + + Title + + +
+ 姓名: +
+ + + + + + + + + + + + + + + + + + + + + +
编号姓名性别年龄电话所属部门操作
${emp.empId}${emp.empName}${emp.sex}${emp.age}${emp.tel}${emp.deptName} + 删除 +
+添加 + + +<%-- + Created by IntelliJ IDEA. + User: y + Date: 2023/6/11 + Time: 22:18 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 友情提示 + + +${msg} +
+返回首页 + + + +``` +