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

Add DAO Ingredients

parent 818daa88
No related branches found
No related tags found
No related merge requests found
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;
}
}
......@@ -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;
}
}
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