0% found this document useful (0 votes)
8 views15 pages

Bakker y

Uploaded by

kenabadane9299
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views15 pages

Bakker y

Uploaded by

kenabadane9299
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

import javax.swing.

*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class BakeryFrameWithDB


extends JFrame {

// 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");

// Add action listeners for


buttons
submitButton.addActionListener(ne
w ActionListener() {
public void
actionPerformed(ActionEvent e) {
submitOrderToDatabase();
}
});

clearButton.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent e) {
clearForm();
}
});

// Add components to the


frame
add(nameLabel);
add(nameField);
add(new JLabel("Select
Product:")); // Label for product
dropdown
add(productDropdown);
add(new JLabel("Additional
Details:"));
add(orderDetails);
add(deliveryCheckbox);
add(new JLabel("")); // Empty
cell for layout spacing
add(submitButton);
add(clearButton);
}

// Method to submit order and


store it in the database
private void
submitOrderToDatabase() {
String name =
nameField.getText();
String product = (String)
productDropdown.getSelectedItem()
;
String details =
orderDetails.getText();
boolean homeDelivery =
deliveryCheckbox.isSelected();

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();

} catch (SQLException ex) {


ex.printStackTrace();
JOptionPane.showMessageDialog(t
his, "Error submitting order. Check
database connection.");
}
}

// Method to clear the form


private void clearForm() {
nameField.setText("");
orderDetails.setText("Order
Details...");
productDropdown.setSelectedIndex
(0);
deliveryCheckbox.setSelected(false
);
}
public static void main(String[]
args) {
// Run the frame
SwingUtilities.invokeLater(() ->
{
new
BakeryFrameWithDB().setVisible(tru
e);
});
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy