0% found this document useful (0 votes)
17 views13 pages

Pharmacy 2

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)
17 views13 pages

Pharmacy 2

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/ 13

import javax.swing.

*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class PharmacySystem


extends JFrame {
private JTextField nameField,
quantityField, priceField;

public PharmacySystem() {
setTitle("Pharmacy System");
setLayout(null);
setBounds(100, 100, 400,
400);
setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE);

// Components
JLabel nameLabel = new
JLabel("Medicine Name:");
nameLabel.setBounds(50, 30,
150, 30);
add(nameLabel);

nameField = new JTextField();


nameField.setBounds(200, 30,
150, 30);
add(nameField);

JLabel quantityLabel = new


JLabel("Quantity:");
quantityLabel.setBounds(50,
80, 150, 30);
add(quantityLabel);

quantityField = new
JTextField();
quantityField.setBounds(200,
80, 150, 30);
add(quantityField);

JLabel priceLabel = new


JLabel("Price:");
priceLabel.setBounds(50, 130,
150,
30);
add(priceLabel);

priceField = new JTextField();


priceField.setBounds(200, 130,
150, 30);
add(priceField);

JButton addButton = new


JButton("Add Medicine");
addButton.setBounds(50, 180,
300, 30);
add(addButton);

// Action Listener for button

addButton.addActionListener(new
ActionListener() {
@Override
public void
actionPerformed(ActionEvent e) {
addMedicine();
}
});

setVisible(true);
}

private void addMedicine() {


String name =
nameField.getText();
String quantity =
quantityField.getText();
String price =
priceField.getText();

// Database connection
String url =
"jdbc:sqlite:pharmacy.db";
String sql = "INSERT INTO
medicines(name, quantity, price)
VALUES(?, ?, ?)";

try (Connection conn =


DriverManager.getConnection(url);
PreparedStatement pstmt =
conn.prepareStatement(sql)) {
pstmt.setString(1, name);
pstmt.setInt(2,
Integer.parseInt(quantity));
pstmt.setDouble(3,
Double.parseDouble(price));
pstmt.executeUpdate();

JOptionPane.showMessageDialog(t
his, "Medicine added
successfully!");

// Clear fields after adding


nameField.setText("");
quantityField.setText("");
priceField.setText("");
} catch (SQLException |
NumberFormatException ex) {
JOptionPane.showMessageDialog(t
his, "Error: " + ex.getMessage());
}
}
public static void main(String[]
args) {
// Create table if not exists
String url =
"jdbc:sqlite:pharmacy.db";
String sql = "CREATE TABLE
IF NOT EXISTS medicines (" +
"id INTEGER
PRIMARY KEY
AUTOINCREMENT," +
"name TEXT NOT
NULL," +
"quantity INTEGER
NOT NULL," +
"price REAL NOT
NULL)";

try (Connection conn =


DriverManager.getConnection(url);
PreparedStatement pstmt =
conn.prepareStatement(sql)) {
pstmt.executeUpdate();
} catch (SQLException e) {

System.out.println(e.getMessage());
}

new PharmacySystem();
}
}

You might also like