From 91f89d3ff6e0465d8c7979a1042bcdeef87feb68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E9=BE=99=E8=85=BE?= <2235304668@qq.com> Date: Tue, 6 Jun 2023 22:55:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...1\347\220\206\347\263\273\347\273\237+.md" | 322 ++++++++++++++++++ 1 file changed, 322 insertions(+) create mode 100644 "22 \345\274\240\351\276\231\350\205\276/20230606. \350\275\246\350\276\206\347\256\241\347\220\206\347\263\273\347\273\237+.md" diff --git "a/22 \345\274\240\351\276\231\350\205\276/20230606. \350\275\246\350\276\206\347\256\241\347\220\206\347\263\273\347\273\237+.md" "b/22 \345\274\240\351\276\231\350\205\276/20230606. \350\275\246\350\276\206\347\256\241\347\220\206\347\263\273\347\273\237+.md" new file mode 100644 index 0000000..007fb4b --- /dev/null +++ "b/22 \345\274\240\351\276\231\350\205\276/20230606. \350\275\246\350\276\206\347\256\241\347\220\206\347\263\273\347\273\237+.md" @@ -0,0 +1,322 @@ +~~~ java +//工具类 +package Util; + +import java.sql.*; + +public class DBUtil { + static String url="jdbc:mysql:///attdb?useUnicode=true&characterEncoding=utf8"; + static String user="root"; + static String pwd="36932211"; + 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, 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) { + throw new RuntimeException(e); + } + 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) { + throw new RuntimeException(e); + } + return num; + + } + + public static void Close(Connection conn, PreparedStatement pst, ResultSet rs){ + try { + if (rs!=null){ + rs.close(); + } + if (pst!=null){ + pst.close(); + } + if (conn!=null){ + conn.close(); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} + +~~~ + +~~~jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: 苏晓 + Date: 2023/6/6 + Time: 21:46 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 学生考勤系统 + + +

考勤系统

+
+ + + + + + + + + + + + + + + + +
学生姓名 + +
考勤时间
考勤状况 + 已到 + 迟到 + 旷课 +
+
+ + + +~~~ + +~~~jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: 苏晓 + Date: 2023/6/6 + Time: 20:10 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 目录 + + + + + + + + + + + + + + + + + + + + + + +
考勤编号学生编号学生姓名出勤时间出勤状况
${stu.aid}${stu.sid}${stu.sname}${stu.time} + + 已到 + + + 迟到 + + + 旷课 + +
+ + + +~~~ + +~~~ java +package Servlet; + +import Bean.studentt; +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("sid"); + String sname = rs.getString("sname"); + studentt studentt = new studentt(sid,sname); + } + 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 { + + } +} + +~~~ + +~~~ java +package Servlet; + +import Util.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; + +@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"); +// 接受浏览器的数据 + String sname=request.getParameter("sname"); + String sid=request.getParameter("sid"); + String type=request.getParameter("type"); + String time=request.getParameter("time"); + String sql="insert into Attence values(?,?,?,?)"; + int i; + try { + i= DBUtil.update(sql,"0",time,type,sid); + DBUtil.Close(null,null,null); + } catch (Exception e) { + throw new RuntimeException(e); + } +// 进行一个判断,是否添加数据进去 + if (i>0){ + request.setAttribute("save","添加成功"); + request.getRequestDispatcher("/WEB-INF/save.jsp").forward(request,response); + }else { + request.setAttribute("save","添加失败"); + request.getRequestDispatcher("/WEB-INF/save.jsp").forward(request,response); + } + } +} + +~~~ + +~~~java +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.*; +import java.util.ArrayList; + +@WebServlet("/list") +public class List extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + ArrayList list = new ArrayList<>(); + String sql="select *from Student s,Attence a where s.sid=a.sid"; + 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("aid"); + int sid = rs.getInt("sid"); + String sname = rs.getString("sname"); + Date time = rs.getDate("time"); + String type = rs.getString("type"); + Student student = new Student(aid,sid,sname,time,type); + list.add(student); + + } + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + DBUtil.Close(conn,pst,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 { + + } +} + +~~~ + -- Gitee