From 13d58a07227e221c27a215c71152281291db0853 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=B9=E6=AD=A3=E6=B3=A2?= <1938448998@qq.com> Date: Tue, 6 Jun 2023 23:35:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=AD=E6=9C=88=E5=85=AD=E5=8F=B7=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...C\345\244\215\344\271\240\351\242\2302.md" | 405 ++++++++++++++++++ 1 file changed, 405 insertions(+) create mode 100644 "09 \346\233\271\346\255\243\346\263\242/20230606 JAVA-JDBC\345\244\215\344\271\240\351\242\2302.md" diff --git "a/09 \346\233\271\346\255\243\346\263\242/20230606 JAVA-JDBC\345\244\215\344\271\240\351\242\2302.md" "b/09 \346\233\271\346\255\243\346\263\242/20230606 JAVA-JDBC\345\244\215\344\271\240\351\242\2302.md" new file mode 100644 index 0000000..dc6a093 --- /dev/null +++ "b/09 \346\233\271\346\255\243\346\263\242/20230606 JAVA-JDBC\345\244\215\344\271\240\351\242\2302.md" @@ -0,0 +1,405 @@ +# 作业 + +## bean + +### Attence + +```java +package bean; + +public class Attence { + private int aid;//考勤编号 + private String time;//出勤时间 + private int type;//出勤状况 + private int sid;//学号 + private String sname;//姓名 + + @Override + public String toString() { + return "Attence{" + + "aid=" + aid + + ", time='" + time + '\'' + + ", type=" + type + + ", sid=" + sid + + ", 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; + } + + 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; + } +} +``` + +### Student + +```java +package bean; + +public class Student { + private int sid; + private 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; + } +} + +``` + +## DButlis + +### dbutlis + +```java +package utlis; + +import java.sql.*; + +public class DButlis { + static String url="jdbc:mysql:///attdb? characterEncoding=utf8"; + static String use="root"; + static String pas="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, use, pas); + return conn; + } + public static ResultSet query(String sql,Object... kery){ + ResultSet re = null; + try { + Connection conn = getconn(); + PreparedStatement per = conn.prepareStatement(sql); + for (int i = 0; i < kery.length; i++) { + per.setObject((i+1),kery[i]); + } + re = per.executeQuery(); + } catch (SQLException e) { + e.printStackTrace(); + } + return re; + } + public static int update(String sql,Object... kery){ + int re = 0; + try { + Connection conn = getconn(); + PreparedStatement per = conn.prepareStatement(sql); + for (int i = 0; i < kery.length; i++) { + per.setObject((i+1),kery[i]); + } + re = per.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + return re; + } +} + +``` + +## Fservler + +### SeleServler + +```java +package FServler; + +import bean.Attence; +import utlis.DButlis; + +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("/sele") +public class SeleServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String sql="select * from attence a,student s where a.sid=s.sid order by aid"; + ResultSet que = DButlis.query(sql); + ArrayList list = new ArrayList<>(); + try { + while (que.next()){ + int aid=que.getInt("aid"); + String time=que.getString("time"); + int type=que.getInt("type"); + int sid=que.getInt("sid"); + String sname=que.getString("sname"); + Attence att = new Attence(aid, time, type, sid, sname); + list.add(att); + } + } catch (SQLException e) { + e.printStackTrace(); + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/sele.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +``` + +### AddServler + +```java +package Fservler; + +import bean.Student; +import utlis.DButlis; + +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 que = DButlis.query(sql); + ArrayList list = new ArrayList<>(); + try { + while (que.next()){ + int sid= que.getInt("sid"); + String sname= que.getString("sname"); + Student stu = new Student(sid, sname); + list.add(stu); + } + } 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 (?,?,?,?)"; +// String sql="insert into attence values (null,'2009-9-9 15:44:33',1,2)"; +// String sql="select * from attence"; + int i = DButlis.update(sql,null,time,type,sid); + if (i>0){ + request.setAttribute("xx","添加成功!"); + request.getRequestDispatcher("/WEB-INF/xinxi.jsp").forward(request,response); + }else{ + request.setAttribute("xx","添加失败!"); + request.getRequestDispatcher("/WEB-INF/xinxi.jsp").forward(request,response); + } + } +} + +``` + +## JSP + +### sele + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 查看 + + + +添加 +
+ + + + + + + + + + + + + + + + + +
考勤编号学生编号学生姓名出勤时间出勤状况
${n.aid}${n.sid}${n.sname}${n.time} + + 已到 + + + 迟到 + + + 旷课 + +
+ + + +``` + +### add + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 添加 + + +
+ +

学生考勤系统

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

${xx}

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