57 lines
1.9 KiB
Java
57 lines
1.9 KiB
Java
package dev.dao;
|
|
|
|
import dev.database.DatabaseConnection;
|
|
import dev.model.MenuItem;
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class MenuItemDao {
|
|
|
|
public List<MenuItem> findAll() {
|
|
List<MenuItem> items = new ArrayList<>();
|
|
String sql = "SELECT * FROM menu_items";
|
|
try (Connection conn = DatabaseConnection.getConnection();
|
|
PreparedStatement stmt = conn.prepareStatement(sql);
|
|
ResultSet rs = stmt.executeQuery()) {
|
|
while (rs.next()) {
|
|
MenuItem item = new MenuItem();
|
|
item.setId(rs.getInt("id"));
|
|
item.setName(rs.getString("name"));
|
|
item.setDescription(rs.getString("description"));
|
|
item.setPrice(rs.getDouble("price"));
|
|
item.setCategory(rs.getString("category"));
|
|
items.add(item);
|
|
}
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return items;
|
|
}
|
|
|
|
public MenuItem findById(int id) {
|
|
String sql = "SELECT * FROM menu_items WHERE id = ?";
|
|
try (Connection conn = DatabaseConnection.getConnection();
|
|
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
|
stmt.setInt(1, id);
|
|
try (ResultSet rs = stmt.executeQuery()) {
|
|
if (rs.next()) {
|
|
MenuItem item = new MenuItem();
|
|
item.setId(rs.getInt("id"));
|
|
item.setName(rs.getString("name"));
|
|
item.setDescription(rs.getString("description"));
|
|
item.setPrice(rs.getDouble("price"));
|
|
item.setCategory(rs.getString("category"));
|
|
return item;
|
|
}
|
|
}
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return null;
|
|
}
|
|
} |