From 2738987ce9e7d7f21adeae1a92b119f6d5a58057 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=AD=A3=E7=84=95?= <1953038944@qq.com> Date: Tue, 6 Jun 2023 22:45:22 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E5=85=AD=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...03\345\213\244\347\263\273\347\273\237.md" | 470 ++++++++++++++++++ 1 file changed, 470 insertions(+) create mode 100644 "38 \351\273\204\346\255\243\347\204\225/20230605 \345\255\246\347\224\237\350\200\203\345\213\244\347\263\273\347\273\237.md" diff --git "a/38 \351\273\204\346\255\243\347\204\225/20230605 \345\255\246\347\224\237\350\200\203\345\213\244\347\263\273\347\273\237.md" "b/38 \351\273\204\346\255\243\347\204\225/20230605 \345\255\246\347\224\237\350\200\203\345\213\244\347\263\273\347\273\237.md" new file mode 100644 index 0000000..285cf6d --- /dev/null +++ "b/38 \351\273\204\346\255\243\347\204\225/20230605 \345\255\246\347\224\237\350\200\203\345\213\244\347\263\273\347\273\237.md" @@ -0,0 +1,470 @@ +```java +//bean包 +//student +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; + } + + @Override + public String toString() { + return "Student{" + + "sid=" + sid + + ", sname='" + sname + '\'' + + '}'; + } +} + +//attence +package bean; + +public class Attence { + private int aid; + private String time; + private int type; + private int sid; + private String sname; + + public Attence() { + } + + public Attence(int aid, String time, int type, int sid, String sname) { + this.aid = aid; + this.time = time; + this.type = type; + this.sid = sid; + this.sname = sname; + } + + public int getAid() { + return aid; + } + + public void setAid(int aid) { + this.aid = aid; + } + + 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; + } + + @Override + public String toString() { + return "Attence{" + + "aid=" + aid + + ", time='" + time + '\'' + + ", type=" + type + + ", sid=" + sid + + ", sname='" + sname + '\'' + + '}'; + } +} + +``` + +```java +//servlet包 +//list +package servlet; + +import Util.DBUtil; +import bean.Attence; + +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"); + response.setContentType("text/html;charset=utf-8"); + String sql = "select * from Student a,Attence b where a.sid=b.sid"; + ArrayList list = new ArrayList<>(); + Connection coon = null; + PreparedStatement pst = null; + ResultSet rs = null; + try { + coon = DBUtil.getCoon(); + pst = coon.prepareStatement(sql); + rs = pst.executeQuery(); + while (rs.next()){ + int aid = rs.getInt("aid"); + int sid = rs.getInt("sid"); + String sname = rs.getString("sname"); + String time = rs.getString("time"); + int type = rs.getInt("type"); + Attence attence = new Attence(aid, time, type, sid, sname); + list.add(attence); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + try { + DBUtil.close(coon,pst,rs); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + + //将集合,往request里放 + 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 +package servlet; + +import Util.DBUtil; +import bean.Student; + +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 { + //处理乱码 + request.setCharacterEncoding("utf-8"); + response.setContentType("text/html;charset=utf-8"); + String sql = "select * from Student"; + ArrayList list = new ArrayList<>(); + Connection coon = null; + PreparedStatement pst = null; + ResultSet rs = null; + try { + coon = DBUtil.getCoon(); + pst = coon.prepareStatement(sql); + rs = pst.executeQuery(); + while (rs.next()){ + int sid = rs.getInt("sid"); + String sname = rs.getString("sname"); + Student student = new Student(sid,sname); + list.add(student); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + try { + DBUtil.close(coon,pst,rs); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + + //将集合,往request里放 + 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 +@WebServlet("/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 +//util包 +package Util; + +import java.sql.*; + +public class DBUtil { + static final String url="jdbc:mysql://localhost:3306/AttDB?useSSL=false&useUnicode=true&characterEncoding=utf8"; + static final String user="root"; + static final String pwd="root"; + + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + public static Connection getCoon() throws SQLException { + Connection conn = DriverManager.getConnection(url, user, 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 +//list +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-05 + Time: 09:53 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 全部 + + +添加 + + + + + + + + + + + + + + + + + + + + + +
考勤编号学生编号学生姓名出勤时间出勤状况
${Attence.aid}${Attence.sid}${Attence.sname}${Attence.time} + + 已到 + + + 迟到 + + + 旷课 + + +
+ + + + +//add +<%@ 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 + + + +
+ + + + + + + + + + + + + + + + + + + + + + +
学生考勤系统
学生姓名 + +
出勤时间
出勤状况 + 已到 + 迟到 + 旷课 +
+ +
+ + + +//smg +<%-- + 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} +
+返回 + + + +``` + -- Gitee