diff --git "a/29 \347\216\213\351\233\257\351\235\231/20230603\346\261\275\350\275\246\347\256\241\347\220\206\347\263\273\347\273\237.md" "b/29 \347\216\213\351\233\257\351\235\231/20230603\346\261\275\350\275\246\347\256\241\347\220\206\347\263\273\347\273\237.md" new file mode 100644 index 0000000000000000000000000000000000000000..b6440d5d75e7984e2d79cc0dc26a2c46dc431c4b --- /dev/null +++ "b/29 \347\216\213\351\233\257\351\235\231/20230603\346\261\275\350\275\246\347\256\241\347\220\206\347\263\273\347\273\237.md" @@ -0,0 +1,484 @@ +```java +create database carinfo charset utf8; + +use carinfo; + +create table Brand( +BrandID int primary key auto_increment, +BrandName varchar(50) +); + +create table cardetail( +CID int primary key auto_increment, +Cname varchar(20), +Content varchar(200) not null, +ltime timestamp default now() not null, +price int, +BrandID int, +foreign key (BrandID) references Brand(BrandID) +); + +insert into Brand values (0,'奔驰'),(0,'宝马'),(0,'劳斯莱斯'); + +insert into CarDetail values (0,'保时捷','豪车','2016.06.04',300000000,1),(0,'奥迪','好车','2013.06.04',5392000000,2),(0,'劳斯莱斯','好车','2020.06.04',5000000,3); +``` + +```java +package bean; + +public class Brand { + private int id; + private String name; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Brand(int id, String name) { + this.id = id; + this.name = name; + } + + public Brand() { + } + + @Override + public String toString() { + return "Brand{" + + "id=" + id + + ", name='" + name + '\'' + + '}'; + } +} +package bean; + +import java.util.Date; + +public class Car { + private int id; + private String name; + private String content; + private Date time; + private int price; + private int brandID; + + public Car(int id, String name, String content, java.sql.Date date, int price, int i, String brandName) { + + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + 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) { + this.brandID = brandID; + } + + public Car(int id, String name, String content, Date time, int price, int brandID) { + this.id = id; + this.name = name; + this.content = content; + this.time = time; + this.price = price; + this.brandID = brandID; + } + + public Car() { + } + + @Override + public String toString() { + return "Car{" + + "id=" + id + + ", name='" + name + '\'' + + ", content='" + content + '\'' + + ", time=" + time + + ", price=" + price + + ", brandID=" + brandID + + '}'; + } +} + +``` + +```java +package servlet; + +import bean.Car; +import utils.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 { + String sql ="select * from cardetail c , brand b where b.BrandID=c.BrandID"; + 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 id = rs.getInt(1); + int price = rs.getInt(5); + String name = rs.getString(2); + String content = rs.getString(3); + String brandName = rs.getString(8); + Date date = rs.getDate(4); + Car car = new Car(id, name, content, date, price, 0, brandName); + list.add(car); + + } + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + DBUtil.close(conn,pst,rs); + } + + + request.setAttribute("list",list); + request.getRequestDispatcher("web/WEB-INF/list.jsp").forward(request,response); + + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + + + + + + + + + +package servlet; + +import bean.Brand; +import bean.Car; +import utils.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +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 pst = null; + ResultSet rs = null; + try { + conn = DBUtil.getConn(); + pst = conn.prepareStatement(sql); + rs = pst.executeQuery(); + while (rs.next()){ + int id = rs.getInt(1); + String name = rs.getString(2); + Brand brand = new Brand(id, name); + list.add(brand); + + } + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + DBUtil.close(conn,pst,rs); + } + + + request.setAttribute("list",list); + request.getRequestDispatcher("web/WEB-INF/add.jsp").forward(request,response); + + + } + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + + +package servlet; + +import utils.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.Connection; +import java.sql.Date; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.text.SimpleDateFormat; + +@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 name = request.getParameter("name"); + int brandid = Integer.parseInt(request.getParameter("brandid")); + String content = request.getParameter("content"); + int price = Integer.parseInt(request.getParameter("price")); + String date = request.getParameter("date"); + //SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD"); + // Date parse = sdf.parse(date); + + String sql = "insert into cardetail values(?,?,?,?,?,?)"; + int i = 0; + Connection conn = null; + PreparedStatement pst = null; + try { + conn = DBUtil.getConn(); + pst = conn.prepareStatement(sql); + pst.setInt(1, 0); + pst.setString(2, name); + pst.setString(3, content); + pst.setDate(4, Date.valueOf(date)); + pst.setInt(5, price); + pst.setInt(6, brandid); + i = pst.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + DBUtil.close(conn, pst, null); + } + if (i > 0) { + response.sendRedirect("/list"); + + } else { + request.setAttribute("mgs","添加失败"); + request.getRequestDispatcher("/WEB-INF/mgs.jsp").forward(request,response); + + } + + + } +} + +``` + +```java +package utils; + +import java.sql.*; + +public class DBUtil { + static String url = "jdbc:mysql:///carinfo?characterEncoding=utf8"; + static String user = "329"; + static String pwd = "041727"; + + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + + } + + public static Connection getConn() throws SQLException { + Connection connection = DriverManager.getConnection(url, user, pwd); + return connection; + } + public static void close(Connection conn, PreparedStatement ps ,ResultSet rs){ + try { + if (rs!=null){ + rs.close(); + } + if (ps!=null){ + ps.close(); + } + if (conn!=null){ + conn.close(); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} + +``` + +```java +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: November + Date: 2023/6/4 + Time: 20:27 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + $Title$ + + + + + + + + + + + + + + + + + + + + + + + +
序号车辆名称车辆品牌车辆介绍价格录入时间
${car.id}${car.name}${car.idrandName}${car.content}${car.price}${car.time}
+ + + + + +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: November + Date: 2023/6/4 + Time: 20:44 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
车辆名称 +
所属品牌 + +
车辆简介 +
价格 +
录入时间 +
+
+ + + + + + + +``` diff --git "a/29 \347\216\213\351\233\257\351\235\231/20230605\350\200\203\345\213\244\347\256\241\347\220\206\347\263\273\347\273\237.md" "b/29 \347\216\213\351\233\257\351\235\231/20230605\350\200\203\345\213\244\347\256\241\347\220\206\347\263\273\347\273\237.md" new file mode 100644 index 0000000000000000000000000000000000000000..07f338f9519094220591b485a3fc7680de237093 --- /dev/null +++ "b/29 \347\216\213\351\233\257\351\235\231/20230605\350\200\203\345\213\244\347\256\241\347\220\206\347\263\273\347\273\237.md" @@ -0,0 +1,496 @@ +```java +create database AttDB character set 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 +(0,'张三'), +(0,'李四'), +(0,'王五'); + +insert into Attence values +(0,'2022-05-20 08:20:00',1,1), +(0,'2022-05-23 08:20:00',2,1), +(0,'2022-05-23 13:40:00',2,2), +(0,'2022-05-27 08:20:00',3,2), +(0,'2022-05-30 08:20:00',2,3); + +package bean; + +public class Attence { + private int aid; + private String time; + private int type; + private int sid; + private String sname; + + public Attence(int aid, String time) { + this.aid = aid; + this.time = time; + this.type = type; + this.sid = sid; + this.sname =sname; + } + + 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 + + package bean; + +public class Student { + private int sid; + private String sname; + + public Student(int sid, String name) { + this.sid = sid; + this.sname = name; + } + + public Student() { + } + + public int getSid() { + return sid; + } + + public void setSid(int sid) { + this.sid = sid; + } + + public String getName() { + return sname; + } + + public void setName(String name) { + this.sname = name; + } + + @Override + public String toString() { + return "Student{" + + "sid=" + sid + + ", name='" + sname + '\package servlet; + +import bean.Attence; +import bean.Student; +import utils.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(1); + String sname = rs.getString(2); + Student student = new Student(sid,sname); + list.add(student); + + } + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + DBUtil.close(conn,pst,rs); + } + + + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/add.jsp").forward(request,response); + + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse responsepackage servlet; + +import bean.Attence; +import bean.Student; +import utils.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("/list") +public class list 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"; + 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 aid = rs.getInt(1); + int type = rs.getInt(3); + int sid = rs.getInt(4); + String time = rs.getString(2); + String sname = rs.getString(6); + Attence attence = new Attence(aid,time,type,sid,sname); + list.add(attence); + + } + } 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 respopackage servlet; + +import utils.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.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 sid = String.valueOf(Integer.parseInt(request.getParameter("sid"))); + int aid = Integer.parseInt(request.getParameter("aid")); + String time = request.getParameter("time"); + int type = Integer.parseInt(request.getParameter("type")); + System.out.println("time = " + time); + System.out.println("type = " + type); + String sql = "insert into Attence values(?,?,?,?)"; + int i; + Connection conn = null; + PreparedStatement pst = null; + try { + conn = DBUtil.getConn(); + pst = conn.prepareStatement(sql); + pst.setInt(1, 0); + pst.setInt(4, 0); + pst.setString(2, time); + pst.setInt(3, type); + i = pst.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + DBUtil.close(conn, pst, null); + } + if (i > 0) { + request.setAttribute("/save","添加成功"); + request.getRequestDispatcher("/WEB-INF/save.jsp").forward(request,response); + + } else { + request.setAttribute("save", "添加失败"); + request.getRequestDispatcher("/WEB-INF/save.jsppackage utils; + +import java.sql.*; + +public class DBUtil { + static String url = "jdbc:mysql:///AttDB?characterEncoding = utf8"; + static String user ="root"; + static String pwd ="root"; + + 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 rst = null; + try { + Connection conn = getConn(); + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length; i++) { + pst.setObject((i+1),keys[i]); + + } + rst = pst.executeQuery(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return rst; + } + 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) { + <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-05 + Time: 10:21 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + +

+ 学生考勤系统 +

+
+ + + + + + + + + + + + + + + +
学生姓名
出勤时间
出勤状况 + 已到 + 迟到 + 旷课 +
+<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-05 + Time: 09:45 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + + + + + + + + + + + + + + + + + + + + + + +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +${save} + + +on>添加 +
考勤编号学生编号学生姓名出勤时间出勤状况
${v.aid}${v.sid}${v.sname}${v.time} + 已到 + + + 迟到 + + + 旷课 + +
+ + +td> +
+
+ + + throw new RuntimeException(e); + } + } +} +").forward(request,response); + } + + + + } +} +nse) throws ServletException, IOException { + + } +} +) throws ServletException, IOException { + + } +} +'' + + '}'; + } +} + ", sname='" + sname + '\'' + + '}'; + } +} + + + + +``` + +```java +四个驱动包 +```