diff --git "a/44 \344\273\243\347\221\236/6\346\234\2106\346\227\245\350\200\203\345\213\244\344\275\234\344\270\232.md" "b/44 \344\273\243\347\221\236/6\346\234\2106\346\227\245\350\200\203\345\213\244\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..64f08c86f269f101529d709f8acbc5125f8319cb --- /dev/null +++ "b/44 \344\273\243\347\221\236/6\346\234\2106\346\227\245\350\200\203\345\213\244\344\275\234\344\270\232.md" @@ -0,0 +1,397 @@ +# 作业 + +## bean + +### Attence + +``` +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 + +``` +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 + +``` +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 + +``` +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 + +``` +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 + +``` +<%@ 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 + +``` +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 添加 + + +
+ +

学生考勤系统

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

${xx}

+
+返回列表 + + +``` \ No newline at end of file diff --git "a/44 \344\273\243\347\221\236/\345\244\247\350\200\203.md" "b/44 \344\273\243\347\221\236/\345\244\247\350\200\203.md" deleted file mode 100644 index 9e6031ff1bc23e9f459ee86f6884e9726df53284..0000000000000000000000000000000000000000 --- "a/44 \344\273\243\347\221\236/\345\244\247\350\200\203.md" +++ /dev/null @@ -1,475 +0,0 @@ -## bean包 - -### Brand类 - -``` -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类 - -``` -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类 - -``` -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类 - -``` -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包 - -``` -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类 - -``` -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 - -``` -<%@ 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 - -``` -<%@ 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 - -``` -<%-- - 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}

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