From 15384c29644c784e6b09759007b6d60f1de22e4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B0=B8=E6=B7=B3?= <2678158018@qq.com> Date: Wed, 7 Jun 2023 22:34:23 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=BA=94=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" | 437 ++++++++++++++++++ 1 file changed, 437 insertions(+) create mode 100644 "18 \345\276\220\346\260\270\346\267\263/20230607 jsp\345\255\246\347\224\237\350\200\203\345\213\244\347\263\273\347\273\237.md" diff --git "a/18 \345\276\220\346\260\270\346\267\263/20230607 jsp\345\255\246\347\224\237\350\200\203\345\213\244\347\263\273\347\273\237.md" "b/18 \345\276\220\346\260\270\346\267\263/20230607 jsp\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..a04cbca --- /dev/null +++ "b/18 \345\276\220\346\260\270\346\267\263/20230607 jsp\345\255\246\347\224\237\350\200\203\345\213\244\347\263\273\347\273\237.md" @@ -0,0 +1,437 @@ +```mysql +create database AttDB charset utf8; +use AttDB; +create table student( +sid int primary key auto_increment,#主键,自动增长列 +sname varchar(20) unique not null 唯一,非空 + +); +create table attence( +aid int primary key auto_increment, +time varchar(20) not null, +type int comment " 1:已到;2:迟到;3旷课",# +sid int, # 外键 +foreign key (sid) references student(sid) +); +insert into student values +(null,wing), +(null,wing_zero), +(null,epyon); +insert into attence values +(null,"1995-4-7",1,1), +(null,"1995-12-7",2,2), +(null,"1995-12-28",3,3); +``` + + + +```java +bean类 +student类 +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 + '\'' + + '}'; + } +} + + +``` + + + +```java +bean类 +attence类 +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 +工具类 +import java.sql.*; + +public class DBUtil { + private static final String URL="jdbc:mysql:///AttDB?characterEncoding=utf-8&useSSL=false&useUnicode=true"; + private static final String USER="root"; + private static final String PWD="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, PWD); + return conn; + } + + public static ResultSet select(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 +sevrlect类 +AddServlect类 +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 { + String sql="select * from student"; + ResultSet rs = DBUtil.select(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 { + request.setCharacterEncoding("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 +servlect类 +StudentServlet类 +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("/student") +public class StudentServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String sql="select a* from attence a,student s where a.sid=s.sid"; + ResultSet rs = DBUtil.select(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 +jsp +查询 +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: 86173 + Date: 2023/6/6 + Time: 20:01 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + +添加 + +
+ + + + + + + + + + + + + + <%-- --%> + + + +
考勤编号学号学生姓名出勤时间出勤状况
${stu.aid}${stu.sid}${stu.sname}${stu.time}${stu.type} + + 已到 + + + 迟到 + + + 旷课 + +
+ + + +``` + + + +```jsp +添加 +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + +
+

学生考勤系统

+ + + + + + + + + + + + + + + + +
学生姓名 + +
考勤时间 + +
考勤状况 + 已到: + 迟到: + 旷课: +
+ +
+
+ + + +``` + + + +```jsp +信息提示 +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 提示信息 + + +

${msg}

+
+返回列表 + + +``` + + + -- Gitee