diff --git "a/47 \346\235\216\345\277\265\345\277\265/20230606 \344\275\234\344\270\232.md" "b/47 \346\235\216\345\277\265\345\277\265/20230606 \344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..66dcc2e0a52e6ae181fcb2678afd09662aee9457 --- /dev/null +++ "b/47 \346\235\216\345\277\265\345\277\265/20230606 \344\275\234\344\270\232.md" @@ -0,0 +1,401 @@ +### 作业: + +~~~ 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/47 \346\235\216\345\277\265\345\277\265/20230609 \344\275\234\344\270\232.md" "b/47 \346\235\216\345\277\265\345\277\265/20230609 \344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..966d8bf5172282628d937b4bba56b6fd4b735a2b --- /dev/null +++ "b/47 \346\235\216\345\277\265\345\277\265/20230609 \344\275\234\344\270\232.md" @@ -0,0 +1,498 @@ +### 作业: + +###### 工具包 + +~~~ java +package Util; + +import java.sql.*; + +public class DBUtil { + static String url = "jdbc:mysql:///text?characterEncoding=utf8"; + static String user = "root"; + static String pwd = "1234"; + + 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 i1 = 0; + try { + Connection conn = getConn(); + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + } + i1 = pst.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + return i1; + } + + public static void close(Connection conn,PreparedStatement pst,ResultSet rs){ + try { + if (conn!=null){ + conn.close(); + } + if (pst!=null){ + pst.close(); + } + if (rs!=null){ + rs.close(); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } + +} +~~~ + +###### bean类 + +~~~ java +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 +public class HouseInfo { + private int id; + private String mode; + private double rent; + private String contacts; + private String method; + private int typeId; + private String address; + private String type; + + public HouseInfo() { + } + + public HouseInfo(int id, String mode, double rent, String contacts, String method, int typeId, String address, String type) { + this.id = id; + this.mode = mode; + this.rent = rent; + this.contacts = contacts; + this.method = method; + this.typeId = typeId; + this.address = address; + this.type = type; + } + + 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 getContacts() { + return contacts; + } + + public void setContacts(String contacts) { + this.contacts = contacts; + } + + public String getMethod() { + return method; + } + + public void setMethod(String method) { + this.method = method; + } + + public int getTypeId() { + return typeId; + } + + public void setTypeId(int typeId) { + this.typeId = typeId; + } + + 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; + } + + @Override + public String toString() { + return "HouseInfo{" + + "id=" + id + + ", mode='" + mode + '\'' + + ", rent=" + rent + + ", contacts='" + contacts + '\'' + + ", method='" + method + '\'' + + ", typeId=" + typeId + + ", address='" + address + '\'' + + ", type='" + type + '\'' + + '}'; + } +} +~~~ + +###### list + +~~~ java +package servlet; + +import Util.DBUtil; +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("/list") +public class List extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String sql = "select * from house_info i,house_type t where t.id=i.house_type_id"; + ArrayList list = new ArrayList<>(); + ResultSet rs = null; + try { + rs = DBUtil.query(sql); + while (rs.next()){ + int id = rs.getInt(1); + String mode = rs.getString(2); + int rent = rs.getInt(3); + String contacts = rs.getString(4); + String method = rs.getString(5); + int typeId = rs.getInt(6); + String address = rs.getString(7); + String type = rs.getString(9); + HouseInfo houseInfo = new HouseInfo(id,mode,rent,contacts,method,typeId,address,type); + list.add(houseInfo); + } + } catch (SQLException e) { + e.printStackTrace(); + } + DBUtil.close(null,null,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 { + + } +} +~~~ + +###### add + +~~~ java +package servlet; + +import bean.HouseType; +import dbutil.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("/AddServlet") +public class AddServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + //language=MySQL + String sql = "select * from house_type"; + ArrayList list = new ArrayList<>(); + try { + ResultSet rs = DBUtil.query(sql); + 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(); + } + DBUtil.close(null,null,null); + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/add.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +~~~ + +###### save + +~~~ java +package servlet; + +import dbutil.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.SQLException; + +@WebServlet("/SaveServlet") +public class SaveServlet 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"); + String mode = request.getParameter("mode"); + String rent = request.getParameter("rent"); + String contacts = request.getParameter("contacts"); + String method = request.getParameter("method"); + String id = request.getParameter("id"); + String address = request.getParameter("address"); + System.out.println(mode); + System.out.println(rent); + + String sql ="insert into house_info values (?,?,?,?,?,?,?)"; + + int i = 0; + i = DBUtil.update(sql, "0",mode, rent, contacts, method, id, address); + DBUtil.close(null,null,null); + if (i > 0) { + // response.sendRedirect("/list"); + request.setAttribute("SaveServlet","添加成功"); + request.getRequestDispatcher("/WEB-INF/save.jsp").forward(request,response); + } else { + request.setAttribute("SaveServlet","添加失败"); + request.getRequestDispatcher("/WEB-INF/save.jsp").forward(request,response); + } + } +} + +~~~ + +###### html + +~~~ html +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Lenovo + Date: 2023/6/8 + Time: 15:36 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 房源列表 + + + + + + + + + + + + + + + + + + + + + + + + +
编号租赁方式租金(元)联系人押金方式房屋类型详细地址
${h.id}${h.mode}${h.rent}${h.contacts}${h.method}${h.typeId}${h.type}
+ + + + +~~~ + +~~~ html +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-07 + Time: 11:35 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
租赁方式
租金
联系人
押金方式
房屋类型
详细地址
+
+ + + +~~~ + +~~~ html +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-07 + Time: 12:36 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +${SaveServlet} +
+返回 + + + +~~~ +