diff --git "a/14 \345\256\213\345\256\217\346\211\254/20230527\345\205\263\344\272\216jsp\347\275\221\351\241\265\345\210\240\351\231\244\344\277\256\346\224\271\346\225\260\346\215\256\347\232\204\344\275\234\344\270\232.md" "b/14 \345\256\213\345\256\217\346\211\254/20230527\345\205\263\344\272\216jsp\347\275\221\351\241\265\345\210\240\351\231\244\344\277\256\346\224\271\346\225\260\346\215\256\347\232\204\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..c98adeea30ab9e7054c686a850b94115a6e641a1 --- /dev/null +++ "b/14 \345\256\213\345\256\217\346\211\254/20230527\345\205\263\344\272\216jsp\347\275\221\351\241\265\345\210\240\351\231\244\344\277\256\346\224\271\346\225\260\346\215\256\347\232\204\344\275\234\344\270\232.md" @@ -0,0 +1,334 @@ +```java +<%@ page import="java.sql.ResultSet" %> +<%@ page import="Utils.JDBC" %><%-- + //index.jsp + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-05-27 + Time: 15:27 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 学生列表 + + + + + + + + + + + <% + String sql = "select *from student"; + ResultSet select = JDBC.select(sql); + while (select.next()) { + int id = select.getInt("id"); + String name = select.getString("name"); + String sex = select.getString("sex"); + %> + + + + + + + + <% + } + %> +
编号姓名年龄操作
<%=id%> + <%=name%> + <%=sex%> + 修改 删除
+ + + +<%@ page import="use.Student" %><%-- + Created by IntelliJ IDEA. + //Update.jsp + User: 阳 + Date: 2023/5/28 + Time: 20:38 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 修改学生信息 + + +<% + // 从request取出传过来的学生对象 +// 强转,因为默认是Object +%> + +<% + Student student = (Student) request.getAttribute("student"); +%> + +
+ 编号:
+ 姓名:
+ 性别:
+ +
+ + + +//工具类 +package Utils; +import java.sql.*; + +public class JDBC { + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + //创建连接 + private static Connection conn() { + String url = "jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf8"; + Connection conn = null; + try { + conn = DriverManager.getConnection(url, "root", "36932211"); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return conn; + } + + //查询 + public static ResultSet select(String sql, String... arr) { + ResultSet re = null; + try { + PreparedStatement pst = conn().prepareStatement(sql); + for (int i = 0; i < arr.length; i++) { + pst.setString((i + 1), arr[i]); + } + re = pst.executeQuery(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return re; + } + + //增删改 + public static int update(String sql, String... arr) { + try { + PreparedStatement pst = conn().prepareStatement(sql); + for (int i = 0; i < arr.length; i++) { + pst.setString((i + 1), arr[i]); + } + int i = pst.executeUpdate(); + return i; + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +//封装的学生对象 +package use; + +public class Student { + private int id; + private String name; + private String sex; + + @Override + public String toString() { + return "Student{" + + "id=" + id + + ", name='" + name + '\'' + + ", sex='" + sex + '\'' + + '}'; + } + + 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 Student() { + } + + public Student(int id, String name, String sex) { + this.id = id; + this.name = name; + this.sex = sex; + } +} + +package Servlet; +//delete Servlet +import Utils.JDBC; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; + +@WebServlet(name = "deletebyid", value = "/deletebyid") +public class deletebyid extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { +// 设置处理乱码 + request.setCharacterEncoding("utf-8"); + response.setContentType("text/html;character=utf-8"); +// 接受网页参数 + String id=request.getParameter("id"); +// 设置sql语句 + String sql="delete from student where id=?"; + response.setCharacterEncoding("utf-8"); +// 处理结果 + try { + int i = JDBC.update(sql,id); + if (i>0){ + response.getWriter().write(""); + + }else { + response.getWriter().write("删除失败"); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +package Servlet; +//update Servlet + +import Utils.JDBC; +import use.Student; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.ResultSet; +import java.sql.SQLException; + +@WebServlet(name = "updatebyid", value = "/updatebyid") +public class updatebyid extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // 设置处理乱码 + request.setCharacterEncoding("utf-8"); + response.setContentType("text/html;character=utf-8"); +// 根据id去更改信息 + String id=request.getParameter("id"); + + String sql="select *from student where id=?"; + try { + ResultSet rs = JDBC.select(sql, id); +// 因为查出来的id只有一个,所以不用while循环了 +// 这里得到学生信息,不直接响应,可以封装成一个学生对象。 +// 然后再将它转发给一个jsp页面,让他去处理显示 + rs.next(); + int stuid = rs.getInt("id"); + String name = rs.getString("name"); + String sex = rs.getString("sex"); + Student stu = new Student(stuid,name,sex); + +// 将Student对象,添加到request。并转发给负责显示表单的jsp + request.setAttribute("student",stu); +// + request.getRequestDispatcher("/update.jsp").forward(request,response); + + } catch (SQLException e) { + throw new RuntimeException(e); + } + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +package Servlet; +//saveUpdate Servlet + +import Utils.JDBC; + + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; + + +@WebServlet(name = "saveUpdate", value = "/saveUpdate") +public class savaUpdate extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { +// 将doGet转发给doPost,这样两个都可以用 + doPost(request, response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { +// 接受表单信息,将数据保存到数据库 + // 设置处理乱码 + request.setCharacterEncoding("utf-8"); + response.setContentType("text/html;character=utf-8"); + + String id = request.getParameter("id"); + String name = request.getParameter("name"); + String sex = request.getParameter("sex"); + + String sql = "update student set name=?,sex=? where id=?"; + response.setCharacterEncoding("utf-8"); +// 处理结果 + try { + int i = JDBC.update(sql,name,sex,id); + if (i>0){ + response.getWriter().write(""); + + }else { + response.getWriter().write("修改失败"); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + + } +} +``` + diff --git "a/14 \345\256\213\345\256\217\346\211\254/20230605\345\205\263\344\272\216\350\200\203\345\213\244\347\256\241\347\220\206\347\232\204\344\275\234\344\270\232.md" "b/14 \345\256\213\345\256\217\346\211\254/20230605\345\205\263\344\272\216\350\200\203\345\213\244\347\256\241\347\220\206\347\232\204\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..44c048370a8e0c0eadf56e65fa0ed028b8d967f7 --- /dev/null +++ "b/14 \345\256\213\345\256\217\346\211\254/20230605\345\205\263\344\272\216\350\200\203\345\213\244\347\256\241\347\220\206\347\232\204\344\275\234\344\270\232.md" @@ -0,0 +1,461 @@ +```mysql +CREATE DATABASE AttDB charset utf8; +USE AttDB; +CREATE TABLE Student ( +sid int PRIMARY KEY auto_increment COMMENT "学号", +sname varchar(20) UNIQUE NOT NULL COMMENT "学生姓名" +); + +INSERT INTO Student VALUES +(0,"张三"), +(0,"李四"), +(0,"王五"); + +CREATE TABLE Attence ( +aid int PRIMARY KEY auto_increment COMMENT "考勤编号", +time varchar(20) NOT NULL COMMENT "出勤时间", +type INT COMMENT "出勤状况", +sid int, +FOREIGN KEY (sid) REFERENCES Student (sid) +); + +INSERT INTO Attence VALUES +(0,"2022-05-20 08:20:00",1,1), +(0,"2022-05-21 08:20:00",2,2), +(0,"2022-05-22 08:20:00",3,3), +(0,"2022-05-23 08:20:00",2,3), +(0,"2022-05-24 08:20:00",1,2); +``` + +```java +//bean_Attence +package bean; + +public class Attence { + private int aid; + private int sid; + private int type; + private String name; + private String time; + + public Attence() { + } + + public Attence(int aid, int sid, int type, String name, String time) { + this.aid = aid; + this.sid = sid; + this.type = type; + this.name = name; + this.time = time; + } + + public int getAid() { + return aid; + } + + public void setAid(int aid) { + this.aid = aid; + } + + public int getSid() { + return sid; + } + + public void setSid(int sid) { + this.sid = sid; + } + + public int getType() { + return type; + } + + public void setType(int type) { + this.type = type; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getTime() { + return time; + } + + public void setTime(String time) { + this.time = time; + } + + @Override + public String toString() { + return "Attence{" + + "aid=" + aid + + ", sid=" + sid + + ", type=" + type + + ", name='" + name + '\'' + + ", time='" + time + '\'' + + '}'; + } +} +//bean_Student +package bean; + +public class Student { + private int id; + private String name; + + public Student() { + } + + public Student(int id, String name) { + this.id = id; + this.name = name; + } + + public int getId() { + return id; + } + + @Override + public String toString() { + return "Student{" + + "id=" + id + + ", name='" + name + '\'' + + '}'; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} +//servlet_add +package servlet; + +import bean.Attence; +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("/add") +public class Add extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String sql = "SELECT * from Student"; + ArrayList list = new ArrayList<>(); + Connection conn = null; + PreparedStatement pst = null; + ResultSet rs = null; + try { + conn = DBUtil.getConn(); + pst = conn.prepareStatement(sql); + rs = pst.executeQuery(); + while (rs.next()) { + int sid = rs.getInt(1); + String name = rs.getString(2); + Student student = new Student(sid, name); + list.add(student); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + try { + DBUtil.close(conn, pst, rs); + } 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 { + + } +} +//servlet_list +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 { + String sql = "SELECT * from Attence A,Student stu where A.sid = stu.sid"; + ArrayList list = new ArrayList<>(); + Connection conn = null; + PreparedStatement pst = null; + ResultSet rs = null; + try { + conn = DBUtil.getConn(); + pst = conn.prepareStatement(sql); + rs = pst.executeQuery(); + while (rs.next()) { + int aid = rs.getInt(1); + int sid = rs.getInt(4); + int type = rs.getInt(3); + String name = rs.getString(6); + String time = rs.getString(2); + Attence attence = new Attence(aid, sid, type, name, time); + list.add(attence); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + try { + DBUtil.close(conn, pst, rs); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + request.setAttribute("list", list); + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request, response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} +//servlet_save +package servlet; + +import com.sun.org.apache.xerces.internal.xs.XSIDCDefinition; +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.Date; +import java.sql.PreparedStatement; +import java.sql.SQLException; + +@WebServlet("/sava") +public class Sava 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 name = request.getParameter("name"); + String date = request.getParameter("date"); + int type = request.getIntHeader("type"); + String sql = "insert into Attence values(?,?,?,?)"; + int i = 0; + Connection conn = null; + PreparedStatement pst = null; + try { + conn = DBUtil.getConn(); + pst = conn.prepareStatement(sql); + pst.setInt(1, 0); + pst.setDate(2, Date.valueOf(date)); + pst.setInt(3, type); + pst.setInt(4, Integer.parseInt(name)); + i = pst.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + try { + DBUtil.close(conn, pst, null); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + 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); + } + } +} +//util_DBUtil +package util; + +import java.sql.*; + +public class DBUtil { + static String url = "jdbc:mysql:///AttDB?characterEncoding=utf8"; + static String username = "root"; + static String pwd = "123456"; + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + System.out.println("注册驱动异常"); + throw new RuntimeException(e); + } + } + public static Connection getConn() throws SQLException { + Connection conn = DriverManager.getConnection(url, username, pwd); + return conn; + } + 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(); + } + } +} +``` + +```jsp +//add,jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-05 + Time: 11:12 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + +
+ +

学生考勤系统

+ + + + + + + + + + + + + + + + +
学生姓名 + +
考勤时间
考勤状况 + 已到 + 迟到 + 旷课 +
+
+ + +//list.jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-05 + Time: 10:28 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + + + + + + + + + + + + + + + + + + +
考勤编号学生编号学生姓名出勤时间出勤状况
${att.aid}${att.sid}${att.name}${att.time} + + 已到 + + + 迟到 + + + 旷课 + +
+ + +//msg.jsp +<%-- + Created by IntelliJ IDEA. + User: y + Date: 2023/6/6 + Time: 21:58 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 友情提醒 + + +${msg} +
+返回列表 + + +``` +