From 98e45d1a9c85c52f0b0a46eed97b6bb1bf350461 Mon Sep 17 00:00:00 2001 From: nxist2202005014 <1463859337@qq.com> Date: Wed, 26 Jun 2024 09:48:46 +0800 Subject: [PATCH] ADD file via upload --- FlowerDaoImpl.java | 257 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 257 insertions(+) create mode 100644 FlowerDaoImpl.java diff --git a/FlowerDaoImpl.java b/FlowerDaoImpl.java new file mode 100644 index 0000000..09f1685 --- /dev/null +++ b/FlowerDaoImpl.java @@ -0,0 +1,257 @@ +package flowershop.daoimpl; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import flowershop.dao.FlowerDao; +import flowershop.model.Flower; + +public class FlowerDaoImpl implements FlowerDao { + + //查询所有 + @Override + public List findAll() { + // TODO 自动生成的方法存根 + String sql = "select productid,name,category,image,price,descn from flower"; + + List list = new ArrayList(); + + try (// 2.创建数据库连接 + Connection conn = Jdbc.getConnection(); + // 3.创建语句对象 + PreparedStatement pstmt = conn.prepareStatement(sql); + // 4.绑定参数 + // 5.执行查询 + ResultSet rs = pstmt.executeQuery(sql)) { + + // 6.遍历结果集 + while (rs.next()) { + + Flower f = new Flower(); + + f.setProductid(rs.getString("productid")); + f.setName(rs.getString("name")); + f.setCategory(rs.getString("category")); + f.setImage(rs.getString("image")); + f.setPrice(rs.getDouble("price")); + f.setDescn(rs.getString("descn")); + + list.add(f); + + } + } catch (SQLException e) { + // TODO 自动生成的 catch 块 + e.printStackTrace(); + } + + return list; + + } + + //按主键查询用户 + @Override + public Flower findById(String productid) { + // TODO 自动生成的方法存根 + Connection conn = null; + PreparedStatement pstmt = null; + ResultSet rs = null; + + String sql = "select productid,name,category,image,price,descn from flower where productid=?"; + + try { + // 2.创建数据库连接 + conn = Jdbc.getConnection();// 1.加载驱动程序 + // 3.创建语句对象 + pstmt = conn.prepareStatement(sql); + // 4.绑定参数 + pstmt.setString(1, productid); + // 5.执行查询 + rs = pstmt.executeQuery(); + // 6.遍历按主键查询 + if (rs.next()) { + + Flower f = new Flower(); + + f.setProductid(rs.getString("productid")); + f.setName(rs.getString("name")); + f.setCategory(rs.getString("category")); + f.setImage(rs.getString("image")); + f.setPrice(rs.getDouble("price")); + f.setDescn(rs.getString("descn")); + + return f; + } + + } catch (SQLException e) { + e.printStackTrace(); + } finally { + if (rs != null) { + try { + rs.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + if (pstmt != null) { + try { + pstmt.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + if (conn != null) { + try { + conn.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + + + return null; + } + + //按名字查询 + @Override + public List findByCategory(String category) { + // TODO 自动生成的方法存根 + Connection conn = null; + PreparedStatement pstmt = null; + ResultSet rs = null; + + List list = new ArrayList(); + String sql = "select productid,name,category,image,price,descn from flower where category=?"; + + try { + // 2.创建数据库连接 + conn = Jdbc.getConnection();// 1.加载驱动程序 + // 3.创建语句对象 + pstmt = conn.prepareStatement(sql); + // 4.绑定参数 + pstmt.setString(1,category); + // 5.执行查询 + rs = pstmt.executeQuery(); + // 6.遍历按主键查询 + while (rs.next()) { + + Flower f = new Flower(); + + f.setProductid(rs.getString("productid")); + f.setName(rs.getString("name")); + f.setCategory(rs.getString("category")); + f.setImage(rs.getString("image")); + f.setPrice(rs.getDouble("price")); + f.setDescn(rs.getString("descn")); + + list.add(f); + } + + } catch (SQLException e) { + e.printStackTrace(); + } finally { + if (rs != null) { + try { + rs.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + if (pstmt != null) { + try { + pstmt.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + if (conn != null) { + try { + conn.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + return list; + + } + @Override + public int create(Flower flower) { + + String sql = "insert into flower(productid,name,category,image,price,descn) values (?,?,?,?,?,?)"; + + try ( // 2.创建数据库连接 + Connection conn = Jdbc.getConnection(); + // 3.创建语句对象 + PreparedStatement pstmt = conn.prepareStatement(sql);) { + // 4.绑定参数 + pstmt.setString(1, flower.getProductid()); + pstmt.setString(2, flower.getName()); + pstmt.setString(3, flower.getCategory()); + pstmt.setString(4, flower.getImage()); + pstmt.setDouble(5, flower.getPrice()); + pstmt.setString(6, flower.getDescn()); + + // 5.执行 + int executeUpdate = pstmt.executeUpdate(); + System.out.printf("成功插入%d数据\n", executeUpdate); + + } catch (Exception e) { + // TODO: handle exception + e.printStackTrace(); + } + + return 0; + } + + @Override + public int modify(Flower flower) { + + String sql = "update flower set price = ? where productid = ?"; + + try ( // 2.创建数据库连接 + Connection conn = Jdbc.getConnection(); + // 3. 创建语句对象 + PreparedStatement pstmt = conn.prepareStatement(sql)) { + // 4. 绑定参数 + + pstmt.setDouble(1, flower.getPrice()); + pstmt.setString(2, flower.getProductid()); + + // 5. 执行修改(C、U、D) + int executeUpdate = pstmt.executeUpdate(); + System.out.printf("成功更新%d条数据。\n", executeUpdate); + } catch (SQLException e) { + e.printStackTrace(); + } + + return 0; + } + + @Override + public int remove(Flower flower) { + + String sql = "delete from flower where productid = ?"; + + try ( // 2.创建数据库连接 + Connection conn = Jdbc.getConnection(); + // 3. 创建语句对象 + PreparedStatement pstmt = conn.prepareStatement(sql)) { + + // 4. 绑定参数 + pstmt.setString(1, flower.getProductid()); + + // 5. 执行修改(C、U、D) + int executeUpdate = pstmt.executeUpdate(); + System.out.printf("成功删除%d条数据。\n", executeUpdate); + } catch (SQLException e) { + e.printStackTrace(); + } + return 0; + } + + +} \ No newline at end of file