diff --git "a/28 \351\273\204\346\242\223\345\242\203/20230606/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" "b/28 \351\273\204\346\242\223\345\242\203/20230606/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" new file mode 100644 index 0000000000000000000000000000000000000000..08c81e73b82b09c14dbff166dae6de985c38b352 --- /dev/null +++ "b/28 \351\273\204\346\242\223\345\242\203/20230606/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" @@ -0,0 +1,399 @@ +### 作业: + +~~~ java +package bean; + +public class attence { + private int id ; + private String time; + private int type; + private int sid; + private String sname; + + public attence() { + } + + public attence(int id, String time, int type, int sid, String sname) { + this.id = id; + this.time = time; + this.type = type; + this.sid = sid; + this.sname = sname; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getTime() { + return time; + } + + public void setTime(String time) { + this.time = time; + } + + public int getType() { + return type; + } + + public void setType(int type) { + this.type = type; + } + + public int getSid() { + return sid; + } + + public void setSid(int sid) { + this.sid = sid; + } + + public String getSname() { + return sname; + } + + public void setSname(String sname) { + this.sname = sname; + } +} +package bean; + +public class student { + private int sid; + private String sname; + + public student() { + } + + public student(int sid, String sname) { + this.sid = sid; + this.sname = sname; + } + + public int getSid() { + return sid; + } + + public void setSid(int sid) { + this.sid = sid; + } + + public String getSname() { + return sname; + } + + public void setSname(String sname) { + this.sname = sname; + } +} +~~~ + +~~~ java +package servlet; + +import bean.attence; +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 list 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 attence a ,student s where s.sid=a.sid"; + Connection conn = null; + PreparedStatement prs = null; + ResultSet rs = null; + try { + conn = DBUtil.getConn(); + prs = conn.prepareStatement(sql); + rs = prs.executeQuery(); + while (rs.next()) { + int aid = rs.getInt(1); + String time = rs.getString(2); + int type = rs.getInt(3); + int sid = rs.getInt(5); + String sname = rs.getString(6); + list.add(new attence(aid, time, type, sid, sname)); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + DBUtil.close(conn, prs, rs); + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +package servlet; + +import bean.student; +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(name = "add", value = "/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 student"; + Connection conn = null; + PreparedStatement prs = null; + ResultSet rs = null; + try { + conn = DBUtil.getConn(); + prs = conn.prepareStatement(sql); + rs = prs.executeQuery(); + while (rs.next()) { + int sid = rs.getInt(1); + String name = rs.getString(2); + list.add(new student(sid,name)); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + DBUtil.close(conn, prs, rs); + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/add.jsp").forward(request,response); + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +package servlet; + +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.SQLException; + +@WebServlet(name = "save", value = "/save") +public class save 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"); + int name = Integer.parseInt(request.getParameter("name")); + String time = request.getParameter("time"); + String type = request.getParameter("type"); + int i=0; + System.out.println(type); + if ("yd".equals(type)) { + i = 1; + } else if ("cd".equals(type)) { + i = 2; + } else if ("kk".equals(type)) { + i=3; + } + System.out.println(i); + int a; + String sql = "insert into attence values (?,?,?,?)"; + Connection conn = null; + PreparedStatement prs = null; + try { + conn = DBUtil.getConn(); + prs = conn.prepareStatement(sql); + prs.setInt(1, 0); + prs.setString(2, time); + prs.setInt(3,i); + prs.setInt(4, name); + a = prs.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + DBUtil.close(conn, prs, null); + } + if (a > 0) { + request.setAttribute("smg", "成功添加"); + request.getRequestDispatcher("/WEB-INF/smg.jsp").forward(request, response); + } else { + request.setAttribute("smg", "添加失败"); + request.getRequestDispatcher("/WEB-INF/smg.jsp").forward(request, response); + } + + } +} + +~~~ + +~~~ java +package util; + +import java.sql.*; + +public class DBUtil { + private static final String url="jdbc:mysql:///attdb?characterEncoding=utf8"; + private static final String user ="root"; + private static final String pwb ="root"; + + 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(url, user, pwb); + return conn; + } + public static void close(Connection conn, PreparedStatement prs, ResultSet rs){ + try { + if (rs!=null){ + rs.close(); + } + if (prs!=null){ + prs.close(); + } + if (conn!=null){ + conn.close(); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +~~~ + +~~~ java +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-05 + Time: 09:01 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +添加 + + + + + + + + + + + +
考勤编号学生编号学生姓名出勤时间出勤状态
${bar.id}${bar.sid}${bar.sname}${bar.time}${bar.type==1 ? "已到" :bar.type==2?"迟到": bar.type==3 ?"旷课":"没来"}
+ + + +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-05 + Time: 09:21 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +

学生考勤系统

+
+ + + + + + + + + + + + + + + + + +
学生姓名 + +
考勤时间
考勤状况已到: + 迟到: + 旷课:
+
+ + + +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-05 + Time: 10:48 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +${smg} +
+返回 + + +~~~ + diff --git "a/28 \351\273\204\346\242\223\345\242\203/20230609/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" "b/28 \351\273\204\346\242\223\345\242\203/20230609/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" new file mode 100644 index 0000000000000000000000000000000000000000..4db8347d92740751d959c065ce3aa1e57896fada --- /dev/null +++ "b/28 \351\273\204\346\242\223\345\242\203/20230609/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" @@ -0,0 +1,594 @@ +# 房屋管理 + +## mysql + +```mysql +create database test charset utf8; +use test; +create table house_type +( + id int primary key auto_increment, # 编号 主键,自动增长列 + type varchar(50) not null # 房屋类型 不允许为空 +); +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_type values +(null,'两室一厅一卫'), +(null,'三室一厅一卫'), +(null,'三室两厅一卫'); +insert into house_info values +(null,'整租',2300.0,null,'押一付三',1,'地球53区'), +(null,'整租',2200.0,'李四','押一付三',1,'地球53区'), +(null,'整租',3400.0,null,'押一付三',2,'地球51区'), +(null,'合租',800.0,'张三','押一付三',2,'地球52区'), +(null,'合租',690.0,null,'押一付三',3,'地球51区'); +``` + +## bean包 + +### HouseType类 + +```java +package bean; + +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 + '\'' + + '}'; + } +} +``` + +### HouseInfo类 + +```java +package bean; + +public class HouseInfo { + + private int id; // 编号 + private String leaseMode; // 租赁方式 + private double rent; // 租金 + private String contacts; // 联系人 + private String depositMethod; // 押金方式 + private int houseTypeId; // 房屋类型编号 + private String address; // 详细地址 + private String type; // 房屋类型 + + @Override + public String toString() { + return "HouseInfo{" + "id=" + id + ", leaseMode='" + leaseMode + '\'' + ", rent=" + rent + ", contacts='" + contacts + '\'' + ", depositMethod='" + depositMethod + '\'' + ", houseTypeId=" + houseTypeId + ", address='" + address + '\'' + ", type='" + type + '\'' + '}'; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getLeaseMode() { + return leaseMode; + } + + public void setLeaseMode(String leaseMode) { + this.leaseMode = leaseMode; + } + + 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 getDepositMethod() { + return depositMethod; + } + + public void setDepositMethod(String depositMethod) { + this.depositMethod = depositMethod; + } + + public int getHouseTypeId() { + return houseTypeId; + } + + public void setHouseTypeId(int houseTypeId) { + this.houseTypeId = houseTypeId; + } + + 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 HouseInfo() { + } + + public HouseInfo(int id, String leaseMode, double rent, String contacts, String depositMethod, int houseTypeId, String address, String type) { + this.id = id; + this.leaseMode = leaseMode; + this.rent = rent; + this.contacts = contacts; + this.depositMethod = depositMethod; + this.houseTypeId = houseTypeId; + this.address = address; + this.type = type; + } +} +``` + +## utils包 + +### DBUtil类 + +```java +package utils; + +import java.sql.*; + +public class DBUtil { + static String url="jdbc:mysql:///test?characterEncoding=utf8";// 主机,端口,数据库,编码 + static String user = "root"; + static String pwd = "122319"; + // 注册驱动 + 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; + } + // 通用的update方法 + 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; + } +} +``` + +## servlet包 + +### ListServlet类 + +```java +package servlet; + +import bean.HouseInfo; +import utils.DBUtil; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +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 { + // * 想办法从数据库查询所有房源信息,将用jsp显示 + // 1 编写SQL + String sql = "select * from house_info i,house_type t where i.house_type_id=t.id"; + // 2 将SQL传给工具类的查询方法,得到结果集 + ResultSet rs = DBUtil.query(sql); + // 3 设置一个房源的集合,遍历结果集,将结果封装成房源对象,再将对象添加到集合 + //设置一个房源的集合 + ArrayList list = new ArrayList<>(); + //遍历结果集 + try { + while (rs.next()){ + // 将结果封装成房源对象 + int id = rs.getInt(1); + String mode = rs.getString(2); + double rent = rs.getDouble(3); + String contacts = rs.getString(4); + String method = rs.getString(5); + int typeId = rs.getInt(6); + String address = rs.getString(7); + String typeName = rs.getString(9); + HouseInfo house = new HouseInfo(id, mode, rent, contacts, method, typeId, address, typeName); + //再将对象添加到集合 + list.add(house); + } + } catch (SQLException e) { + e.printStackTrace(); + } + // 4 把集合添加到request域中 + request.setAttribute("list",list); + // 5 将请求转发给jsp + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response); + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // 根据姓名搜索对应的房源 + // 1 处理乱码 + request.setCharacterEncoding("utf-8"); + // 1获取姓名的值 + String user = request.getParameter("user"); + // 1 编写SQL + String sql = "select * from house_info i,house_type t where i.house_type_id=t.id and contacts like ? "; + // 2 将SQL传给工具类的查询方法,得到结果集 + ResultSet rs = DBUtil.query(sql,"%"+user+"%"); + // 3 设置一个房源的集合,遍历结果集,将结果封装成房源对象,再将对象添加到集合 + //设置一个房源的集合 + ArrayList list = new ArrayList<>(); + //遍历结果集 + try { + while (rs.next()){ + // 将结果封装成房源对象 + int id = rs.getInt(1); + String mode = rs.getString(2); + double rent = rs.getDouble(3); + String contacts = rs.getString(4); + String method = rs.getString(5); + int typeId = rs.getInt(6); + String address = rs.getString(7); + String typeName = rs.getString(9); + HouseInfo house = new HouseInfo(id, mode, rent, contacts, method, typeId, address, typeName); + //再将对象添加到集合 + list.add(house); + } + } catch (SQLException e) { + e.printStackTrace(); + } + // 4 把集合添加到request域中 + request.setAttribute("list",list); + // 5 将请求转发给jsp + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response); + } +} +``` + +### AddServlet类 + +```java +package servlet; + +import bean.HouseType; +import utils.DBUtil; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +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 { +// * 想办法从数据库查询所有房源信息,将用jsp显示 + // 1 编写SQL + String sql = "select * from house_type"; + // 2 将SQL传给工具类的查询方法,得到结果集 + ResultSet rs = DBUtil.query(sql); + // 3 设置一个房源的集合,遍历结果集,将结果封装成房源对象,再将对象添加到集合 + //设置一个房源的集合 + ArrayList list = new ArrayList<>(); + //遍历结果集 + try { + while (rs.next()){ + // 将结果封装成房源对象 + int id = rs.getInt(1); + String type = rs.getString(2); + HouseType houseType = new HouseType(id, type); + //再将对象添加到集合 + list.add(houseType); + } + } catch (SQLException e) { + e.printStackTrace(); + } + // 4 把集合添加到request域中 + request.setAttribute("list",list); + // 5 将请求转发给jsp + request.getRequestDispatcher("/WEB-INF/add.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // 1 处理乱码 + request.setCharacterEncoding("utf-8"); + // 2 接收表单数据 + String leaseMode = request.getParameter("leaseMode"); + String rent = request.getParameter("rent"); + String contacts = request.getParameter("contacts"); + String depositMethod = request.getParameter("depositMethod"); + String type = request.getParameter("type"); + String address = request.getParameter("address"); + // 3 编写sql + String sql = "insert into house_info values (?,?,?,?,?,?,?)"; + // 4 调用工具类执行SQL,得到影响的行数 + int i = DBUtil.update(sql, null, leaseMode, rent, contacts, depositMethod, type, address); + // 5 根据影响的行数,做判断提示 + if (i>0){ + // 使用响应的重定向可以直接刷新列表 + response.sendRedirect("/list"); + }else{ + // 在request域中添加失败的提示信息 + request.setAttribute("msg","添加失败"); + // 5 将请求转发给jsp + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + } + } +} +``` + +DeleteServlet + +```java +package servlet; + +import utils.DBUtil; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +@WebServlet("/delete") +public class DeleteServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // 根据id删除对应的房源 + // 1获取ID的值 + String id = request.getParameter("id"); + // 3 编写sql + String sql = "delete from house_info where id=?"; + // 4 调用工具类执行SQL,得到影响的行数 + int i = DBUtil.update(sql, id); + // 5 根据影响的行数,做判断提示 + if (i>0){ + // 使用响应的重定向可以直接刷新列表 + response.sendRedirect("/list"); + }else{ + // 在request域中添加失败的提示信息 + request.setAttribute("msg","删除失败"); + // 5 将请求转发给jsp + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + } + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} +``` + +## JSP网页 + +### list.jsp + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-09 + Time: 14:43 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + + + + + + + + + + + + + + + + + + + + + + + + + +
编号租赁方式租金(元)联系人押金方式房屋类型详细地址操作
${house.id}${house.leaseMode}${house.rent}${house.contacts}${house.depositMethod}${house.type}${house.address} + 删除 +
+
+ 姓名: +
+ + +``` + +add.jsp + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-09 + Time: 15:09 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
租赁方式
租金(元)
联系人
押金方式
房屋类型 + +
详细地址
+
+ + +``` + +### msg.list + +```jsp +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-09 + Time: 15:30 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +${msg} +返回列表 + + \ No newline at end of file diff --git "a/28 \351\273\204\346\242\223\345\242\203/29239528/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" "b/28 \351\273\204\346\242\223\345\242\203/20239528/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" similarity index 100% rename from "28 \351\273\204\346\242\223\345\242\203/29239528/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" rename to "28 \351\273\204\346\242\223\345\242\203/20239528/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt"