From 48572c042ca6bc7104b292a7a72c9e98e671c122 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=99=B4?= <691705597@qq.com> Date: Sun, 4 Jun 2023 22:04:33 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=AC=AC=E4=BA=94=E6=AC=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230604 \344\275\234\344\270\232.md" | 408 ++++++++++++++++++ 1 file changed, 408 insertions(+) create mode 100644 "37 \347\216\213\346\231\264/20230604 \344\275\234\344\270\232.md" diff --git "a/37 \347\216\213\346\231\264/20230604 \344\275\234\344\270\232.md" "b/37 \347\216\213\346\231\264/20230604 \344\275\234\344\270\232.md" new file mode 100644 index 0000000..03c0ab5 --- /dev/null +++ "b/37 \347\216\213\346\231\264/20230604 \344\275\234\344\270\232.md" @@ -0,0 +1,408 @@ +```javascript +package DBUtil; + +import java.sql.*; + +public class DBUtil { + private static final String url="jdbc:mysql//localhost:3306/CarInfo?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String user="root"; + private static final String pwd="1234"; + + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + + public static Connection getConn() throws SQLException { + Connection conn = DriverManager.getConnection(url,user,pwd); + return conn; + } + + + public static void close(Connection conn, PreparedStatement pre, ResultSet re) { + try { + if (re != null){ + re.close(); + } + if (pre != null) { + pre.close(); + } + if (conn != null) { + conn.close(); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} + + + +``` + +```java +package bean; + +public class brand { + private int BrandID; + private String BrandName; + + @Override + public String toString() { + return "brand{" + + "BrandID=" + BrandID + + ", BrandName='" + BrandName + '\'' + + '}'; + } + + public int getBrandID() { + return BrandID; + } + + public void setBrandID(int brandID) { + BrandID = brandID; + } + + public String getBrandName() { + return BrandName; + } + + public void setBrandName(String brandName) { + BrandName = brandName; + } + + public brand() { + } + + public brand(int brandID, String brandName) { + BrandID = brandID; + BrandName = brandName; + } +} + +``` + +```java +package bean; + +import java.util.Date; + +public class CarDetail { + private int cid; + private String cname; + private String content; + private Date time; + private int price; + private int BrandID; + private String BrandName; + + @Override + public String toString() { + return "CarDetail{" + + "cid=" + cid + + ", cname='" + cname + '\'' + + ", content='" + content + '\'' + + ", time=" + time + + ", price=" + price + + ", BrandID=" + BrandID + + ", BrandName='" + BrandName + '\'' + + '}'; + } + + public int getCid() { + return cid; + } + + public void setCid(int cid) { + this.cid = cid; + } + + public String getCname() { + return cname; + } + + public void setCname(String cname) { + this.cname = cname; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public Date getTime() { + return time; + } + + public void setTime(Date time) { + this.time = time; + } + + public int getPrice() { + return price; + } + + public void setPrice(int price) { + this.price = price; + } + + public int getBrandID() { + return BrandID; + } + + public void setBrandID(int brandID) { + BrandID = brandID; + } + + public String getBrandName() { + return BrandName; + } + + public void setBrandName(String brandName) { + BrandName = brandName; + } + + public CarDetail() { + } + + public CarDetail(int cid, String cname, String content, Date time, int price, int brandID, String brandName) { + this.cid = cid; + this.cname = cname; + this.content = content; + this.time = time; + this.price = price; + BrandID = brandID; + BrandName = brandName; + } +} + +``` + +```java +package servlet; + +import bean.brand; +import javax.servlet.*; +import DBUtil.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.Connection; +import java.sql.*; +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 brand"; + ArrayList list = new ArrayList<>(); + Connection conn = null; + PreparedStatement pre = null; + ResultSet re = null; + try { + conn = DBUtil.getConn(); + pre = conn.prepareStatement(sql); + re = pre.executeQuery(); + while (re.next()){ + int id = re.getInt(1); + String name = re.getString(2); + brand brand = new brand(id, name); + list.add(brand); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + DBUtil.close(conn,pre,re); + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/form.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +``` + +```java +package servlet; + +import DBUtil.DBUtil; +import bean.CarDetail; + +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; +import java.util.Date; + +@WebServlet("/select") +public class select extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String sql = "select * from cardetail c,brand b where c.BrandID = b.BrandID"; + ArrayList list = new ArrayList<>(); + Connection conn = null; + PreparedStatement pre = null; + ResultSet re = null; + try { + conn = DBUtil.getConn(); + pre = conn.prepareStatement(sql); + re = pre.executeQuery(); + while (re.next()) { + int cid = re.getInt(1); + String cname = re.getString(2); + String brandname = re.getString(8); + String contcet = re.getString(3); + int price = re.getInt(5); + Date time = re.getDate(8); + CarDetail carDetail = new CarDetail(cid, cname, contcet, time, price, 0, brandname); + list.add(carDetail); + } + + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + DBUtil.close(conn,pre,re); + } + request.setAttribute("list", list); + request.getRequestDispatcher("/WEB-INF/select.jsp").forward(request, response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +``` + +```html +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: leafrosey + Date: 2023/6/4 + Time: 21:10 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + + + + + + + + + + + + + + + + + + + + +
序号车辆名称车辆品牌车辆简介价格录入时间
${cardetail.cid}${cardetail.cname}${cardetail.BrandName}${cardetail.content}${cardetail.price}${cardetail.time}
+ + + +``` + +```html +<%-- + Created by IntelliJ IDEA. + User: leafrosey + Date: 2023/6/4 + Time: 22:20 + To change this template use File | Settings | File Templates. +--%> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +
+ + + + + + + + + + + + + + + + + + + + + + +
车辆名称
所属品牌 + +
车辆简介
价格
录入时间
+
+ + + +``` + +```html +<%-- + Created by IntelliJ IDEA. + User: leafrosey + Date: 2023/6/4 + Time: 22:31 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +返回列表 + + + +``` + -- Gitee From 81ef98b447907d5fff40233796660957d7270f3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=99=B4?= <691705597@qq.com> Date: Tue, 6 Jun 2023 22:30:54 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=AC=AC=E5=85=AD=E6=AC=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230606\344\275\234\344\270\232.md" | 455 ++++++++++++++++++ 1 file changed, 455 insertions(+) create mode 100644 "37 \347\216\213\346\231\264/20230606\344\275\234\344\270\232.md" diff --git "a/37 \347\216\213\346\231\264/20230606\344\275\234\344\270\232.md" "b/37 \347\216\213\346\231\264/20230606\344\275\234\344\270\232.md" new file mode 100644 index 0000000..154ba73 --- /dev/null +++ "b/37 \347\216\213\346\231\264/20230606\344\275\234\344\270\232.md" @@ -0,0 +1,455 @@ +工具包 + +```java +package DBUtil; + +import java.sql.*; +import java.util.Objects; + +public class DBUtil { + static String url = "jdbc:mysql://localhost:3306/AttDB?useSSL=false&useUnicode=true&characterEncoding=utf8"; + static String user ="root"; + static String pwd ="root"; + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + 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,String... keys) throws SQLException { + Connection conn = getConn(); + PreparedStatement pre = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pre.setString((i+1),keys[i]); + } + ResultSet re = pre.executeQuery(); + return re; + } + public static int Update(String sql, String... keys) throws SQLException { + Connection conn = getConn(); + PreparedStatement pre = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pre.setString((i+1),keys[i]); + } + int i = pre.executeUpdate(); + return i; + } + public static void close(Connection conn,PreparedStatement pre,ResultSet re) throws SQLException { + if (conn!=null){ + conn.close(); + } + if (pre!=null){ + pre.close(); + } + if (re!=null){ + re.close(); + } + } +} + +``` + +类 + +```java +package bean; + +public class Attence { + private int aid; + private String time ; + private int type; + private int sid; + + public Attence() { + } + + private String sname; + + @Override + public String toString() { + return "Attence{" + + "aid=" + aid + + ", time='" + time + '\'' + + ", type=" + type + + ", sid=" + sid + + ", sname='" + sname + '\'' + + '}'; + } + + 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 int getAid() { + return aid; + } + + public void setAid(int aid) { + this.aid = aid; + } + + 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; + } + +} + +``` + +```java +package bean; + +public class studend { + private int sid; + private String sname; + + @Override + public String toString() { + return "studend{" + + "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 studend() { + } + + public studend(int sid, String sname) { + this.sid = sid; + this.sname = sname; + } +} + +``` + +list + +```java +package servlet; + +import DBUtil.DBUtil; +import bean.Attence; + +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(name = "list", value = "/list") +public class list extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String sql = "select * from Student s,Attence a where s.sid=a.sid"; + ArrayList list; + try { + ResultSet re = DBUtil.query(sql); + list = new ArrayList<>(); + while (re.next()) { + int aid = re.getInt(3); + String time = re.getString(4); + int type = re.getInt(5); + int sid = re.getInt(1); + String sname = re.getString(2); + Attence attence = new Attence(aid, time, type, sid, sname); + list.add(attence); + } + DBUtil.close(null, null, re); + } catch (SQLException e) { + throw new RuntimeException(e); + } + 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 + +```java +package servlet; + +import DBUtil.DBUtil; +import bean.studend; +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(name = "add", value = "/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<>(); + try { + ResultSet re = DBUtil.query(sql); + while (re.next()){ + int sid = re.getInt(1); + String sname = re.getString(2); + studend studend = new studend(sid,sname); + list.add(studend); + } + DBUtil.close(null,null,re); + } 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 { + + } +} + +``` + +save + +```java +package servlet; + +import DBUtil.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.SQLException; + +@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"); + System.out.println("time = " + time); + System.out.println(type); + String sql = "insert into attence values(?,?,?,?)"; + int i; + try { + i = DBUtil.Update(sql, "0", time, type, sid); + DBUtil.close(null, null, null); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + //判断 + if (i > 0) { + // response.sendRedirect("/list"); + 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); + } + } +} + +``` + + + +```jsp +<%@ page import="java.util.ArrayList" %> +<%@ page import="bean.Attence" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-05 + Time: 09:34 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + + + + + + + + + + + + + + + + + + + +
考勤编号学生编号学生姓名出勤时间出勤状况
${Attence.aid}${Attence.sid}${Attence.sname}${Attence.time} + + 已到 + + + 迟到 + + + 旷课 + +
+ + + +``` + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-05 + Time: 10:55 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +

学生考勤系统

+
+ + + + + + + + + + + + + + + + + +
学生姓名 + +
考勤时间
考勤状况 + + 已到 + 迟到 + 旷课 +
+
+ + + +``` + +```jsp +<%-- + Created by IntelliJ IDEA. + User: leafrosey + Date: 2023/6/6 + Time: 16:30 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +${save} +
+返回 + + + +``` + -- Gitee