diff --git "a/01 \350\213\217\346\270\205\345\215\216/20230529 \344\277\256\346\224\271\345\210\240\351\231\244.md" "b/01 \350\213\217\346\270\205\345\215\216/20230529 \344\277\256\346\224\271\345\210\240\351\231\244.md" new file mode 100644 index 0000000000000000000000000000000000000000..a3f1caa69894fa6c7fac42bf79024cf7df343a17 --- /dev/null +++ "b/01 \350\213\217\346\270\205\345\215\216/20230529 \344\277\256\346\224\271\345\210\240\351\231\244.md" @@ -0,0 +1,12 @@ +```mysql +create database student_db CHARSET utf8; +use student_db; +create table student(id int PRIMARY key, +name VARCHAR(10), +sex char(5) default '女'); +insert into student values +(1,"高坂穗乃果","女"), +(2,"园田海未","女"), +(3,"南琴梨","女"); +``` + diff --git "a/01 \350\213\217\346\270\205\345\215\216/20230603 car.md" "b/01 \350\213\217\346\270\205\345\215\216/20230603 car.md" new file mode 100644 index 0000000000000000000000000000000000000000..a4032fc8696f4dfefbb0b165f43f05c163914c73 --- /dev/null +++ "b/01 \350\213\217\346\270\205\345\215\216/20230603 car.md" @@ -0,0 +1,500 @@ +# 数据库 + +```java +create database CarInfo charset utf8; +use CarInfo; +create table Brand +(BrandID int primary key auto_increment, +BrandName varchar(50) not null); +insert into Brand values +(1,"奔驰"), +(2,"奥迪"); +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 CarDetail values +(1,"奔驰C200","豪车",NOW(),300000,1), +(2,"奥迪A4L","适合年轻人的一款车",NOW(),410000,2); +``` + +## bean包 + +### Brand类 + +```java +package bean; + +public class Brand { + private int brandId; + private String brandName; + + public Brand() { + } + + public Brand(int brandId, String brandName) { + this.brandId = brandId; + this.brandName = brandName; + } + + public int getBrandId() { + return brandId; + } + + public void setBrandId(int brandId) { + this.brandId = brandId; + } + + public String getBrandName() { + return brandName; + } + + public void setBrandName(String brandName) { + this.brandName = brandName; + } + + @Override + public String toString() { + return "Brand{" + + "brandId=" + brandId + + ", brandName='" + brandName + '\'' + + '}'; + } +} +``` + +### CarDetail类 + +```java +package bean; + +import java.util.Date; + +public class CarDetail { + private int cId; + private String cName; + private String content; + private Date lTime; + private int price; + private int BrandID; + private String brandName; + + public CarDetail() { + } + + public CarDetail(int cId, String cName, String content, Date lTime, int price, int brandID, String brandName) { + this.cId = cId; + this.cName = cName; + this.content = content; + this.lTime = lTime; + this.price = price; + BrandID = brandID; + this.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 getlTime() { + return lTime; + } + + public void setlTime(Date lTime) { + this.lTime = lTime; + } + + 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) { + this.brandName = brandName; + } + + @Override + public String toString() { + return "CarDetail{" + + "cId=" + cId + + ", cName='" + cName + '\'' + + ", content='" + content + '\'' + + ", lTime=" + lTime + + ", price=" + price + + ", BrandID=" + BrandID + + ", brandName='" + brandName + '\'' + + '}'; + } +} +``` + +## utils包 + +### DBUtil类 + +```java +package utils; + +import java.sql.*; + +public class DBUtil { + private static final String URL="jdbc:mysql:///CarInfo?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) { + System.out.println("驱动注册失败"); + e.printStackTrace(); + } + } + + public static Connection getConn() throws SQLException { + Connection conn = DriverManager.getConnection(URL, USER, PWD); + 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; + } +} +``` + +## dao包 + +### CarDao类 + +```java +package dao; + +import bean.CarDetail; +import utils.DBUtil; + +import java.sql.Date; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +public class CarDao { + public ArrayList getAllCar(){ + ArrayList list = new ArrayList<>(); + String sql="select * from CarDetail c,brand b where c.brandId=b.brandId"; + ResultSet rs = DBUtil.query(sql); + try { + while (rs.next()){ + int cid = rs.getInt("cid"); + String cname = rs.getString("cname"); + String content = rs.getString("content"); + Date ltime = rs.getDate("ltime"); + int price = rs.getInt("price"); + int brandid = rs.getInt("brandid"); + String brandName = rs.getString("brandname"); + CarDetail car = new CarDetail(cid, cname, content, ltime, price, brandid,brandName); + list.add(car); + } + } catch (SQLException e) { + e.printStackTrace(); + } + return list; + } + + public int addCar(CarDetail car){ + String sql="insert into CarDetail values(?,?,?,?,?,?)"; + int i = DBUtil.update(sql,car.getcId(),car.getcName(),car.getContent(),car.getlTime(),car.getPrice(),car.getBrandID()); + return i; + } +} +``` + +### BrandDao包 + +```java +package dao; + +import bean.Brand; +import utils.DBUtil; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +public class BrandDao { + public ArrayList getAllBrand(){ + String sql="select * from brand"; + ResultSet rs = DBUtil.query(sql); + ArrayList list = new ArrayList<>(); + try { + 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) { + e.printStackTrace(); + } + return list; + } +} +``` + +## servlet包 + +### CarController类 + +```java +package servlet; + +import bean.Brand; +import bean.CarDetail; +import dao.BrandDao; +import dao.CarDao; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; + +@WebServlet("/car/*") +public class CarController extends HttpServlet { + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + response.setContentType("text/html;charset=utf-8"); + String path=request.getPathInfo(); + if(path.equals("/save")){ + 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 simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); + Date newDate = null; + try { + newDate = simpleDateFormat.parse(date); + } catch (ParseException e) { + e.printStackTrace(); + } + CarDetail car = new CarDetail(0, name, content, newDate, price, brandid, null); + int i = new CarDao().addCar(car); + if(i>0){ + //response.sendRedirect("/car"); + 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); + } + } + + } + + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + response.setContentType("text/html;charset=utf-8"); + String path=request.getPathInfo(); + if(path==null || path.equals("/")){ + ArrayList list = new CarDao().getAllCar(); + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/carList.jsp").forward(request,response); + }else if(path.equals("/add")){ + ArrayList list = new BrandDao().getAllBrand(); + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/form.jsp").forward(request,response); + } + } +} +``` + +## 网页 + +### 显示页面carList + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023/6/3 + Time: 11:08 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + +
+ + + + + + + + + + + + + + + + + + + +
序号车辆名称所属品牌车辆简介价格录入时间
${car.cId}${car.cName}${car.brandName}${car.content}${car.price}${car.lTime}
+ + +``` + +### 添加页面add + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023/6/3 + Time: 14:24 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 添加 + + +
+ + + + + + + + + + + + + + + + + + + + + + + +
车辆名称
所属品牌 + +
车辆简介
价格
录入时间
+
+ + +``` + +### 添加成功页面msg + +```jsp +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023/6/3 + Time: 15:07 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 提示信息 + + +

${msg}

+ + +``` + diff --git "a/01 \350\213\217\346\270\205\345\215\216/20230605 attdb.md" "b/01 \350\213\217\346\270\205\345\215\216/20230605 attdb.md" new file mode 100644 index 0000000000000000000000000000000000000000..3cfa440d658be08493381cd065d910dc1ed9db50 --- /dev/null +++ "b/01 \350\213\217\346\270\205\345\215\216/20230605 attdb.md" @@ -0,0 +1,503 @@ +# 数据库 + +```mysql +create database AttDB character set utf8; +use AttDB; + +create table Student( +sid int primary key auto_increment, -- 学号 +sname varchar(20) unique not null -- 学生姓名 +); +insert into Student values(0,'张三'); +insert into Student values(0,'李四'); +insert into Student values(0,'王五'); + +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 Attence values(0,'2022-05-20 08:20:00',1,1); +insert into Attence values(0,'2022-05-23 08:20:00',2,1); +insert into Attence values(0,'2022-05-23 13:40:00',2,2); +insert into Attence values(0,'2022-05-27 08:20:00',3,2); +insert into Attence values(0,'2022-05-30 08:20:00',2,3); +``` + +# bean + +## attence + +```java +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, int type, int sid) { + this.aid = aid; + this.time = time; + this.type = type; + this.sid = sid; + } + + @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; + } +} + +``` + + # dao + +## attencedao + +```java +package dao; + +import bean.Attence; +import util.DBUtil; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +public class AttenceDao { + //1.获取信息 + public List getAllAttdb(){ + List list = new ArrayList<>(); + String sql="select * from attence a,student s where a.sid=s.sid;"; + ResultSet rs = DBUtil.query(sql); + 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(); + } + return list; + } + //2.添加信息 + public int addAttdb(Attence attence){ + String sql="insert into attence values(?,?,?,?)"; + int i=DBUtil.update(sql,attence.getAid(),attence.getTime(),attence.getType(),attence.getSid()); + return i; + } +} + +``` + +## studentdao + +```java +package dao; + +import bean.Student; +import util.DBUtil; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +public class StudentDao { + //获取信息 + public ArrayList getAllStudent(){ + String sql="select * from student"; + ResultSet rs = DBUtil.query(sql); + ArrayList list = new ArrayList<>(); + try { + 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) { + e.printStackTrace(); + } + return list; + } +} + +``` + +# servlet + +```java +package servlet; + +import bean.Attence; +import bean.Student; +import dao.AttenceDao; +import dao.StudentDao; +import util.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +@WebServlet("/attdb/*") +public class AttdbController extends HttpServlet { + AttenceDao attDao = new AttenceDao(); + StudentDao stuDao = new StudentDao(); + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + response.setContentType("text/html;charset=utf-8"); + String pathInfo = request.getPathInfo(); + if (pathInfo == null || pathInfo.equals("/")) { + List list = attDao.getAllAttdb(); + request.setAttribute("list", list); + request.getRequestDispatcher("/WEB-INF/attdblist.jsp").forward(request, response); + } else if (pathInfo.equals("/add")) { + List list = stuDao.getAllStudent(); + request.setAttribute("list", list); + request.getRequestDispatcher("/WEB-INF/addattdb.jsp").forward(request, response); + } + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + response.setContentType("text/html;charset=utf-8"); + String pathInfo = request.getPathInfo(); + if (pathInfo.equals("/save")) { + int sid = Integer.parseInt(request.getParameter("sid")); + String time = request.getParameter("time"); + int type = Integer.parseInt(request.getParameter("type")); + Attence attence = new Attence(0, time, type, sid); + int i = attDao.addAttdb(attence); + 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); + } + } + } +} + +``` + +# util + +```java +package util; + +import java.sql.*; + +public class DBUtil { + + static String url="jdbc:mysql:///AttDB?characterEncoding=utf-8&useSSL=false&useUnicode=true"; + static String user="root"; + static String password="122319"; + //1.注册驱动 + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } + //2.获取连接 + public static Connection getConn() throws SQLException { + Connection conn = DriverManager.getConnection(url, user, password); + return conn; + } + //3.通用query + 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) { + throw new RuntimeException(e); + } + return rs; + } + //4.通用update + 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; + } + //5.通用close + 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) { + throw new RuntimeException(e); + } + } +} + +``` + +# jsp + +## add + +```java +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: sqh + Date: 2023/6/6 + Time: 15:39 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 添加 + + +
+

学生考勤系统

+ + + + + + + + + + + + + + + + +
学生姓名 + +
考勤时间
考勤状况 + 已到: + 迟到: + 旷课: +
+
+ + + +``` + +## list + +```java +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: sqh + Date: 2023/6/6 + Time: 15:15 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 学生考勤 + + +添加 + + + + + + + + + + + + + + + + + + +
考勤编号学生编号学生姓名出勤时间出勤状况
${stu.aid}${stu.sid}${stu.sname}${stu.time} + + 已到 + + + 迟到 + + + 旷课 + +
+ + + +``` + +## msg + +```java +<%-- + Created by IntelliJ IDEA. + User: sqh + Date: 2023/6/6 + Time: 17:01 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 提示 + + +

${msg}

+ + + +``` \ No newline at end of file