From 62b7abaaa888ca81954db081861d87ce6ce07350 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?16=20=E9=98=99=E8=8B=8F=E6=96=87?= <2361635242@qq.com> Date: Wed, 7 Jun 2023 22:24:16 +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 --- ...41\347\220\206\344\275\234\344\270\232.md" | 387 ++++++++++++++++++ 1 file changed, 387 insertions(+) create mode 100644 "16 \351\230\231\350\213\217\346\226\207/20230607\350\200\203\345\213\244\347\256\241\347\220\206\344\275\234\344\270\232.md" diff --git "a/16 \351\230\231\350\213\217\346\226\207/20230607\350\200\203\345\213\244\347\256\241\347\220\206\344\275\234\344\270\232.md" "b/16 \351\230\231\350\213\217\346\226\207/20230607\350\200\203\345\213\244\347\256\241\347\220\206\344\275\234\344\270\232.md" new file mode 100644 index 0000000..fe11af8 --- /dev/null +++ "b/16 \351\230\231\350\213\217\346\226\207/20230607\350\200\203\345\213\244\347\256\241\347\220\206\344\275\234\344\270\232.md" @@ -0,0 +1,387 @@ +# 作业 + +```java +package bean; + +public class Student { + int sid; + String sname; + + @Override + public String toString() { + return "Student{" + + "sid=" + sid + + ", 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; + } + + public Student() { + } + + public Student(int sid, String sname) { + this.sid = sid; + this.sname = sname; + } +} +``` + +```java +package bean; + +import javax.naming.ldap.PagedResultsControl; + +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 +package dbutil; + +import java.sql.*; + +//定义数据库名,用户名和密码; +public class DBUtil { + static String url="jdbc:mysql:///attdb?useSSL=false&characterEhcding=utf8"; + static String user="root"; + static String prw="root"; + + //注册驱动 + 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, prw); + 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 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) { + e.printStackTrace(); + } + return num; + } +} +``` + +```java +package servlet; + +import bean.Student; +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("/add") +public class AddServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.getRequestDispatcher("utf-8"); + String sql="select * from student"; + ResultSet rs = DBUtil.query(sql); + ArrayList list = new ArrayList<>(); + try { + 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) { + e.printStackTrace(); + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/add.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + //接收表单数据,处理添加到数据库attence + request.getRequestDispatcher("utf-8"); + String sid=request.getParameter("sid"); + String time=request.getParameter("time"); + String type=request.getParameter("type"); + + String sql="insert into attence values (?,?,?,?)"; + int i = DBUtil.update(sql,null,time,type,sid); + 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); + } + } +} +``` + +```java +package servlet; + +import bean.Attence; +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("/list") +public class ListServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.getRequestDispatcher("utf8"); + String sql="select * from attence a,student s where a.sid=s.sid order by aid"; + ResultSet rs = DBUtil.query(sql); + ArrayList list = new ArrayList<>(); + try { + while (rs.next()){ + int aid=rs.getInt("aid"); + String time=rs.getString("time"); + int type = rs.getInt("type"); + int sid = rs.getInt("sid"); + String sname = rs.getString("sname"); + Attence attence = new Attence(aid, time, type, sid, sname); + list.add(attence); + } + } catch (SQLException e) { + e.printStackTrace(); + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} +``` + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 添加考勤 + + +

学生考勤系统

+
+ + + + + + + + + + + + + + + + +
学生姓名 + +
添加时间
考勤状态 + 已到: + 迟到: + 旷课: +
+
+ + +``` + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 考勤列表 + + +添加 + + + + + + + + + + + + + + + + + +
考勤编号学生编号学生姓名出勤时间出勤状况
${a.aid}${a.sid}${a.sname}${a.time} + + + 已到 + + + + 迟到 + + + + 旷课 + +
+ + +``` + +```jsp +<%-- + Created by IntelliJ IDEA. + User: 86185 + Date: 2023/6/7 + Time: 22:12 + 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 -- Gitee