Bakker y
Bakker y
*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
// Components
private JLabel nameLabel;
private JTextField nameField;
private JTextArea orderDetails;
private JComboBox<String>
productDropdown;
private JCheckBox
deliveryCheckbox;
private JButton submitButton;
private JButton clearButton;
// MySQL Database connection
details
private final String DB_URL =
"jdbc:mysql://localhost:3306/bakery
_orders"; // Replace with your
database URL
private final String DB_USER =
"root"; // Replace with your MySQL
username
private final String
DB_PASSWORD = "yourpassword";
// Replace with your MySQL
password
public BakeryFrameWithDB() {
// Set Frame properties
setTitle("Bakery Ordering
System");
setBounds(100, 100, 500,
300);
setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE);
setLayout(new GridLayout(6,
2)); // Layout with 6 rows and 2
columns
// Initialize components
nameLabel = new
JLabel("Customer Name:");
nameField = new JTextField();
orderDetails = new
JTextArea("Order Details...");
productDropdown = new
JComboBox<>(new String[]
{"Bread", "Cake", "Pastry",
"Cookies"});
deliveryCheckbox = new
JCheckBox("Home Delivery");
submitButton = new
JButton("Submit Order");
clearButton = new
JButton("Clear Form");
clearButton.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent e) {
clearForm();
}
});
if (name.isEmpty() || product
== null || details.isEmpty()) {
JOptionPane.showMessageDialog(t
his, "Please fill all fields!");
return;
}
// Insert the order into the
database
try (Connection conn =
DriverManager.getConnection(DB_
URL, DB_USER,
DB_PASSWORD)) {
String query = "INSERT
INTO orders (customer_name,
product, details, home_delivery)
VALUES (?, ?, ?, ?)";
PreparedStatement pstmt =
conn.prepareStatement(query);
pstmt.setString(1, name);
pstmt.setString(2, product);
pstmt.setString(3, details);
pstmt.setBoolean(4,
homeDelivery);
pstmt.executeUpdate();
JOptionPane.showMessageDialog(t
his, "Order submitted
successfully!");
clearForm();