64 lines
1.8 KiB
Java
64 lines
1.8 KiB
Java
package dev.dao;
|
|
|
|
import dev.database.DatabaseConnection;
|
|
import dev.model.OrderDetail;
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class OrderDetailDao {
|
|
|
|
public void save(OrderDetail detail) {
|
|
|
|
String sql = "INSERT INTO order_details (order_id, menu_item_id, quantity, item_price) VALUES (?, ?, ?, ?)";
|
|
|
|
try (Connection conn = DatabaseConnection.getConnection();
|
|
PreparedStatement ps = conn.prepareStatement(sql)) {
|
|
|
|
ps.setLong(1, detail.getOrderId());
|
|
ps.setLong(2, detail.getMenuItemId());
|
|
ps.setInt(3, detail.getQuantity());
|
|
ps.setDouble(4, detail.getPrice());
|
|
|
|
ps.executeUpdate();
|
|
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public List<OrderDetail> findByOrderId(int orderId) {
|
|
|
|
List<OrderDetail> list = new ArrayList<>();
|
|
|
|
String sql = "SELECT * FROM order_details WHERE order_id = ?";
|
|
|
|
try (Connection conn = DatabaseConnection.getConnection();
|
|
PreparedStatement ps = conn.prepareStatement(sql)) {
|
|
|
|
ps.setInt(1, orderId);
|
|
|
|
ResultSet rs = ps.executeQuery();
|
|
|
|
while (rs.next()) {
|
|
|
|
OrderDetail detail = new OrderDetail();
|
|
detail.setId(rs.getInt("id"));
|
|
detail.setOrderId(rs.getInt("order_id"));
|
|
detail.setMenuItemId(rs.getInt("menu_item_id"));
|
|
detail.setQuantity(rs.getInt("quantity"));
|
|
detail.setPrice(rs.getDouble("item_price"));
|
|
|
|
list.add(detail);
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
return list;
|
|
}
|
|
} |