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;
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();
}
}
}
package Exceptions;
public class IngredientDAOException extends Exception {
public IngredientDAOException(String message, Throwable cause) {
super(message, cause);
}
}
......@@ -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;
......
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