diff --git "a/45 \347\216\213\345\274\272/230610jsp\344\275\234\344\270\232.md" "b/45 \347\216\213\345\274\272/230610jsp\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..b5b433e6b247e57ed2210257e26756c971ccbd04 --- /dev/null +++ "b/45 \347\216\213\345\274\272/230610jsp\344\275\234\344\270\232.md" @@ -0,0 +1,1011 @@ +```java +package bean; + +public class HouseInfo { + private int id; + private String lease_mode; + private double rent; + private String contacts; + private String deposit_method; + private String house_type_id; + private String address; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getLease_mode() { + return lease_mode; + } + + public void setLease_mode(String lease_mode) { + this.lease_mode = lease_mode; + } + + public double getRent() { + return rent; + } + + public void setRent(double rent) { + this.rent = rent; + } + + public String getContacts() { + return contacts; + } + + public void setContacts(String contacts) { + this.contacts = contacts; + } + + public String getDeposit_method() { + return deposit_method; + } + + public void setDeposit_method(String deposit_method) { + this.deposit_method = deposit_method; + } + + public String getHouse_type_id() { + return house_type_id; + } + + public void setHouse_type_id(String house_type_id) { + this.house_type_id = house_type_id; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public HouseInfo(int id, String lease_mode, double rent, String contacts, String deposit_method, String house_type_id, String address) { + this.id = id; + this.lease_mode = lease_mode; + this.rent = rent; + this.contacts = contacts; + this.deposit_method = deposit_method; + this.house_type_id = house_type_id; + this.address = address; + } + + public HouseInfo() { + } +} +package bean; + +public class Type { + private int id; + private String 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; + } + + public Type(int id, String type) { + this.id = id; + this.type = type; + } + + public Type() { + } +} +servlet +package servlet; + +import Util.Jdbc; +import bean.Type; + +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(name = "add", value = "/add") +public class add extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + ArrayList list = new ArrayList<>(); + ResultSet re = Jdbc.select("select * from house_type"); + try { + while (re.next()) { + list.add(new Type(re.getInt(1),re.getString(2))); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + 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 leaseMode = request.getParameter("lease_mode"); + String rent = request.getParameter("rent"); + String contacts = request.getParameter("contacts"); + String depositMethod = request.getParameter("deposit_method"); + String type = request.getParameter("type"); + String address = request.getParameter("address"); + int i = Jdbc.update("insert into house_info values(null,?,?,?,?,?,?)", leaseMode, rent, contacts, depositMethod, type, address); + if (i>0){ + response.sendRedirect("/select"); + } + } +} +package servlet; + +import Util.Jdbc; +import bean.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(name = "select", value = "/select") +public class select extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + ResultSet re = Jdbc.select("select * from house_type a,house_info b where a.id=b.house_type_id"); + ArrayList list = new ArrayList<>(); + try { + while (re.next()){ + list.add(new HouseInfo( + re.getInt(3), + re.getString(4), + re.getDouble(5), + re.getString(6), + re.getString(7), + re.getString(2), + re.getString(9))); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + request.setAttribute("list",list); + request.getRequestDispatcher("WEB-INF/index.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} +util +package Util; + +import java.sql.*; + +public class Jdbc { + static + { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + private static Connection getConn(){ + String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8"; + try { + return DriverManager.getConnection(url,"root","sxxddytdn"); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + public static ResultSet select(String sql ,String...arr){ + try { + PreparedStatement pst = getConn().prepareStatement(sql); + for (int i = 0; i < arr.length; i++) { + pst.setString((i+1),arr[i]); + } + return pst.executeQuery(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + public static int update(String sql ,String...arr){ + try { + PreparedStatement pst = getConn().prepareStatement(sql); + for (int i = 0; i < arr.length; i++) { + pst.setString((i+1),arr[i]); + } + return pst.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: NO.1 + Date: 2023/6/9 + Time: 16:16 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
租赁方式
租金
联系人
押金方式
房屋类型 + +
详细地址
+
+ + +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: NO.1 + Date: 2023/6/9 + Time: 15:37 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + $Title$ + + + + + + + + + + + + + + + + + + + + + + + + +
编号租赁方式租金联系人押金方式房屋类型详细地址
${h.id}${h.lease_mode}${h.rent}${h.contacts}${h.deposit_method}${h.house_type_id}${h.address}
+ + +\ No newline at end of filepackage bean; + +public class HouseInfo { + private int id; + private String lease_mode; + private double rent; + private String contacts; + private String deposit_method; + private String house_type_id; + private String address; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getLease_mode() { + return lease_mode; + } + + public void setLease_mode(String lease_mode) { + this.lease_mode = lease_mode; + } + + public double getRent() { + return rent; + } + + public void setRent(double rent) { + this.rent = rent; + } + + public String getContacts() { + return contacts; + } + + public void setContacts(String contacts) { + this.contacts = contacts; + } + + public String getDeposit_method() { + return deposit_method; + } + + public void setDeposit_method(String deposit_method) { + this.deposit_method = deposit_method; + } + + public String getHouse_type_id() { + return house_type_id; + } + + public void setHouse_type_id(String house_type_id) { + this.house_type_id = house_type_id; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public HouseInfo(int id, String lease_mode, double rent, String contacts, String deposit_method, String house_type_id, String address) { + this.id = id; + this.lease_mode = lease_mode; + this.rent = rent; + this.contacts = contacts; + this.deposit_method = deposit_method; + this.house_type_id = house_type_id; + this.address = address; + } + + public HouseInfo() { + } +} +package bean; + +public class Type { + private int id; + private String 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; + } + + public Type(int id, String type) { + this.id = id; + this.type = type; + } + + public Type() { + } +} +servlet +package servlet; + +import Util.Jdbc; +import bean.Type; + +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(name = "add", value = "/add") +public class add extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + ArrayList list = new ArrayList<>(); + ResultSet re = Jdbc.select("select * from house_type"); + try { + while (re.next()) { + list.add(new Type(re.getInt(1),re.getString(2))); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + 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 leaseMode = request.getParameter("lease_mode"); + String rent = request.getParameter("rent"); + String contacts = request.getParameter("contacts"); + String depositMethod = request.getParameter("deposit_method"); + String type = request.getParameter("type"); + String address = request.getParameter("address"); + int i = Jdbc.update("insert into house_info values(null,?,?,?,?,?,?)", leaseMode, rent, contacts, depositMethod, type, address); + if (i>0){ + response.sendRedirect("/select"); + } + } +} +package servlet; + +import Util.Jdbc; +import bean.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(name = "select", value = "/select") +public class select extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + ResultSet re = Jdbc.select("select * from house_type a,house_info b where a.id=b.house_type_id"); + ArrayList list = new ArrayList<>(); + try { + while (re.next()){ + list.add(new HouseInfo( + re.getInt(3), + re.getString(4), + re.getDouble(5), + re.getString(6), + re.getString(7), + re.getString(2), + re.getString(9))); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + request.setAttribute("list",list); + request.getRequestDispatcher("WEB-INF/index.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} +util +package Util; + +import java.sql.*; + +public class Jdbc { + static + { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + private static Connection getConn(){ + String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8"; + try { + return DriverManager.getConnection(url,"root","sxxddytdn"); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + public static ResultSet select(String sql ,String...arr){ + try { + PreparedStatement pst = getConn().prepareStatement(sql); + for (int i = 0; i < arr.length; i++) { + pst.setString((i+1),arr[i]); + } + return pst.executeQuery(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + public static int update(String sql ,String...arr){ + try { + PreparedStatement pst = getConn().prepareStatement(sql); + for (int i = 0; i < arr.length; i++) { + pst.setString((i+1),arr[i]); + } + return pst.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: NO.1 + Date: 2023/6/9 + Time: 16:16 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
租赁方式
租金
联系人
押金方式
房屋类型 + +
详细地址
+
+ + +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: NO.1 + Date: 2023/6/9 + Time: 15:37 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + $Title$ + + + + + + + + + + + + + + + + + + + + + + + + +
编号租赁方式租金联系人押金方式房屋类型详细地址
${h.id}${h.lease_mode}${h.rent}${h.contacts}${h.deposit_method}${h.house_type_id}${h.address}
+ + +\ No newline at end of filepackage bean; + +public class HouseInfo { + private int id; + private String lease_mode; + private double rent; + private String contacts; + private String deposit_method; + private String house_type_id; + private String address; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getLease_mode() { + return lease_mode; + } + + public void setLease_mode(String lease_mode) { + this.lease_mode = lease_mode; + } + + public double getRent() { + return rent; + } + + public void setRent(double rent) { + this.rent = rent; + } + + public String getContacts() { + return contacts; + } + + public void setContacts(String contacts) { + this.contacts = contacts; + } + + public String getDeposit_method() { + return deposit_method; + } + + public void setDeposit_method(String deposit_method) { + this.deposit_method = deposit_method; + } + + public String getHouse_type_id() { + return house_type_id; + } + + public void setHouse_type_id(String house_type_id) { + this.house_type_id = house_type_id; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public HouseInfo(int id, String lease_mode, double rent, String contacts, String deposit_method, String house_type_id, String address) { + this.id = id; + this.lease_mode = lease_mode; + this.rent = rent; + this.contacts = contacts; + this.deposit_method = deposit_method; + this.house_type_id = house_type_id; + this.address = address; + } + + public HouseInfo() { + } +} +package bean; + +public class Type { + private int id; + private String 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; + } + + public Type(int id, String type) { + this.id = id; + this.type = type; + } + + public Type() { + } +} +servlet +package servlet; + +import Util.Jdbc; +import bean.Type; + +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(name = "add", value = "/add") +public class add extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + ArrayList list = new ArrayList<>(); + ResultSet re = Jdbc.select("select * from house_type"); + try { + while (re.next()) { + list.add(new Type(re.getInt(1),re.getString(2))); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + 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 leaseMode = request.getParameter("lease_mode"); + String rent = request.getParameter("rent"); + String contacts = request.getParameter("contacts"); + String depositMethod = request.getParameter("deposit_method"); + String type = request.getParameter("type"); + String address = request.getParameter("address"); + int i = Jdbc.update("insert into house_info values(null,?,?,?,?,?,?)", leaseMode, rent, contacts, depositMethod, type, address); + if (i>0){ + response.sendRedirect("/select"); + } + } +} +package servlet; + +import Util.Jdbc; +import bean.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(name = "select", value = "/select") +public class select extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + ResultSet re = Jdbc.select("select * from house_type a,house_info b where a.id=b.house_type_id"); + ArrayList list = new ArrayList<>(); + try { + while (re.next()){ + list.add(new HouseInfo( + re.getInt(3), + re.getString(4), + re.getDouble(5), + re.getString(6), + re.getString(7), + re.getString(2), + re.getString(9))); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + request.setAttribute("list",list); + request.getRequestDispatcher("WEB-INF/index.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} +util +package Util; + +import java.sql.*; + +public class Jdbc { + static + { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + private static Connection getConn(){ + String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8"; + try { + return DriverManager.getConnection(url,"root","sxxddytdn"); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + public static ResultSet select(String sql ,String...arr){ + try { + PreparedStatement pst = getConn().prepareStatement(sql); + for (int i = 0; i < arr.length; i++) { + pst.setString((i+1),arr[i]); + } + return pst.executeQuery(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + public static int update(String sql ,String...arr){ + try { + PreparedStatement pst = getConn().prepareStatement(sql); + for (int i = 0; i < arr.length; i++) { + pst.setString((i+1),arr[i]); + } + return pst.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: NO.1 + Date: 2023/6/9 + Time: 16:16 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
租赁方式
租金
联系人
押金方式
房屋类型 + +
详细地址
+
+ + +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: NO.1 + Date: 2023/6/9 + Time: 15:37 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + $Title$ + + + + + + + + + + + + + + + + + + + + + + + + +
编号租赁方式租金联系人押金方式房屋类型详细地址
${h.id}${h.lease_mode}${h.rent}${h.contacts}${h.deposit_method}${h.house_type_id}${h.address}
+ + +\ No newline at end of file +``` diff --git "a/45 \347\216\213\345\274\272/230611jsp\344\275\234\344\270\232.md" "b/45 \347\216\213\345\274\272/230611jsp\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..a64c70a95f18bc36f6de2674ee5003414bd792d2 --- /dev/null +++ "b/45 \347\216\213\345\274\272/230611jsp\344\275\234\344\270\232.md" @@ -0,0 +1,380 @@ +# 数据库CompanyManager + +```mysql +# 数据库名称:CompanyManager +create database CompanyManager character set utf8; +use CompanyManager; +# 表: Dept (部门信息表) +# 字段显示 字段名 数据类型 默认值 备注和说明 +create table Dept +( + DeptID int primary key auto_increment, # 部门编号主键,自动增长列 + DeptName varchar(20) not 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 Dept +values (null, '开发部'), + (null, '后勤部'), + (null, '测试部'); +insert into Emp +values (null, '张三', '女', 18, '10086', '123456', 1), + (null, '李四', '男', 24, '10087', '123456', 2), + (null, '王五', '男', 28, '10088', '123456', 3); + +``` + +# bean包 + +## Dept + +```java +package bean; + +public class Dept { + private int deptId; + private String deptName; + + public Dept() { + } + + @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(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(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; + } + + public Emp() { + } +} + +``` + +# Servlet + +```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.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +@WebServlet("/list") +public class ServletList 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 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(); + } + 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 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(); + } + request.setAttribute("list", list); + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response); + } +} + +``` + +DBUtil + +```java +package util; + +import java.sql.*; + +public class DBUtil { + static String url="jdbc:mysql:///CompanyManager?characterEncoding=utf8"; + static String use = "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, use, pwd); + return conn; + } + public static ResultSet query(String sql,Object...keys){ + ResultSet rs = null; + try { + Connection conn = getconn(); + PreparedStatement pst = conn.prepareStatement(sql); + rs = null; + 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); + rs = 0; + 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(ResultSet rs,PreparedStatement pst,Connection conn) throws SQLException { + if (rs!=null){ + rs.close(); + } + if (pst!=null){ + pst.close(); + } + if (conn!=null){ + conn.close(); + } + } +} + +``` + +# JSP + +```javascript +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-10 + Time: 16:16 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 员工列表 + + +
+ 姓名: +
+<%--b) 在列表中,需要显示列“编号”、“姓名”、“性别”、“年龄”、“电话”、“所属部门”;--%> + + + + + + + + + + + + + + + + + + + +
编号姓名性别年龄电话所属部门
${Emp.empId}${Emp.empName}${Emp.sex}${Emp.age}${Emp.tel}${Emp.deptName}
+ + + +```