diff --git a/WEB-INF/src/DAO/IngredientDAO.java b/WEB-INF/src/DAO/IngredientDAO.java index c7008956acda4fbbb83b32c601e5bc80598018c7..db01497930a0234627806c6d16e931c17c6523be 100644 --- a/WEB-INF/src/DAO/IngredientDAO.java +++ b/WEB-INF/src/DAO/IngredientDAO.java @@ -1,4 +1,5 @@ package DAO; +import Exceptions.IngredientDAOException; import POJO.Ingredient; import java.sql.*; @@ -20,79 +21,105 @@ public class IngredientDAO { 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 = ?"; + private static final String DELETE_QUERY = "DELETE FROM ingredients WHERE id = ?"; // Method to establish database connection - private static void connect() throws SQLException { - connection = DriverManager.getConnection(url, user, password); + // Method to establish database connection + private static void connect() throws IngredientDAOException { + try { + connection = DriverManager.getConnection(url, user, password); + } catch (SQLException e) { + throw new IngredientDAOException("Error establishing database connection", e); + } } // 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(); + private static void close() throws IngredientDAOException { + try { + if (resultSet != null) { + resultSet.close(); + } + if (preparedStatement != null) { + preparedStatement.close(); + } + if (connection != null) { + connection.close(); + } + } catch (SQLException e) { + throw new IngredientDAOException("Error closing database resources", e); } } // Method to retrieve all ingredients from the database - public static List<Ingredient> getAllIngredients() throws SQLException { + public List<Ingredient> getAllIngredients() throws IngredientDAOException { List<Ingredient> ingredients = new ArrayList<>(); - connect(); - preparedStatement = connection.prepareStatement(SELECT_ALL_QUERY); - resultSet = preparedStatement.executeQuery(); + try { + 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); + 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); + } + } catch (SQLException e) { + throw new IngredientDAOException("Error fetching ingredients", e); + } finally { + close(); } - - 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(); + public static void insertIngredient(Ingredient ingredient) throws IngredientDAOException { + try { + connect(); + preparedStatement = connection.prepareStatement(INSERT_QUERY); + preparedStatement.setString(1, ingredient.getName()); + preparedStatement.setDouble(2, ingredient.getPrice()); + preparedStatement.executeUpdate(); + } catch (SQLException e) { + throw new IngredientDAOException("Error inserting ingredient", e); + } finally { + 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(); - + public static Ingredient getIngredient(int id) throws IngredientDAOException { Ingredient ingredient = null; - if (resultSet.next()) { - String name = resultSet.getString("name"); - double price = resultSet.getDouble("price"); - ingredient = new Ingredient(id, name, price); - } + try { + connect(); + preparedStatement = connection.prepareStatement(SELECT_BY_ID_QUERY); + preparedStatement.setInt(1, id); + resultSet = preparedStatement.executeQuery(); - close(); + if (resultSet.next()) { + String name = resultSet.getString("name"); + double price = resultSet.getDouble("price"); + ingredient = new Ingredient(id, name, price); + } + } catch (SQLException e) { + throw new IngredientDAOException("Error fetching ingredient", e); + } finally { + close(); + } return ingredient; } - public void deleteIngredient(int id) throws SQLException { - String deleteQuery = "DELETE FROM ingredients WHERE id = ?"; - try (Connection connection = DriverManager.getConnection(url, user, password); - PreparedStatement statement = connection.prepareStatement(deleteQuery)) { - statement.setInt(1, id); - statement.executeUpdate(); + public void deleteIngredient(int id) throws IngredientDAOException { + try { + connect(); + preparedStatement = connection.prepareStatement(DELETE_QUERY); + preparedStatement.setInt(1, id); + preparedStatement.executeUpdate(); + } catch (SQLException e) { + throw new IngredientDAOException("Error deleting ingredient", e); + } finally { + close(); } } - } diff --git a/WEB-INF/src/Exceptions/IngredientDAOException.java b/WEB-INF/src/Exceptions/IngredientDAOException.java new file mode 100644 index 0000000000000000000000000000000000000000..e2e37a2462845dff477e1b955b53be9d2bbbc85f --- /dev/null +++ b/WEB-INF/src/Exceptions/IngredientDAOException.java @@ -0,0 +1,7 @@ +package Exceptions; + +public class IngredientDAOException extends Exception { + public IngredientDAOException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/WEB-INF/src/Servlets/IngredientServlet.java b/WEB-INF/src/Servlets/IngredientServlet.java index 0df5ab9a9ec8ce64c0e239bdaf83b4e58db793a8..30d81ee97bf1e513e5506083f67f44535418af12 100644 --- a/WEB-INF/src/Servlets/IngredientServlet.java +++ b/WEB-INF/src/Servlets/IngredientServlet.java @@ -4,7 +4,7 @@ package Servlets; import DAO.IngredientDAO; import POJO.Ingredient; -import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.*; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet;