diff --git "a/03 \345\276\220\351\233\250\346\231\264/20230529 JSP\345\205\245\351\227\250\342\200\224\342\200\224\351\241\265\351\235\242\347\232\204\345\242\236\345\210\240\346\224\271\346\237\245.md" "b/03 \345\276\220\351\233\250\346\231\264/20230529 JSP\345\205\245\351\227\250\342\200\224\342\200\224\351\241\265\351\235\242\347\232\204\345\242\236\345\210\240\346\224\271\346\237\245.md" new file mode 100644 index 0000000000000000000000000000000000000000..6cdf499957a38cc8dab0c1a99246b4f5c371c081 --- /dev/null +++ "b/03 \345\276\220\351\233\250\346\231\264/20230529 JSP\345\205\245\351\227\250\342\200\224\342\200\224\351\241\265\351\235\242\347\232\204\345\242\236\345\210\240\346\224\271\346\237\245.md" @@ -0,0 +1,96 @@ +```java +create database DB_brand charset utf8; +use db_brand; +create table brands( + id int, + name varchar(15), + company varchar(30), + description varchar(100), + status int not null default 1 comment '状态:0禁用1启用' +); +insert into brands values +(1,'三只松鼠','北京二只松鼠科技有限公司','二只松鼠是中国领先的休闲零食品牌',1), +(2,'百草味','上海百草味零食有限公司','百草味是一家致力于绿色、天然、健康、美味的特产零食品牌',1), +(3,'米伊价','湖南米伊价食品股份有限公司','米伊价是中国知名粮食制品加工商之一',1); +``` + +```java +package dao; + +import bean.Brand; +import utils.DBUtil; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +public class BrandDao { + //查询所有品牌 + public List getAllBrand() throws SQLException { + ArrayList list = new ArrayList<>(); + String sql="selec t* from brands"; + ResultSet rst = DBUtil.query(sql); + try { + while (rst.next()) { + int id = rst.getInt("id"); + String name = rst.getString("name"); + String company = rst.getString("company"); + String description = rst.getString("description"); + int status = rst.getInt("status"); + + Brand brand = new Brand(id, name, company, description, status); + list.add(brand); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + try { + DBUtil.close(null,null,rst); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return list; + } + //按ID查询品牌 + public Brand getABrandById(int id) throws SQLException { + Brand brand=null; + String sql = "select * from brands where id=?"; + ResultSet rst = DBUtil.query(sql,id); + try { + while (rst.next()) { + String name = rst.getString("name"); + String company = rst.getString("company"); + String description = rst.getString("description"); + int status = rst.getInt("status"); + brand=new Brand(id, name, company, description, status); + } + } catch (SQLException e) { + e.printStackTrace(); + } finally { + try { + DBUtil.close(null,null,rst); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return brand; + } + //添加新品牌 + public int addBrand(Brand brand){ + String sql = "insert into brands (name,company,description) values (?,?,?)"; + int i=0; + try { + i = DBUtil.update(sql, brand.getName(), brand.getCompany(), brand.getDescription()); + } catch (SQLException e) { + e.printStackTrace(); } + return i; + } + //修改某品牌 + + //按ID删除 +} + +``` + diff --git "a/03 \345\276\220\351\233\250\346\231\264/20230603 JSP\346\265\213\350\257\225\342\200\224\342\200\224\350\275\246\350\276\206\344\277\241\346\201\257\347\256\241\347\220\206.md" "b/03 \345\276\220\351\233\250\346\231\264/20230603 JSP\346\265\213\350\257\225\342\200\224\342\200\224\350\275\246\350\276\206\344\277\241\346\201\257\347\256\241\347\220\206.md" new file mode 100644 index 0000000000000000000000000000000000000000..906cb43f10a7360a6c4d8bd6bdd9556038d56f86 --- /dev/null +++ "b/03 \345\276\220\351\233\250\346\231\264/20230603 JSP\346\265\213\350\257\225\342\200\224\342\200\224\350\275\246\350\276\206\344\277\241\346\201\257\347\256\241\347\220\206.md" @@ -0,0 +1,515 @@ +MySQL建库建表 + +```mysql +create database CarInfo charset utf8; + +use CarInfo; + +create table Brand( +BrandID int primary key auto_increment, +BrandName varchar(50) +); + +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 Brand values (0,'奔驰C200'),(0,'奥迪A4L'),(0,'劳斯莱斯888'); + +insert into CarDetail values (0,'奔驰','豪车','2023.06.04',300000,1),(0,'奥迪','适合年轻人的一款车','2023.06.04',410000,2),(0,'劳斯莱斯','裕铭银行时宴的专属座驾','2023.06.04',5000000,3); +``` + +JSP网页设计 + +```java +package bean; + +public class Brand { + private int brandId; + private 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 javax.xml.crypto.Data; +import java.util.Date; + +public class Car { + private int cid; + private String cname; + private String content; + private Date ltime; + private int price; + private int brandId; + private String 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) { + this.brandId = brandId; +} + +public String getBrandName() { + return brandName; +} + +public void setBrandName(String brandName) { + this.brandName = brandName; +} + +public Car() { +} + +public Car(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; + this.brandId = brandId; + this.brandName = brandName; +} +} +``` + +```java + package servlet; + +import bean.Brand; +import bean.Car; +import utils.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.*; +import java.util.ArrayList; + +@WebServlet("/add") +public class Add extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String sql = "select * from Brand"; + ArrayList list = new ArrayList<>(); + Connection conn=null; + PreparedStatement pst = null; + ResultSet rst = null; + try { + conn = DBUtil.getConn(); + pst = conn.prepareStatement(sql); + rst = pst.executeQuery(); + while (rst.next()) { + int id = rst.getInt("brandId"); + String name = rst.getString("brandName"); + Brand brand = new Brand(id, name); + list.add(brand); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + DBUtil.close(conn,pst,rst); + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/form.jsp").forward(request,response); +} + +@Override +protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + +} +} +``` + +```java + package servlet; + +import bean.Car; +import utils.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.*; +import java.util.ArrayList; +import java.util.Collection; + +@WebServlet("/list") +public class List extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String sql = "select * from cardetail c,brand b where b.BrandID=c.BrandID"; + ArrayList list = new ArrayList<>(); + Connection conn=null; + PreparedStatement pst = null; + ResultSet rst = null; + try { + conn = DBUtil.getConn(); + pst = conn.prepareStatement(sql); + rst = pst.executeQuery(); + while (rst.next()) { + int id = rst.getInt("cid"); + String name = rst.getString("cname"); + String content = rst.getString("content"); + Date date = rst.getDate("ltime"); + int price = rst.getInt("price"); + String brandName = rst.getString("brandName"); package servlet; + +import bean.Car; +import utils.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.*; +import java.util.ArrayList; +import java.util.Collection; + +@WebServlet("/list") +public class List extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String sql = "select * from cardetail c,brand b where b.BrandID=c.BrandID"; + ArrayList list = new ArrayList<>(); + Connection conn=null; + PreparedStatement pst = null; + ResultSet rst = null; + try { + conn = DBUtil.getConn(); + pst = conn.prepareStatement(sql); + rst = pst.executeQuery(); + while (rst.next()) { + int id = rst.getInt("cid"); + String name = rst.getString("cname"); + String content = rst.getString("content"); + Date date = rst.getDate("ltime"); + int price = rst.getInt("price"); + String brandName = rst.getString("brandName"); + Car car = new Car(id, name, content, date, price, 0, brandName); + list.add(car); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + DBUtil.close(conn,pst,rst); + } + request.setAttribute("list",list); + request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response); +} + +@Override +protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + +} +} +``` + +```java +package servlet; + +import utils.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.Connection; +import java.sql.Date; +import java.sql.PreparedStatement; +import java.sql.SQLException; + +@WebServlet("/save") +public class Save extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + +} + +@Override +protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + 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("ltime"); + System.out.println(date); + + String sql = "insert into cardetail values (?,?,?,?,?,?)"; + int i = 0; + Connection conn = null; + PreparedStatement pst = null; + try { + conn = DBUtil.getConn(); + pst = conn.prepareStatement(sql); + pst.setInt(1, 0); + pst.setString(2, name); + pst.setString(3, content); + pst.setDate(4, Date.valueOf(date)); + pst.setInt(5, price); + pst.setInt(6, brandId); + i = pst.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + DBUtil.close(conn, pst, null); + } + if (i > 0) { + response.sendRedirect("/list"); + }else{ + request.setAttribute("msg","添加失败!"); + request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,response); + } +} +} +``` + +```java +package utils; + +import java.sql.*; + +public class DBUtil { + private static final String url="jdbc:mysql:///carInfo?characterEncoding=utf8"; + private static final String user="root"; + private static final String pwd="root"; +static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } +} + +public static Connection getConn() throws SQLException { + Connection conn = DriverManager.getConnection(url,user,pwd); + return conn; +} + +// public static ResultSet query(String sql,Object ...keys) throws SQLException { +// Connection conn = getConn(); +// PreparedStatement pst = conn.prepareStatement(sql); +// for (int i = 0; i < keys.length; i++) { +// pst.setObject((i+1),keys[i]); +// } +// ResultSet rst = pst.executeQuery(); +// return rst; +// } +// public static int update(String sql, Object ...keys) throws SQLException { +// Connection conn = getConn(); +// PreparedStatement pst = conn.prepareStatement(sql); +// for (int i = 0; i < keys.length; i++) { +// pst.setObject((i+1),keys[i]); +// } +// int i = pst.executeUpdate(); +// return i; +// } + +public static void close(Connection conn,PreparedStatement pst,ResultSet rst){ + try { + if (rst != null){ + rst.close(); + } + if (pst != null) { + pst.close(); + } + if (conn != null) { + conn.close(); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } +} +} +``` + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-04 + Time: 20:18 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +
+ + + + + + + + + + + + + + + + + + + + + + + + +
车辆名称
所属品牌 + +
车辆简介
价格
录入时间
+
+ + +``` + +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-04 + Time: 19:48 + 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 +<%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-06-04 + Time: 20:58 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 友情提示 + + + ${msg} +
+返回列表 + + +``` +