Employee App
Employee App
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public EmployeeApp() {
connectDatabase();
initializeUI();
}
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(searchLabel, gbc);
gbc.gridx = 1;
panel.add(searchField, gbc);
gbc.gridx = 2;
panel.add(searchButton, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
panel.add(nameLabel, gbc);
gbc.gridx = 1;
gbc.gridwidth = 2;
panel.add(nameField, gbc);
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 2;
panel.add(positionLabel, gbc);
gbc.gridx = 1;
gbc.gridwidth = 2;
panel.add(positionField, gbc);
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 3;
panel.add(salaryLabel, gbc);
gbc.gridx = 1;
gbc.gridwidth = 2;
panel.add(salaryField, gbc);
gbc.gridwidth = 1;
gbc.gridx = 1;
gbc.gridy = 4;
panel.add(updateButton, gbc);
frame.add(panel, BorderLayout.CENTER);
frame.setVisible(true);
searchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
searchEmployee();
}
});
updateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateEmployee();
}
});
}
if (rs.next()) {
nameField.setText(rs.getString("name"));
positionField.setText(rs.getString("position"));
salaryField.setText(rs.getString("salary"));
} else {
JOptionPane.showMessageDialog(frame, "Employee not found!", "Error",
JOptionPane.ERROR_MESSAGE);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
Database:
Sample Output: