Skip to content
Snippets Groups Projects
Commit 9b98cc85 authored by Gwendal Margely's avatar Gwendal Margely :alembic:
Browse files

Improvement on Exceptions on DAO

parent 4dd9cf40
No related branches found
No related tags found
No related merge requests found
package DAO; package DAO;
import Exceptions.IngredientDAOException;
import POJO.Ingredient; import POJO.Ingredient;
import java.sql.*; import java.sql.*;
...@@ -20,79 +21,105 @@ public class IngredientDAO { ...@@ -20,79 +21,105 @@ public class IngredientDAO {
private static final String SELECT_ALL_QUERY = "SELECT * FROM ingredients"; 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 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 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 // Method to establish database connection
private static void connect() throws SQLException { // Method to establish database connection
connection = DriverManager.getConnection(url, user, password); 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 // Method to close database connection
private static void close() throws SQLException { private static void close() throws IngredientDAOException {
if (resultSet != null) { try {
resultSet.close(); if (resultSet != null) {
} resultSet.close();
if (preparedStatement != null) { }
preparedStatement.close(); if (preparedStatement != null) {
} preparedStatement.close();
if (connection != null) { }
connection.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 // 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<>(); List<Ingredient> ingredients = new ArrayList<>();
connect(); try {
preparedStatement = connection.prepareStatement(SELECT_ALL_QUERY); connect();
resultSet = preparedStatement.executeQuery(); preparedStatement = connection.prepareStatement(SELECT_ALL_QUERY);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {
int id = resultSet.getInt("id"); int id = resultSet.getInt("id");
String name = resultSet.getString("name"); String name = resultSet.getString("name");
double price = resultSet.getDouble("price"); double price = resultSet.getDouble("price");
Ingredient ingredient = new Ingredient(id, name, price); Ingredient ingredient = new Ingredient(id, name, price);
ingredients.add(ingredient); ingredients.add(ingredient);
}
} catch (SQLException e) {
throw new IngredientDAOException("Error fetching ingredients", e);
} finally {
close();
} }
close();
return ingredients; return ingredients;
} }
// Method to insert a new ingredient into the database // Method to insert a new ingredient into the database
public static void insertIngredient(Ingredient ingredient) throws SQLException { public static void insertIngredient(Ingredient ingredient) throws IngredientDAOException {
connect(); try {
preparedStatement = connection.prepareStatement(INSERT_QUERY); connect();
preparedStatement.setString(1, ingredient.getName()); preparedStatement = connection.prepareStatement(INSERT_QUERY);
preparedStatement.setDouble(2, ingredient.getPrice()); preparedStatement.setString(1, ingredient.getName());
preparedStatement.executeUpdate(); preparedStatement.setDouble(2, ingredient.getPrice());
close(); preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new IngredientDAOException("Error inserting ingredient", e);
} finally {
close();
}
} }
// Method to retrieve an ingredient by its ID // Method to retrieve an ingredient by its ID
public static Ingredient getIngredient(int id) throws SQLException { public static Ingredient getIngredient(int id) throws IngredientDAOException {
connect();
preparedStatement = connection.prepareStatement(SELECT_BY_ID_QUERY);
preparedStatement.setInt(1, id);
resultSet = preparedStatement.executeQuery();
Ingredient ingredient = null; Ingredient ingredient = null;
if (resultSet.next()) { try {
String name = resultSet.getString("name"); connect();
double price = resultSet.getDouble("price"); preparedStatement = connection.prepareStatement(SELECT_BY_ID_QUERY);
ingredient = new Ingredient(id, name, price); 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; return ingredient;
} }
public void deleteIngredient(int id) throws SQLException { public void deleteIngredient(int id) throws IngredientDAOException {
String deleteQuery = "DELETE FROM ingredients WHERE id = ?"; try {
try (Connection connection = DriverManager.getConnection(url, user, password); connect();
PreparedStatement statement = connection.prepareStatement(deleteQuery)) { preparedStatement = connection.prepareStatement(DELETE_QUERY);
statement.setInt(1, id); preparedStatement.setInt(1, id);
statement.executeUpdate(); preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new IngredientDAOException("Error deleting ingredient", e);
} finally {
close();
} }
} }
} }
package Exceptions;
public class IngredientDAOException extends Exception {
public IngredientDAOException(String message, Throwable cause) {
super(message, cause);
}
}
...@@ -4,7 +4,7 @@ package Servlets; ...@@ -4,7 +4,7 @@ package Servlets;
import DAO.IngredientDAO; import DAO.IngredientDAO;
import POJO.Ingredient; import POJO.Ingredient;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.*;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet; import javax.servlet.annotation.WebServlet;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment