diff --git "a/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/.gitignore" "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/.gitignore" new file mode 100644 index 0000000000000000000000000000000000000000..f68d1099657e34d4e7a68aadc730b3ecad84667d --- /dev/null +++ "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/.gitignore" @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git "a/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/Day20230604.iml" "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/Day20230604.iml" new file mode 100644 index 0000000000000000000000000000000000000000..673ae60c714c96ffe4393d279294a5298481d381 --- /dev/null +++ "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/Day20230604.iml" @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git "a/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/src/Bean/Brand.java" "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/src/Bean/Brand.java" new file mode 100644 index 0000000000000000000000000000000000000000..38e2ee875fc1b033bc49d27c29bacfd96d9a8d66 --- /dev/null +++ "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/src/Bean/Brand.java" @@ -0,0 +1,39 @@ +package Bean; + +//品牌类 +public class Brand { + private int BrandID; //品牌编号 + private String BrandName; //品牌名称 + + public Brand() { + } + + public Brand(int brandID, String brandName) { + 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; + } + + @Override + public String toString() { + return "Brand{" + + "BrandID=" + BrandID + + ", BrandName='" + BrandName + '\'' + + '}'; + } +} diff --git "a/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/src/Bean/CarDetail.java" "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/src/Bean/CarDetail.java" new file mode 100644 index 0000000000000000000000000000000000000000..6e86911f8891d999f05dc642b1f585615494b780 --- /dev/null +++ "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/src/Bean/CarDetail.java" @@ -0,0 +1,105 @@ +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) { + this.cid = cid; + this.CName = CName; + Content = content; + this.LTime = LTime; + this.price = price; + BrandID = brandID; + } + + //只有品牌名称,没有品牌编号 + public CarDetail(int cid, String CName, String content, Date LTime, int price, String brandName) { + this.cid = cid; + this.CName = CName; + Content = content; + this.LTime = LTime; + this.price = price; + 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) { + 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) { + BrandName = brandName; + } + + @Override + public String toString() { + return "CarDetail{" + + "cid=" + cid + + ", CName='" + CName + '\'' + + ", Content='" + Content + '\'' + + ", LTime=" + LTime + + ", price=" + price + + ", BrandID=" + BrandID + + '}'; + } + +} diff --git "a/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/src/Dao/BrandDao.java" "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/src/Dao/BrandDao.java" new file mode 100644 index 0000000000000000000000000000000000000000..c402fdd9d04bb859aa49b7f43ef37a490565a029 --- /dev/null +++ "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/src/Dao/BrandDao.java" @@ -0,0 +1,28 @@ +package Dao; + +import Bean.Brand; +import Utils.DBUtils; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +public class BrandDao { + //重数据库查询所有品牌 + public static ArrayList getAllBrand(){ + String sql = "select * from brand"; + ResultSet rs = DBUtils.query(sql); + ArrayList list = new ArrayList<>(); + try { + while (rs.next()){ + int id = rs.getInt(1); + String name = rs.getString(2); + Brand b = new Brand(id, name); + list.add(b); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + return list; + } +} diff --git "a/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/src/Dao/CarDao.java" "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/src/Dao/CarDao.java" new file mode 100644 index 0000000000000000000000000000000000000000..ec19478fcae6a15f48b580a8ef63018e5ae5f920 --- /dev/null +++ "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/src/Dao/CarDao.java" @@ -0,0 +1,46 @@ +package Dao; + +import Bean.CarDetail; +import Utils.DBUtils; + +import java.sql.Date; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +public class CarDao { + + //获取所有汽车信息方法 + public static ArrayList getAllCar() { + //创建一个集合用来存放汽车对象 + ArrayList list = new ArrayList<>(); + String sql = "select * from brand b left join cardetail c on b.BrandID=c.BrandID;"; + ResultSet rs = DBUtils.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,brandName); + list.add(car); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + return list; + } + + //添加汽车方法 + public static int addCar(CarDetail car){ + //编写sql语句 + String sql = "insert into cardetail values(?,?,?,?,?,?)"; + //将获取的对象拆封赋值给每个? + int i = DBUtils.update(sql, car.getCid(), car.getCName(), car.getContent(), car.getLTime(), car.getPrice(), car.getBrandID()); + return i; + } +} diff --git "a/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/src/Servlet/CarServlet.java" "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/src/Servlet/CarServlet.java" new file mode 100644 index 0000000000000000000000000000000000000000..c440781e404d771e126cbcfa8ef03d63535c4edf --- /dev/null +++ "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/src/Servlet/CarServlet.java" @@ -0,0 +1,82 @@ +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 CarServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + //处理乱码问题 + req.setCharacterEncoding("utf-8"); + //获取/*具体的路径 + String path = req.getPathInfo(); + System.out.println(path); + //根据path判断逻辑代码(默认路径为/car或/car/就显示所有车辆信息) + if (path == null || path.equals("/")) { + //调用DAO查询方法,得到装有所有车辆信息的集合 + ArrayList list = CarDao.getAllCar(); + //将集合放到request请求域对象中 + req.setAttribute("list", list); + //将请求转发给一个负责显示的jsp + req.getRequestDispatcher("/WEB-INF/carList.jsp").forward(req, resp); + } else if (path.equals("/add")) { + //先从数据库查询所有品牌信息 + ArrayList list = BrandDao.getAllBrand(); + //将品牌信息封装好后发给from.jsp + req.setAttribute("list", list); + req.getRequestDispatcher("/WEB-INF/form.jsp").forward(req, resp); + } + + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + //处理乱码问题 + req.setCharacterEncoding("utf-8"); + //获取/*具体的路径 + String path = req.getPathInfo(); + //根据path判断逻辑代码 + if(path.endsWith("/save")) { + //获取表单数据 + String cname = req.getParameter("cname"); + int brandid = Integer.parseInt(req.getParameter("brandid")); + String content = req.getParameter("content"); + int price = Integer.parseInt(req.getParameter("price")); + String date = req.getParameter("date"); + + //得到的时间是字符串,要解析成date类型 + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + Date newDate; + try { + newDate = sdf.parse(date); + } catch (ParseException e) { + throw new RuntimeException(e); + } + //将表单获取的数据封装成对象传给Dao + CarDetail car = new CarDetail(0, cname, content, newDate, price, brandid); + int i = CarDao.addCar(car); + if(i>0){ + //重定向(添加成功,刷新页面) + resp.sendRedirect("/car"); + }else{ + //将添加失败信息传给msg显示 + req.setAttribute("smg","添加失败"); + req.getRequestDispatcher("/WEB-INF/msg.jsp").forward(req,resp); + } + } + } +} diff --git "a/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/src/Test.java" "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/src/Test.java" new file mode 100644 index 0000000000000000000000000000000000000000..58c44261abc87198b3d182041e05c68e117e23f4 --- /dev/null +++ "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/src/Test.java" @@ -0,0 +1,33 @@ +import Bean.CarDetail; +import Utils.DBUtils; + +import java.sql.Date; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Calendar; + +public class Test { + public static void main(String[] args) { + //创建一个集合用来存放汽车对象 + ArrayList list = new ArrayList<>(); + String sql ="select * from CarDetail"; + ResultSet rs = DBUtils.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"); + //将获取的数据封装成一个对象并添加到集合 + CarDetail car = new CarDetail(cid, cname, content, lTime, price, brandID); + list.add(car); + System.out.println(car); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} diff --git "a/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/src/Utils/DBUtils.java" "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/src/Utils/DBUtils.java" new file mode 100644 index 0000000000000000000000000000000000000000..de90ee1dfdd66daef275d583b7668c8b4c4b010f --- /dev/null +++ "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/src/Utils/DBUtils.java" @@ -0,0 +1,71 @@ +package Utils; + +import java.sql.*; + +public class DBUtils { + //1.定义数据库地址,用户名,密码,utf8编码 + public static final String URL = "jdbc:mysql:///carInfo?useSSL=false&useUnicode=true&characterEncoding=utf8"; + public static final String USER = "root"; + public static final String PSW = "root"; + + //2.注册驱动 + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + + //3.获取连接对象方法 + public static Connection getConn() { + Connection conn; + try { + conn = DriverManager.getConnection(URL, USER, PSW); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return conn; + } + + //4.通用的查询方法 + public static ResultSet query(String sql, Object... keys) { + ResultSet rs; + try { + //1.获取连接对象 + Connection conn = getConn(); + //2.获取执行sql语句的对象,并预编译 + PreparedStatement prs = conn.prepareStatement(sql); + //2.5预编译后,遍历keys数组,将获取的数据赋值给每个? + for (int i = 0; i < keys.length; i++) { + prs.setObject((i + 1), keys[i]); + } + rs = prs.executeQuery(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + //返回结果集 + return rs; + } + + //5.通用的update方法。(负责增,删,改) + public static int update(String sql, Object... keys) { + + int num = 0; + try { + //1.获取连接对象 + Connection conn = getConn(); + //2.获取执行sql语句的对象,并预编译 + PreparedStatement prs = conn.prepareStatement(sql); + //2.5预编译后,遍历keys数组,将获取的数据赋值给每个? + for (int i = 0; i < keys.length; i++) { + prs.setObject((i + 1), keys[i]); + } + num = prs.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + //返回一个受影响的函数 + return num; + } +} diff --git "a/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/web/WEB-INF/carList.jsp" "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/web/WEB-INF/carList.jsp" new file mode 100644 index 0000000000000000000000000000000000000000..b441226107847df87f4bea9499a9a79c2ade07ca --- /dev/null +++ "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/web/WEB-INF/carList.jsp" @@ -0,0 +1,45 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: 111 + Date: 2023/6/4 + Time: 15:50 + 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}
+ + diff --git "a/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/web/WEB-INF/form.jsp" "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/web/WEB-INF/form.jsp" new file mode 100644 index 0000000000000000000000000000000000000000..17fa5940ca50eb61992cde3577b3b138b38a481c --- /dev/null +++ "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/web/WEB-INF/form.jsp" @@ -0,0 +1,55 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: 111 + Date: 2023/6/4 + Time: 17:02 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 添加 + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
车辆名称
所属品牌 + +
车辆简介
价格
录入时间
+
+ + diff --git "a/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/web/WEB-INF/lib/jstl.jar" "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/web/WEB-INF/lib/jstl.jar" new file mode 100644 index 0000000000000000000000000000000000000000..a02abecc8b888f09e4d1f9e4c9790dc482426d4f Binary files /dev/null and "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/web/WEB-INF/lib/jstl.jar" differ diff --git "a/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/web/WEB-INF/lib/mysql-connector-java-5.1.48.jar" "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/web/WEB-INF/lib/mysql-connector-java-5.1.48.jar" new file mode 100644 index 0000000000000000000000000000000000000000..bd1c4bbc218ccff3c5a0dc9a09d248c54c577fd8 Binary files /dev/null and "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/web/WEB-INF/lib/mysql-connector-java-5.1.48.jar" differ diff --git "a/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/web/WEB-INF/lib/servlet-api.jar" "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/web/WEB-INF/lib/servlet-api.jar" new file mode 100644 index 0000000000000000000000000000000000000000..a8208fbb4a7d2db192f70f641b42c4d549b32084 Binary files /dev/null and "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/web/WEB-INF/lib/servlet-api.jar" differ diff --git "a/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/web/WEB-INF/lib/standard.jar" "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/web/WEB-INF/lib/standard.jar" new file mode 100644 index 0000000000000000000000000000000000000000..bc528acb949096eeb2e3048de1ff5b9ab12a66c5 Binary files /dev/null and "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/web/WEB-INF/lib/standard.jar" differ diff --git "a/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/web/WEB-INF/msg.jsp" "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/web/WEB-INF/msg.jsp" new file mode 100644 index 0000000000000000000000000000000000000000..01ab841e00c3ef67f1979a5f72348fbf76b119bf --- /dev/null +++ "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/web/WEB-INF/msg.jsp" @@ -0,0 +1,16 @@ +<%-- + Created by IntelliJ IDEA. + User: 111 + Date: 2023/6/4 + Time: 23:35 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 提示信息 + + +

${msg}

+ + diff --git "a/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/web/WEB-INF/web.xml" "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/web/WEB-INF/web.xml" new file mode 100644 index 0000000000000000000000000000000000000000..d80081d1318531b6c30eaf0d748bf80a0b2e042a --- /dev/null +++ "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/Day20230604/web/WEB-INF/web.xml" @@ -0,0 +1,6 @@ + + + \ No newline at end of file diff --git "a/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/mysql \344\273\243\347\240\201.md" "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/mysql \344\273\243\347\240\201.md" new file mode 100644 index 0000000000000000000000000000000000000000..4990fce00625e2481d9c635e2658d78514d30045 --- /dev/null +++ "b/14 \346\235\216\344\277\212\345\205\264/20230603 \347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/mysql \344\273\243\347\240\201.md" @@ -0,0 +1,42 @@ +## Mysql + +~~~ mysql +-- 创建数据库,编写字符集 +create database CarInfo charset utf8; +use CarInfo; + +-- 创建(品牌表) +create table Brand( + BrandID int primary key auto_increment, -- 品牌编号 + BrandName varchar(50) not null -- 品牌名称 +); + +-- 创建(车型明细表) +create table CarDetail( + CID int primary key auto_increment, -- 序号 + Cname Varchar(20), -- 车型名称 + Content varchar(200) not null, -- 车型简介 + ltime TIMESTAMP not null, -- 录入时间 + price int, -- 参考价格 + BrandID int, -- 所属品牌编号 + -- 外键,车辆所属品牌编号 + foreign key(BrandID) references Brand(BrandID) +); + +-- 给品牌表添加初始数据 +insert into Brand values +(null,"奔驰"), +(null,"奥迪"), +(null,"宝马"); + +-- 给车型明细表添加初始数据 +insert into CarDetail values +(null,"奔驰C200","豪车","2021-01-03 18:48:10",300000,1), +(null,"奥迪A4L","适合年轻人的一款车","2021-01-03 18:48:10",410000,2), +(null,"宝马330Li","许多人喜欢买这款车","2021-01-03 18:48:10",400000,3); + +select * from brand b left join cardetail c on b.BrandID=c.BrandID; +select * from brand; +select * from CarDetail; +~~~ + diff --git "a/14 \346\235\216\344\277\212\345\205\264/Day0530/.idea/.gitignore" "b/14 \346\235\216\344\277\212\345\205\264/Day0530/.idea/.gitignore" new file mode 100644 index 0000000000000000000000000000000000000000..35410cacdc5e87f985c93a96520f5e11a5c822e4 --- /dev/null +++ "b/14 \346\235\216\344\277\212\345\205\264/Day0530/.idea/.gitignore" @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git "a/14 \346\235\216\344\277\212\345\205\264/Day0530/.idea/artifacts/Day0530_war_exploded.xml" "b/14 \346\235\216\344\277\212\345\205\264/Day0530/.idea/artifacts/Day0530_war_exploded.xml" new file mode 100644 index 0000000000000000000000000000000000000000..b9f2fd2c5b4cb6deb8f06927a3ce5b78838ae403 --- /dev/null +++ "b/14 \346\235\216\344\277\212\345\205\264/Day0530/.idea/artifacts/Day0530_war_exploded.xml" @@ -0,0 +1,13 @@ + + + $PROJECT_DIR$/out/artifacts/Day0530_war_exploded + + + + + + + + + + \ No newline at end of file diff --git "a/14 \346\235\216\344\277\212\345\205\264/Day0530/.idea/libraries/lib.xml" "b/14 \346\235\216\344\277\212\345\205\264/Day0530/.idea/libraries/lib.xml" new file mode 100644 index 0000000000000000000000000000000000000000..5796aca9cd1838ea7d7e55f014e0e8f80afa0c6f --- /dev/null +++ "b/14 \346\235\216\344\277\212\345\205\264/Day0530/.idea/libraries/lib.xml" @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git "a/14 \346\235\216\344\277\212\345\205\264/Day0530/.idea/misc.xml" "b/14 \346\235\216\344\277\212\345\205\264/Day0530/.idea/misc.xml" new file mode 100644 index 0000000000000000000000000000000000000000..921e39b2e4376cad022d5a90c918fbf445870377 --- /dev/null +++ "b/14 \346\235\216\344\277\212\345\205\264/Day0530/.idea/misc.xml" @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git "a/14 \346\235\216\344\277\212\345\205\264/Day0530/.idea/modules.xml" "b/14 \346\235\216\344\277\212\345\205\264/Day0530/.idea/modules.xml" new file mode 100644 index 0000000000000000000000000000000000000000..3e68dc5aa18a203fdc79e9d4a1f9868d462a1ec3 --- /dev/null +++ "b/14 \346\235\216\344\277\212\345\205\264/Day0530/.idea/modules.xml" @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git "a/14 \346\235\216\344\277\212\345\205\264/Day0530/Day0530.iml" "b/14 \346\235\216\344\277\212\345\205\264/Day0530/Day0530.iml" new file mode 100644 index 0000000000000000000000000000000000000000..74ba473fb0c255ff3dddae0713f4f08d623406ae --- /dev/null +++ "b/14 \346\235\216\344\277\212\345\205\264/Day0530/Day0530.iml" @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git "a/14 \346\235\216\344\277\212\345\205\264/Day0530/src/DBUtil.java" "b/14 \346\235\216\344\277\212\345\205\264/Day0530/src/DBUtil.java" new file mode 100644 index 0000000000000000000000000000000000000000..c8a286f848606408971db5be2072b33b027f4dca --- /dev/null +++ "b/14 \346\235\216\344\277\212\345\205\264/Day0530/src/DBUtil.java" @@ -0,0 +1,83 @@ +import java.sql.*; + +public class DBUtil { // Math System + // 数据库的主机名,端口号,数据库名,关闭SSL,采用Unicode字符集,并以Utf8为编码 + private static final String url = "jdbc:mysql:///student_db?useSSL=false&useUnicode=true&characterEncoding=utf8";// localhost:3306是默认的主机和端口号,可以省略掉 + private static final String user = "root"; // 用户名 + private static final String password = "root";// 密码 + + // 注册驱动 + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + System.out.println("驱动注册异常"); + e.printStackTrace();// 开发调试阶段,为了排查具体错误,要把详细的异常信息打印 + } + } + + // 获取连接对象 // static方法是静态方法,是直接通过类名访问的 + public static Connection getConn() { + Connection conn = null;// alt+回车 + try { + conn = DriverManager.getConnection(url, user, password); + } catch (SQLException e) { + System.out.println("获取连接对象失败"); + e.printStackTrace(); + } + return conn; + } + + // 一个通用查询方法,接收SQL语句,返回结果集 + public static ResultSet query(String sql, String... keys) { + // String sql = "update student set id=? , name =? , sex =? where id=?"; + // 形参前加... 就表示其是可变参数,就是数组 + // 获取连接对象 + ResultSet rs = null; + try { + Connection conn = getConn(); + // 建立预编译SQL的对象 + PreparedStatement pst = conn.prepareStatement(sql);// 将?号转成变量 + // 给每个变量赋值 + // 遍历可变参数 + for (int i = 0; i < keys.length; i++) { + pst.setString((i + 1), keys[i]); + } + // 执行SQL + rs = pst.executeQuery(); + } catch (SQLException e) { + System.out.println("执行查询异常"); + e.printStackTrace(); + } + return rs; + } + + // ,一个通用更新的方法(删除,添加,修改), + public static int update(String sql, String... keys) { + // String sql = "update student set id=? , name =? , sex =? where id=?"; + // 形参前加... 就表示其是可变参数,就是数组 + // 获取连接对象 + int num = 0; + try { + Connection conn = getConn(); + // 建立预编译SQL的对象 + PreparedStatement pst = conn.prepareStatement(sql);// 将?号转成变量 + // 给每个变量赋值 + // 遍历可变参数 + for (int i = 0; i < keys.length; i++) { + pst.setString((i + 1), keys[i]); + } + // 执行SQL + num = pst.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + return num; + + // + // + // 一个通用的关闭资源的方法 + + } +} diff --git "a/14 \346\235\216\344\277\212\345\205\264/Day0530/src/Student.java" "b/14 \346\235\216\344\277\212\345\205\264/Day0530/src/Student.java" new file mode 100644 index 0000000000000000000000000000000000000000..9cb53d40d26810cfd55e483a056abf0b0b08fb2c --- /dev/null +++ "b/14 \346\235\216\344\277\212\345\205\264/Day0530/src/Student.java" @@ -0,0 +1,69 @@ +public class Student { + private int id; + private int age; + private String name; + private String sex; + private String address; + + @Override + public String toString() { + return "Student{" + + "id=" + id + + ", age=" + age + + ", name='" + name + '\'' + + ", sex='" + sex + '\'' + + ", address='" + address + '\'' + + '}'; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSex() { + return sex; + } + + public void setSex(String sex) { + this.sex = sex; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public Student() { + } + + public Student(int id, int age, String name, String sex, String address) { + this.id = id; + this.age = age; + this.name = name; + this.sex = sex; + this.address = address; + } +} diff --git "a/14 \346\235\216\344\277\212\345\205\264/Day0530/src/stu.java" "b/14 \346\235\216\344\277\212\345\205\264/Day0530/src/stu.java" new file mode 100644 index 0000000000000000000000000000000000000000..5e618d198124f471a75f100697df1255720cd388 --- /dev/null +++ "b/14 \346\235\216\344\277\212\345\205\264/Day0530/src/stu.java" @@ -0,0 +1,42 @@ +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 = "stu", value = "/stu") +public class stu extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // 从数据库取出数据,封装到一个集合 + ArrayList list = new ArrayList<>(); + String sql="select * from student"; + ResultSet rs = DBUtil.query(sql); + try { + while (rs.next()){ + int id = rs.getInt("id"); + int age = rs.getInt("age"); + String name = rs.getString("name"); + String sex = rs.getString("sex"); + String address = rs.getString("address"); + // 将查到的每行数据,封装成一个学生对象,再把学生对象,封装到集合 + Student stu = new Student(id, age, name, sex, address); + list.add(stu); + } + } catch (SQLException e) { + e.printStackTrace(); + } + System.out.println(list); + // 将集合请求转发给/stu.jsp + request.setAttribute("stuList",list);// 转发之前.先把集合放入请求域中 + request.getRequestDispatcher("/stu.jsp").forward(request,response); + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} diff --git "a/14 \346\235\216\344\277\212\345\205\264/Day0530/src/test.java" "b/14 \346\235\216\344\277\212\345\205\264/Day0530/src/test.java" new file mode 100644 index 0000000000000000000000000000000000000000..0966537bb2a94223127d813fe7a4e3a50814ff08 --- /dev/null +++ "b/14 \346\235\216\344\277\212\345\205\264/Day0530/src/test.java" @@ -0,0 +1,35 @@ +import com.sun.jmx.snmp.SnmpString; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.util.ArrayList; + +@WebServlet(name = "test", value = "/test") +public class test extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String name="上山打老虎"; + request.setAttribute("name",name); + ArrayList list = new ArrayList<>(); + list.add("张三"); + list.add("张三2"); + list.add("张三3"); + list.add("张三4"); + list.add("张三5"); + for (String n : list) { + System.out.println(n); + } + + + request.setAttribute("names",list);// 将list集合放入到request里的names属性里(变量) + // 请求转发 + request.getRequestDispatcher("/test.jsp").forward(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} diff --git "a/14 \346\235\216\344\277\212\345\205\264/Day0530/web/WEB-INF/lib/jstl.jar" "b/14 \346\235\216\344\277\212\345\205\264/Day0530/web/WEB-INF/lib/jstl.jar" new file mode 100644 index 0000000000000000000000000000000000000000..a02abecc8b888f09e4d1f9e4c9790dc482426d4f Binary files /dev/null and "b/14 \346\235\216\344\277\212\345\205\264/Day0530/web/WEB-INF/lib/jstl.jar" differ diff --git "a/14 \346\235\216\344\277\212\345\205\264/Day0530/web/WEB-INF/lib/mysql-connector-java-5.1.48.jar" "b/14 \346\235\216\344\277\212\345\205\264/Day0530/web/WEB-INF/lib/mysql-connector-java-5.1.48.jar" new file mode 100644 index 0000000000000000000000000000000000000000..bd1c4bbc218ccff3c5a0dc9a09d248c54c577fd8 Binary files /dev/null and "b/14 \346\235\216\344\277\212\345\205\264/Day0530/web/WEB-INF/lib/mysql-connector-java-5.1.48.jar" differ diff --git "a/14 \346\235\216\344\277\212\345\205\264/Day0530/web/WEB-INF/lib/servlet-api.jar" "b/14 \346\235\216\344\277\212\345\205\264/Day0530/web/WEB-INF/lib/servlet-api.jar" new file mode 100644 index 0000000000000000000000000000000000000000..daf690af39e115e15974b23843a3987eb3089e18 Binary files /dev/null and "b/14 \346\235\216\344\277\212\345\205\264/Day0530/web/WEB-INF/lib/servlet-api.jar" differ diff --git "a/14 \346\235\216\344\277\212\345\205\264/Day0530/web/WEB-INF/lib/standard.jar" "b/14 \346\235\216\344\277\212\345\205\264/Day0530/web/WEB-INF/lib/standard.jar" new file mode 100644 index 0000000000000000000000000000000000000000..bc528acb949096eeb2e3048de1ff5b9ab12a66c5 Binary files /dev/null and "b/14 \346\235\216\344\277\212\345\205\264/Day0530/web/WEB-INF/lib/standard.jar" differ diff --git "a/14 \346\235\216\344\277\212\345\205\264/Day0530/web/WEB-INF/web.xml" "b/14 \346\235\216\344\277\212\345\205\264/Day0530/web/WEB-INF/web.xml" new file mode 100644 index 0000000000000000000000000000000000000000..d80081d1318531b6c30eaf0d748bf80a0b2e042a --- /dev/null +++ "b/14 \346\235\216\344\277\212\345\205\264/Day0530/web/WEB-INF/web.xml" @@ -0,0 +1,6 @@ + + + \ No newline at end of file diff --git "a/14 \346\235\216\344\277\212\345\205\264/Day0530/web/index.jsp" "b/14 \346\235\216\344\277\212\345\205\264/Day0530/web/index.jsp" new file mode 100644 index 0000000000000000000000000000000000000000..f062726f8faceb973cd99fc5732c55cfd8d3144a --- /dev/null +++ "b/14 \346\235\216\344\277\212\345\205\264/Day0530/web/index.jsp" @@ -0,0 +1,16 @@ +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-05-30 + Time: 08:16 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + $Title$ + + + $END$ + + diff --git "a/14 \346\235\216\344\277\212\345\205\264/Day0530/web/stu.jsp" "b/14 \346\235\216\344\277\212\345\205\264/Day0530/web/stu.jsp" new file mode 100644 index 0000000000000000000000000000000000000000..67d0cf1812042516991c555fa2207077d2aabd2b --- /dev/null +++ "b/14 \346\235\216\344\277\212\345\205\264/Day0530/web/stu.jsp" @@ -0,0 +1,52 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + +

添加学生

+
+ + + + + + + + + + <%--list集合要遍历,需要jstl标签库--%> + + + + + + + + + + +
编号姓名年龄性别地址操作
${student.id} ${student.name} ${student.age} ${student.sex}${student.address} + + 修改 + 删除 +
+ + + diff --git "a/14 \346\235\216\344\277\212\345\205\264/Day0530/web/test.jsp" "b/14 \346\235\216\344\277\212\345\205\264/Day0530/web/test.jsp" new file mode 100644 index 0000000000000000000000000000000000000000..fbee2b5c895d90cad611f7999fed115a450c140b --- /dev/null +++ "b/14 \346\235\216\344\277\212\345\205\264/Day0530/web/test.jsp" @@ -0,0 +1,14 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + + + ${n}
+
+ + +