diff --git "a/40 \346\227\266\345\255\246\345\256\211/JDBC\344\275\234\344\270\2322.md" "b/40 \346\227\266\345\255\246\345\256\211/JDBC\344\275\234\344\270\2322.md" new file mode 100644 index 0000000000000000000000000000000000000000..9abc367a604d328124bf880433c98074343702a5 --- /dev/null +++ "b/40 \346\227\266\345\255\246\345\256\211/JDBC\344\275\234\344\270\2322.md" @@ -0,0 +1,440 @@ + + +```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 + '\'' + + '}'; +} +``` +} +```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 + '\'' + + '}'; + } +} +``` + +```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; + } +} +``` + +```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; + } +} +``` + +```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; + } +} +``` + +```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); + } + } +} +``` + +```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}
+ + +``` + +```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" %> + + + 添加 + + +
+ + + + + + + + + + + + + + + + + + + + + + + +
车辆名称
所属品牌 + +
车辆简介
价格
录入时间
+
+ + +``` + +```jsp +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 提示信息 + + +

${msg}

+ + \ No newline at end of file