diff --git "a/21 \346\264\252\347\220\246\347\232\223/0609.md" "b/21 \346\264\252\347\220\246\347\232\223/0609.md" new file mode 100644 index 0000000000000000000000000000000000000000..a7e6d394fac690228b70c6c5c7c277983f76fe53 --- /dev/null +++ "b/21 \346\264\252\347\220\246\347\232\223/0609.md" @@ -0,0 +1,330 @@ +类 + +```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) { + DeptID = deptID; + } + + public String getDeptName() { + return DeptName; + } + + public void setDeptName(String deptName) { + DeptName = deptName; + } + + public Dept() { + } + + public Dept(int deptID, String deptName) { + DeptID = deptID; + DeptName = deptName; + } +} +``` + +```java +package bean; + +public class Emp { + int id; + String name; + String sex; + int age; + String tel; + String pwd; + int deptID; + String deptName; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + 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 id, String name, String sex, int age, String tel, String pwd, int deptID, String deptName) { + this.id = id; + this.name = name; + this.sex = sex; + this.age = age; + this.tel = tel; + this.pwd = pwd; + this.deptID = deptID; + this.deptName = deptName; + } +} +``` + +servlet + +```java +package servlet; + +import bean.Emp; +import utils.DBUtils; + +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("/All") +public class AllServlet 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 = DBUtils.query(sql); + ArrayList list = new ArrayList<>(); + try { + while(rs.next()){ + int id = rs.getInt(1); + String name = rs.getString(2); + String sex=rs.getString(3); + int age = rs.getInt(4); + String tel = rs.getString(5); + String pwd = rs.getString(6); + int deptId = rs.getInt(7); + String DeptName = rs.getString(9); + Emp emp = new Emp(id, name, sex, age, tel, pwd, deptId,DeptName); + list.add(emp); + } + } catch (SQLException e) { + e.printStackTrace(); + } + request.setAttribute("list",list); + request.getRequestDispatcher("WEB-INF/lib/All.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} +``` + +```java +package servlet; + +import bean.Emp; +import utils.DBUtils; + +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("/s") +public class sServlet 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("utf8"); + String user=request.getParameter("user"); + String sql="select * from emp e,dept d where e.deptid=d.DeptID and EmpName like ?"; + ResultSet rs = DBUtils.query(sql, "%"+user+"%"); + ArrayList list = new ArrayList<>(); + try { + while(rs.next()){ + int id = rs.getInt(1); + String name = rs.getString( 2); + String sex=rs.getString(3); + int age = rs.getInt(4); + String tel = rs.getString(5); + String pwd = rs.getString(6); + int deptId = rs.getInt(7); + String DeptName = rs.getString(9); + Emp emp = new Emp(id, name, sex, age, tel, pwd, deptId,DeptName); + list.add(emp); + } + } catch (SQLException e) { + e.printStackTrace(); + } + request.setAttribute("list",list); + request.getRequestDispatcher("WEB-INF/lib/All.jsp").forward(request,response); + } +} +``` + +工具类 + +```java +package utils; + +import java.sql.*; +import java.util.Enumeration; + +public class DBUtils { + static String url="jdbc:mysql:///companymanager?useSSL=false&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 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 num=0; + try { + Connection conn = getConn(); + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + } + num = pst.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + return num; + } +} +``` + +jsp + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 员工列表 + + +
+ 姓名: +
+ + + + + + + + + + + + + + + + + + + + +
编号姓名性别年龄电话部门
${emp.id}${emp.name}${emp.sex}${emp.age}${emp.tel}${emp.deptName}
+ + +``` + diff --git "a/21 \346\264\252\347\220\246\347\232\223/0613.md" "b/21 \346\264\252\347\220\246\347\232\223/0613.md" new file mode 100644 index 0000000000000000000000000000000000000000..8b3c4c50ed5a59099a1282386466e831730d4d26 --- /dev/null +++ "b/21 \346\264\252\347\220\246\347\232\223/0613.md" @@ -0,0 +1,426 @@ +工具类 + +```java +package utils; + +import java.sql.*; + +public class DBUtil { + static String url="jdbc:mysql:///test?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 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 num=0; + try { + Connection conn = getConn(); + PreparedStatement pst = conn.prepareStatement(sql); + + num = pst.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return num; + } +} +``` + + + +servlet + +```java +package servlet; + +import bean.Info; +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 { + request.setCharacterEncoding("utf-8"); + String sql="select * from house_info i,house_type e where i.house_type_id=e.id"; + ResultSet rs = DBUtil.query(sql); + ArrayList list = new ArrayList<>(); + try { + while(rs.next()){ + int id = rs.getInt(1); + String lease_mode = rs.getString(2); + double rent = rs.getDouble(3); + String contacts = rs.getString(4); + String deposit_method = rs.getString(5); + int house_type_id = rs.getInt(6); + String address = rs.getString(7); + String type = rs.getString(9); + Info info = new Info(id, lease_mode, rent, contacts,deposit_method, house_type_id, address,type); + list.add(info); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/lib/add.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + String fanshi = request.getParameter("fanshi"); + String zujin = request.getParameter("zujin"); + String people = request.getParameter("people"); + String yajin = request.getParameter("yajin"); + String leixing = request.getParameter("leixing"); + String dizhi = request.getParameter("dizhi"); + String sql="insert into house_info values(?,?,?,?,?,?,?)"; + int i = DBUtil.update(sql,null, fanshi, zujin, people, yajin, leixing, dizhi); + if (i>0){ + response.sendRedirect("/All"); + }else{ + request.setAttribute("msg","添加失败"); + request.getRequestDispatcher("/WEB-INF/lib/hahaha.jsp"); + } + } +} +``` + +```java +package servlet; + +import bean.Info; +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("/All") +public class AllServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + String sql="select * from house_info i,house_type e where i.house_type_id=e.id"; + ResultSet rs = DBUtil.query(sql); + ArrayList list = new ArrayList<>(); + try { + while(rs.next()){ + int id = rs.getInt(1); + String lease_mode = rs.getString(2); + double rent = rs.getDouble(3); + String contacts = rs.getString(4); + String deposit_method = rs.getString(5); + int house_type_id = rs.getInt(6); + String address = rs.getString(7); + String type = rs.getString(9); + Info info = new Info(id, lease_mode, rent, contacts,deposit_method, house_type_id, address,type); + list.add(info); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + request.setAttribute("list",list); + request.getRequestDispatcher("WEB-INF/All.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} +``` + +```java +package bean; + +public class Info { + private int id; + private String lease_mode; + private Double rent; + private String contacts; + private String deposit_moethod; + private int house_tyoe_id; + private String address; + + private String type; + + @Override + public String toString() { + return "Info{" + + "id=" + id + + ", lease_mode='" + lease_mode + '\'' + + ", rent=" + rent + + ", contacts='" + contacts + '\'' + + ", deposit_moethod='" + deposit_moethod + '\'' + + ", house_tyoe_id=" + house_tyoe_id + + ", address='" + address + '\'' + + ", type='" + type + '\'' + + '}'; + } + + 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_moethod() { + return deposit_moethod; + } + + public void setDeposit_moethod(String deposit_moethod) { + this.deposit_moethod = deposit_moethod; + } + + public int getHouse_tyoe_id() { + return house_tyoe_id; + } + + public void setHouse_tyoe_id(int house_tyoe_id) { + this.house_tyoe_id = house_tyoe_id; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public Info() { + } + + public Info(int id, String lease_mode, Double rent, String contacts, String deposit_moethod, int house_tyoe_id, String address, String type) { + this.id = id; + this.lease_mode = lease_mode; + this.rent = rent; + this.contacts = contacts; + this.deposit_moethod = deposit_moethod; + this.house_tyoe_id = house_tyoe_id; + this.address = address; + this.type = type; + } +} +``` + +```java +package bean; + +public class Type { + private int id; + private String type; + + @Override + public String toString() { + return "type{" + + "id=" + id + + ", 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; + } + + public Type() { + } + + public Type(int id, String type) { + this.id = id; + this.type = type; + } +} +``` + +jsp + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 添加房源 + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
租赁方式
租金
联系人
押金方式
房屋类型 + +
详细地址
+
+ + +``` + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 房源信息 + + + + + + + + + + + + + + + + + + + + + + + + +
编号租赁方式租金联系人押金方式房屋类型详细地址
${l.id}${l.lease_mode}${l.rent}${l.contacts}${l.deposit_moethod}${l.type}${l.address}
+ + + + + +``` + +```jsp +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-10 + Time: 11:28 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + + + + ${msg} + 返回列表 + + +``` +