import javax.swing.
*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.table.DefaultTableModel;
public final class Project extends JFrame {
DefaultTableModel model;
JTable table;
JTextField searchField;
public Project() {
setTitle("Student CRUD - Menu Based UI");
setSize(700, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
initMenu();
showHome(); // default screen
setVisible(true);
// Initialize menu bar
private void initMenu() {
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Task");
JMenuItem insertItem = new JMenuItem("Insert");
JMenuItem updateItem = new JMenuItem("Update");
JMenuItem deleteItem = new JMenuItem("Delete");
JMenu find = new JMenu("Find");
JMenuItem findItem = new JMenuItem("Search");
menu.add(insertItem);
menu.add(updateItem);
menu.add(deleteItem);
menuBar.add(menu);
menuBar.add(find);
find.add(findItem);
setJMenuBar(menuBar);
insertItem.addActionListener(e -> showInsertForm());
updateItem.addActionListener(e -> showUpdateForm());
deleteItem.addActionListener(e -> showDeleteForm());
findItem.addActionListener(e -> showFindForm());
// Show main screen
private void showHome() {
getContentPane().removeAll();
JLabel homeLabel = new JLabel("Select an action from the Menu.", SwingConstants.CENTER);
homeLabel.setFont(new Font("Arial", Font.BOLD, 18));
add(homeLabel, BorderLayout.CENTER);
revalidate(); repaint();
}
// Insert UI
private void showInsertForm() {
getContentPane().removeAll();
setLayout(null); // Using absolute positioning
// Create components
JLabel lblName = new JLabel("Full Name:");
JTextField tfName = new JTextField(10);
JLabel lblGender = new JLabel("Gender:");
JTextField tfGender = new JTextField(10);
JLabel lblAddress = new JLabel("Address:");
JTextField tfAddress = new JTextField(10);
JLabel lblPhone = new JLabel("Phone:");
JTextField tfPhone = new JTextField(10);
JButton insertBtn = new JButton("Insert");
// Set bounds (x, y, width, height)
lblName.setBounds(30, 30, 100, 25);
tfName.setBounds(150, 30, 150, 25);
lblGender.setBounds(30, 70, 100, 25);
tfGender.setBounds(150, 70, 150, 25);
lblAddress. setBounds(30, 110, 100, 25);
tfAddress.setBounds(150, 110, 150, 25);
lblPhone.setBounds(30, 150, 100, 25);
tfPhone.setBounds(150, 150, 150, 25);
insertBtn.setBounds(150, 190, 100, 30);
// Add components
add(lblName); add(tfName);
add(lblGender); add(tfGender);
add(lblAddress); add(tfAddress);
add(lblPhone); add(tfPhone);
add(insertBtn);
insertBtn.addActionListener(e -> {
try (Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", ""))
{
PreparedStatement ps = c.prepareStatement("INSERT INTO person (Full_name, Gender,
Address, Phone) VALUES (?, ?, ?, ?)");
ps.setString(1, tfName.getText());
ps.setString(2, tfGender.getText());
ps.setString(3, tfAddress.getText());
ps.setString(4, tfPhone.getText());
ps.executeUpdate();
JOptionPane.showMessageDialog(this, "Inserted successfully.");
showHome();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage());
});
revalidate(); repaint();
// Update UI
private void showUpdateForm() {
getContentPane().removeAll();
setLayout(null); // Required for setBounds()
// Create components
JLabel lblId = new JLabel("ID:");
JTextField tfId = new JTextField(10);
JLabel lblName = new JLabel("New Name:");
JTextField tfName = new JTextField(10);
JLabel lblGender = new JLabel("New Gender:");
JTextField tfGender = new JTextField(10);
JButton updateBtn = new JButton("Update");
// Set bounds (x, y, width, height)
lblId.setBounds(30, 30, 100, 25);
tfId.setBounds(150, 30, 150, 25);
lblName.setBounds(30, 70, 100, 25);
tfName.setBounds(150, 70, 150, 25);
lblGender.setBounds(30, 110, 100, 25);
tfGender.setBounds(150, 110, 150, 25);
updateBtn.setBounds(150, 150, 100, 30);
// Add components
add(lblId); add(tfId);
add(lblName); add(tfName);
add(lblGender); add(tfGender);
add(updateBtn);
updateBtn.addActionListener(e -> {
try (Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", ""))
{
PreparedStatement ps = c.prepareStatement("UPDATE person SET Full_name=?, Gender=?
WHERE ID=?");
ps.setString(1, tfName.getText());
ps.setString(2, tfGender.getText());
ps.setInt(3, Integer.parseInt(tfId.getText()));
int rows = ps.executeUpdate();
if (rows > 0) {
JOptionPane.showMessageDialog(this, "Updated successfully.");
showHome();
} else {
JOptionPane.showMessageDialog(this, "ID not found.");
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage());
}
});
revalidate(); repaint();
// Delete UI
private void showDeleteForm() {
getContentPane().removeAll();
setLayout(new FlowLayout());
JTextField tfId = new JTextField(10);
JButton deleteBtn = new JButton("Delete");
add(new JLabel("Enter ID to delete:")); add(tfId); add(deleteBtn);
deleteBtn.addActionListener(e -> {
try (Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", ""))
{
PreparedStatement ps = c.prepareStatement("DELETE FROM person WHERE ID=?");
ps.setInt(1, Integer.parseInt(tfId.getText()));
int rows = ps.executeUpdate();
if (rows > 0) {
JOptionPane.showMessageDialog(this, "Deleted successfully.");
showHome();
} else {
JOptionPane.showMessageDialog(this, "ID not found.");
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage());
}
});
revalidate(); repaint();
// Find/Search UI
private void showFindForm() {
getContentPane().removeAll();
setLayout(new BorderLayout());
JPanel searchPanel = new JPanel(new FlowLayout());
searchField = new JTextField(15);
searchPanel.add(new JLabel("Search by Gender:"));
searchPanel.add(searchField);
model = new DefaultTableModel();
table = new JTable(model);
model.addColumn("ID");
model.addColumn("Full Name");
model.addColumn("Gender");
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setPreferredSize(new Dimension(650, 300));
add(searchPanel, BorderLayout.NORTH);
add(scrollPane, BorderLayout.CENTER);
searchField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
performSearch(searchField.getText().trim());
});
revalidate(); repaint();
private void performSearch(String gender) {
model.setRowCount(0); // clear table
try (Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "")) {
PreparedStatement ps = c.prepareStatement("SELECT * FROM person WHERE Gender = ?");
ps.setString(1, gender);
ResultSet rs = ps.executeQuery();
boolean found = false;
while (rs.next()) {
model.addRow(new Object[]{
rs.getInt("ID"),
rs.getString("Full_name"),
rs.getString("Gender")
});
found = true;
if (!found) {
JOptionPane.showMessageDialog(this, "No records found.");
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage());
public class MyReport {
public static void main(String[] args) {
Project p1 = new Project();