From 7d38fafe27d923efb2ff0efe1abfd313591f63e5 Mon Sep 17 00:00:00 2001 From: Gwendal Margely <gwendal.margely.etu@univ-lille.fr> Date: Thu, 14 Mar 2024 01:28:00 +0100 Subject: [PATCH] Add DAO Ingredients --- WEB-INF/src/DAO/DAOIngredients.java | 84 +++++++++++++++++++++++++++++ WEB-INF/src/POJO/Ingredient.java | 12 ++--- 2 files changed, 89 insertions(+), 7 deletions(-) diff --git a/WEB-INF/src/DAO/DAOIngredients.java b/WEB-INF/src/DAO/DAOIngredients.java index 78e7da1..2a42631 100644 --- a/WEB-INF/src/DAO/DAOIngredients.java +++ b/WEB-INF/src/DAO/DAOIngredients.java @@ -1,4 +1,88 @@ package DAO; +import POJO.Ingredient; + +import java.sql.*; +import java.util.ArrayList; +import java.util.List; public class DAOIngredients { + // JDBC URL, username, and password of PostgresSQL server + private static final String url = "jdbc:postgresql://"; //CHANGE URL + private static final String user = "gwendalmargelyetu"; + private static final String password = "moi"; + + // JDBC variables for opening, closing and managing connection + private static Connection connection; + private static PreparedStatement preparedStatement; + private static ResultSet resultSet; + + // SQL queries + private static final String SELECT_ALL_QUERY = "SELECT * FROM ingredients"; + private static final String INSERT_QUERY = "INSERT INTO ingredients(name, price) VALUES (?, ?)"; + private static final String SELECT_BY_ID_QUERY = "SELECT * FROM ingredients WHERE id = ?"; + + // Method to establish database connection + private static void connect() throws SQLException { + connection = DriverManager.getConnection(url, user, password); + } + + // Method to close database connection + private static void close() throws SQLException { + if (resultSet != null) { + resultSet.close(); + } + if (preparedStatement != null) { + preparedStatement.close(); + } + if (connection != null) { + connection.close(); + } + } + + // Method to retrieve all ingredients from the database + public static List<Ingredient> getAllIngredients() throws SQLException { + List<Ingredient> ingredients = new ArrayList<>(); + connect(); + preparedStatement = connection.prepareStatement(SELECT_ALL_QUERY); + resultSet = preparedStatement.executeQuery(); + + while (resultSet.next()) { + int id = resultSet.getInt("id"); + String name = resultSet.getString("name"); + double price = resultSet.getDouble("price"); + Ingredient ingredient = new Ingredient(id, name, price); + ingredients.add(ingredient); + } + + close(); + return ingredients; + } + + // Method to insert a new ingredient into the database + public static void insertIngredient(Ingredient ingredient) throws SQLException { + connect(); + preparedStatement = connection.prepareStatement(INSERT_QUERY); + preparedStatement.setString(1, ingredient.getName()); + preparedStatement.setDouble(2, ingredient.getPrice()); + preparedStatement.executeUpdate(); + close(); + } + + // Method to retrieve an ingredient by its ID + public static Ingredient getIngredient(int id) throws SQLException { + connect(); + preparedStatement = connection.prepareStatement(SELECT_BY_ID_QUERY); + preparedStatement.setInt(1, id); + resultSet = preparedStatement.executeQuery(); + + Ingredient ingredient = null; + if (resultSet.next()) { + String name = resultSet.getString("name"); + double price = resultSet.getDouble("price"); + ingredient = new Ingredient(id, name, price); + } + + close(); + return ingredient; + } } diff --git a/WEB-INF/src/POJO/Ingredient.java b/WEB-INF/src/POJO/Ingredient.java index ecfa59c..1202b98 100644 --- a/WEB-INF/src/POJO/Ingredient.java +++ b/WEB-INF/src/POJO/Ingredient.java @@ -3,18 +3,16 @@ package POJO; public class Ingredient { public int id; private String name; - private Price price; + private double price; - public Ingredient(int id, String name, Price price) { + public Ingredient(int id, String name, double price) { this.id = id; this.name = name; this.price = price; } public Ingredient(int id, String name) { - this.id = id; - this.name = name; - this.price = new Price(); + this(id, name, 0.00); } public int getId() { @@ -33,11 +31,11 @@ public class Ingredient { this.name = name; } - public Price getPrice() { + public double getPrice() { return price; } - public void setPrice(Price price) { + public void setPrice(double price) { this.price = price; } } -- GitLab