From d4d496236e929313761f2922cbe227d6bf8dcd87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B6=E5=AD=90=E8=B1=AA?= <2936219414@qq.com> Date: Mon, 5 Jun 2023 23:15:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../JSP\344\275\234\344\270\232 4.md" | 370 ++++++++++++++++++ 1 file changed, 370 insertions(+) create mode 100644 "54 \345\217\266\345\255\220\350\261\252/JSP\344\275\234\344\270\232 4.md" diff --git "a/54 \345\217\266\345\255\220\350\261\252/JSP\344\275\234\344\270\232 4.md" "b/54 \345\217\266\345\255\220\350\261\252/JSP\344\275\234\344\270\232 4.md" new file mode 100644 index 0000000..6e71d7b --- /dev/null +++ "b/54 \345\217\266\345\255\220\350\261\252/JSP\344\275\234\344\270\232 4.md" @@ -0,0 +1,370 @@ +package Bean; + +public class Brand { + int brandId; + String brandName; + +```java +package Bean; + +public class Brand { + int brandId; + String 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; +} + +public Brand() { + +} + +public Brand(int brandId, String brandName) { + this.brandId = brandId; + this.brandName = brandName; +} + } +``` + + +~~~java + +package Bean; + +import java.util.Date; + +public class CarDatail { + private int cId; + private String cName; + private String content; + private Date lTime; + private int price; + private int BrandID; + + 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 CarDatail() { + } + + public CarDatail(int cId, String cName, String content, Date lTime, int price, int brandID) { + this.cId = cId; + this.cName = cName; + this.content = content; + this.lTime = lTime; + this.price = price; + BrandID = brandID; + } +} + +~~~ + + + +~~~ java +package Dao; + +import Bean.CarDatail; +import Utiis.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"; + + ResultSet re = DBUtil.query(sql); + + try { + while (re.next()){ + int cid = re.getInt("cid"); + String cname = re.getString("cname"); + String content = re.getString("content"); + Date ltime = re.getDate("time"); + int price = re.getInt("price"); + int brandid = re.getInt("brandid"); + CarDatail car = new CarDatail(cid, cname, content, ltime, price, brandid); + list.add(car); + } + } catch (SQLException e) { + e.printStackTrace(); + } + return list; + } + + public int addCar(CarDatail 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; + } +} + +~~~ + + + +~~~ java +package Utiis; + +import java.sql.*; + +public class DBUtil { + private static final String url = "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String username = "root"; + private static final String password = "root"; + + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } + +public static Connection getConn(){ + + Connection conn = null; + try { + conn = DriverManager.getConnection(url, username, password); + } catch (SQLException e) { + e.printStackTrace(); + } + return conn; + } + + + public static ResultSet query(String sql,Object... keys) { // 通用的查询方法. + ResultSet re = null; + try { + Connection conn = getConn(); + + PreparedStatement pre = conn.prepareStatement(sql); + + for (int i = 0; i < keys.length; i++) { + pre.setObject((i+1),keys[i]); + } + re = pre.executeQuery(); + } catch (SQLException e) { + e.printStackTrace(); + } + return re; + } + + + + public static int update(String sql, Object... keys) { //通用的增,删,改 + int num=0; + try { + Connection conn = getConn(); + + PreparedStatement pre = conn.prepareStatement(sql); + + for (int i = 0; i < keys.length; i++) { + pre.setObject((i+1),keys[i]); + } + num = pre.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + return num; + } +} + +~~~ + + + +~~~ java +package servlet; + +import Bean.CarDatail; +import Bean.CarDatail; +import Dao.CarDao; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.util.ArrayList; + +@WebServlet("/car/*") +public class Servlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + String path = request.getPathInfo(); + + if (path==null || path.equals("/")){ + + ArrayList list= new CarDao().getAllCar(); + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/lib/Dab.jsp").forward(request,response); + } + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + String path = request.getPathInfo(); + + } +} + +~~~ + +~~~ java +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: cihan + Date: 2023/6/5 + Time: 19:43 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + + + + + + + + + + + + + + + + + + + + + + + + +
序号车辆名称所属品牌车辆简介价格录入时间
${car.cId}${car.cName}${car.BrandID}${car.content}${car.price}${car.lTime}
+ + + +~~~ + + + +~~~ java +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: cihan + Date: 2023/6/5 + Time: 21:13 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +
+ + + + + + + + + + + + + + + + + + + + + + + +
车辆名称
所属品牌 + +
车辆简介
价格
录入时间
+
+ + \ No newline at end of file -- Gitee