From 717dddbeba9d1a31e488118833c295e0c0726470 Mon Sep 17 00:00:00 2001 From: nxist2202005025 <1970759842@qq.com> Date: Tue, 11 Jun 2024 09:38:16 +0800 Subject: [PATCH] ADD file via upload --- dishDAOimp.java | 98 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 dishDAOimp.java diff --git a/dishDAOimp.java b/dishDAOimp.java new file mode 100644 index 0000000..e781345 --- /dev/null +++ b/dishDAOimp.java @@ -0,0 +1,98 @@ +package imp; + +import DAO.dishDAO; +import model.dish; +import java.sql.*; +import java.util.ArrayList; +import java.util.List; + +public class dishDAOimp implements dishDAO { + + @Override + public void insert(dish dish) { + String sql = "INSERT INTO dishes (dishID, dishName, description, price, image) VALUES (?, ?, ?, ?, ?)"; + try (Connection conn = JDBCutil.getConnection(); + PreparedStatement pstmt = conn.prepareStatement(sql)) { + pstmt.setInt(1, dish.getDishID()); + pstmt.setString(2, dish.getDishName()); + pstmt.setString(3, dish.getDescription()); + pstmt.setInt(4, dish.getPrice()); + pstmt.setString(5, dish.getImage()); + pstmt.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + @Override + public void delete(dish dish) { + String sql = "DELETE FROM dishes WHERE dishID = ?"; + try (Connection conn = JDBCutil.getConnection(); + PreparedStatement pstmt = conn.prepareStatement(sql)) { + pstmt.setInt(1, dish.getDishID()); + pstmt.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + @Override + public void update(dish dish) { + String sql = "UPDATE dishes SET dishName = ?, description = ?, price = ?, image = ? WHERE dishID = ?"; + try (Connection conn = JDBCutil.getConnection(); + PreparedStatement pstmt = conn.prepareStatement(sql)) { + pstmt.setString(1, dish.getDishName()); + pstmt.setString(2, dish.getDescription()); + pstmt.setInt(3, dish.getPrice()); + pstmt.setString(4, dish.getImage()); + pstmt.setInt(5, dish.getDishID()); + pstmt.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + @Override + public dish findById(int id) { + String sql = "SELECT * FROM dishes WHERE dishID = ?"; + try (Connection conn = JDBCutil.getConnection(); + PreparedStatement pstmt = conn.prepareStatement(sql)) { + pstmt.setInt(1, id); + ResultSet rs = pstmt.executeQuery(); + if (rs.next()) { + dish newDish = new dish(); + newDish.setDishID(rs.getInt("dishID")); + newDish.setDishName(rs.getString("dishName")); + newDish.setDescription(rs.getString("description")); + newDish.setPrice(rs.getInt("price")); + newDish.setImage(rs.getString("image")); + return newDish; + } + } catch (SQLException e) { + e.printStackTrace(); + } + return null; + } + + @Override + public List findAll() { + String sql = "SELECT * FROM dishes"; + List dishes = new ArrayList<>(); + try (Connection conn = JDBCutil.getConnection(); + Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery(sql)) { + while (rs.next()) { + dish newDish = new dish(); + newDish.setDishID(rs.getInt("dishID")); + newDish.setDishName(rs.getString("dishName")); + newDish.setDescription(rs.getString("description")); + newDish.setPrice(rs.getInt("price")); + newDish.setImage(rs.getString("image")); + dishes.add(newDish); + } + } catch (SQLException e) { + e.printStackTrace(); + } + return dishes; + } +} \ No newline at end of file