diff --git "a/21 \345\215\242\344\272\250\350\200\200/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/DBTool/DB.java" "b/21 \345\215\242\344\272\250\350\200\200/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/DBTool/DB.java" new file mode 100644 index 0000000000000000000000000000000000000000..3ee53f89d41b8ed79d9d77728694fcf9d2d5a377 --- /dev/null +++ "b/21 \345\215\242\344\272\250\350\200\200/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/DBTool/DB.java" @@ -0,0 +1,61 @@ +package DBTool; + +import java.sql.*; + +public class DB { + private static final String URL = "jdbc:mysql:///carinfo?useSSL=false&characterEncoding=utf-8"; + private static final String USER = "root"; + private static final String PSW = "root"; + + //加载驱动 + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + System.out.println("驱动加载异常"); + } + } + + //获取连接 + public static Connection getconn() { + Connection conn = null; + try { + conn = DriverManager.getConnection(URL, USER, PSW); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return conn; + } + + //获取数据库信息 + public static ResultSet query(String sql, Object... keys) { + Connection conn = getconn(); + ResultSet rs = null; + try { + 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) { + System.out.println("获取数据库数据失败"); + } + return rs; + } + + //修改数据库数据 + public static int update(String sql, Object... keys) { + Connection conn = getconn(); + int num = 0; + try { + 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) { + System.out.println("修改失败"); + } + return num; + } +} diff --git "a/21 \345\215\242\344\272\250\350\200\200/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/Dao/brandDao.java" "b/21 \345\215\242\344\272\250\350\200\200/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/Dao/brandDao.java" new file mode 100644 index 0000000000000000000000000000000000000000..bdc0c8c59e1e86761f96181f6eb0baf38949fc87 --- /dev/null +++ "b/21 \345\215\242\344\272\250\350\200\200/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/Dao/brandDao.java" @@ -0,0 +1,27 @@ +package Dao; + +import DBTool.DB; +import bean.Brand; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +public class brandDao { + public static ArrayList getbrand() { + String sql = "select * from brand"; + ResultSet rs = DB.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) { + throw new RuntimeException(e); + } + return list; + } +} diff --git "a/21 \345\215\242\344\272\250\350\200\200/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/Dao/selectDao.java" "b/21 \345\215\242\344\272\250\350\200\200/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/Dao/selectDao.java" new file mode 100644 index 0000000000000000000000000000000000000000..1de73dd6038e00b8045b6418f8f2a4ba558b0abd --- /dev/null +++ "b/21 \345\215\242\344\272\250\350\200\200/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/Dao/selectDao.java" @@ -0,0 +1,38 @@ +package Dao; + +import DBTool.DB; +import bean.Car; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Date; + +public class selectDao { + public static ArrayList getAllcar() { + ArrayList list = new ArrayList<>(); + String sql = "select * from brand b,cardetail c where b.BrandID=c.BrandID"; + ResultSet rs = DB.query(sql); + try { + while (rs.next()) { + int cid = rs.getInt(1); + String brandname = rs.getString(2); + String cname = rs.getString(4); + String content = rs.getString(5); + int price = rs.getInt(7); + Date time = rs.getDate(6); + Car car = new Car(cid, brandname, cname, content, price, time); + list.add(car); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + return list; + } + + public static int add(Car car) { + String sql = "insert into cardetail values(?,?,?,?,?,?)"; + int num = DB.update(sql, car.getcId(), car.getbName(), car.getcName(), car.getContent(), car.getPrice(), car.getTime()); + return num; + } +} diff --git "a/21 \345\215\242\344\272\250\350\200\200/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/bean/Brand.java" "b/21 \345\215\242\344\272\250\350\200\200/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/bean/Brand.java" new file mode 100644 index 0000000000000000000000000000000000000000..63499635caaacffaecd1aa3a4c6a48446af4205d --- /dev/null +++ "b/21 \345\215\242\344\272\250\350\200\200/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/bean/Brand.java" @@ -0,0 +1,38 @@ +package bean; + +public class Brand { + private int brandId; + private String bandName; + + public Brand() { + } + + public Brand(int brandId, String bandName) { + this.brandId = brandId; + this.bandName = bandName; + } + + public int getBrandId() { + return brandId; + } + + public void setBrandId(int brandId) { + this.brandId = brandId; + } + + public String getBandName() { + return bandName; + } + + public void setBandName(String bandName) { + this.bandName = bandName; + } + + @Override + public String toString() { + return "Brand{" + + "brandId=" + brandId + + ", bandName='" + bandName + '\'' + + '}'; + } +} diff --git "a/21 \345\215\242\344\272\250\350\200\200/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/bean/Car.java" "b/21 \345\215\242\344\272\250\350\200\200/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/bean/Car.java" new file mode 100644 index 0000000000000000000000000000000000000000..dacf929a8d0595a71ea9a1022c41ac8c243f5b22 --- /dev/null +++ "b/21 \345\215\242\344\272\250\350\200\200/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/bean/Car.java" @@ -0,0 +1,98 @@ +package bean; + +import com.mysql.fabric.xmlrpc.base.Data; + +import java.util.Date; + +public class Car { + private int cId; + private String bName; + private String cName; + private String content; + private int price; + private Date time; + private int brandId; + + + public Car() { + } + + public Car(int cId, String bName, String cName, String content, int price, Date time) { + this.cId = cId; + this.bName = bName; + this.cName = cName; + this.content = content; + this.price = price; + this.time = time; + } + + public Car(int cId, String cName, String bName, String content, int price, Date time, int brandId) { + this.cId = cId; + this.bName = bName; + this.cName = cName; + this.content = content; + this.price = price; + this.time = time; + this.brandId = brandId; + } + + public int getcId() { + return cId; + } + + public void setcId(int cId) { + this.cId = cId; + } + + public String getbName() { + return bName; + } + + public void setbName(String bName) { + this.bName = bName; + } + + 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 int getPrice() { + return price; + } + + public void setPrice(int price) { + this.price = price; + } + + public Date getTime() { + return time; + } + + public void setTime(Date time) { + this.time = time; + } + + @Override + public String toString() { + return "Car{" + + "cId=" + cId + + ", bName='" + bName + '\'' + + ", cName='" + cName + '\'' + + ", content='" + content + '\'' + + ", price=" + price + + ", time=" + time + + '}'; + } +} diff --git "a/21 \345\215\242\344\272\250\350\200\200/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/servlet/CarSer.java" "b/21 \345\215\242\344\272\250\350\200\200/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/servlet/CarSer.java" new file mode 100644 index 0000000000000000000000000000000000000000..053ce647be02044a8ca38e7cc9f3bcf58533e3c5 --- /dev/null +++ "b/21 \345\215\242\344\272\250\350\200\200/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/servlet/CarSer.java" @@ -0,0 +1,69 @@ +package servlet; + +import DBTool.DB; +import Dao.brandDao; +import Dao.selectDao; +import bean.Brand; +import bean.Car; + +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.sql.ResultSet; +import java.sql.SQLException; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.logging.SimpleFormatter; + +@WebServlet("/car/*") +public class CarSer extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setCharacterEncoding("utf-8"); + resp.setContentType("text/html"); + String path = req.getPathInfo(); + if (path == null || path.equals("/")) { + ArrayList list = new selectDao().getAllcar(); + req.setAttribute("list", list); + req.getRequestDispatcher("/WEB-INF/getlist.jsp").forward(req, resp); + } else if (path.equals("/add")) { + ArrayList list = new brandDao().getbrand(); + req.setAttribute("list", list); + req.getRequestDispatcher("/WEB-INF/add.jsp").forward(req, resp); + } + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setCharacterEncoding("utf-8"); + resp.setContentType("text/html"); + String path = req.getPathInfo(); + if (path.equals("/save")) { + String name = req.getParameter("name"); + String brandid = req.getParameter("brandid"); + String content = req.getParameter("content"); + int price = Integer.parseInt(req.getParameter("price")); + String date = req.getParameter("date"); + SimpleDateFormat da = new SimpleDateFormat("yyyy-MM-dd"); + Date newdate = null; + try { + newdate = da.parse(date); + } catch (ParseException e) { + throw new RuntimeException(e); + } + + Car car = new Car(0, null, name, content, price, newdate); + selectDao selectDao = new selectDao(); + int i = selectDao.add(car); + System.out.println(i); + if (i > 0) { + resp.sendRedirect("/car"); + } + } + } +}