Java Code:
import [Link].*;
import [Link].*;
import [Link];
import [Link];
import [Link].*;
public class EmployeeApp {
private JFrame frame;
private JTextField searchField, nameField, positionField, salaryField;
private JButton searchButton, updateButton;
private Connection conn;
public EmployeeApp() {
connectDatabase();
initializeUI();
}
private void connectDatabase() {
try {
conn =
[Link]("jdbc:mysql://localhost:3306/employeeapp", "root", "");
} catch (SQLException e) {
[Link](null, "Database Connection Failed!",
"Error", JOptionPane.ERROR_MESSAGE);
[Link]();
}
}
private void initializeUI() {
frame = new JFrame("Employee Management System");
[Link](400, 250);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](new BorderLayout());
JPanel panel = new JPanel();
[Link](new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
[Link] = new Insets(5, 5, 5, 5);
[Link] = [Link];
JLabel searchLabel = new JLabel("Search by ID:");
searchField = new JTextField(15);
searchButton = new JButton("Search");
JLabel nameLabel = new JLabel("Name:");
nameField = new JTextField(15);
JLabel positionLabel = new JLabel("Position:");
positionField = new JTextField(15);
JLabel salaryLabel = new JLabel("Salary:");
salaryField = new JTextField(15);
updateButton = new JButton("Update");
[Link] = 0;
[Link] = 0;
[Link](searchLabel, gbc);
[Link] = 1;
[Link](searchField, gbc);
[Link] = 2;
[Link](searchButton, gbc);
[Link] = 0;
[Link] = 1;
[Link](nameLabel, gbc);
[Link] = 1;
[Link] = 2;
[Link](nameField, gbc);
[Link] = 1;
[Link] = 0;
[Link] = 2;
[Link](positionLabel, gbc);
[Link] = 1;
[Link] = 2;
[Link](positionField, gbc);
[Link] = 1;
[Link] = 0;
[Link] = 3;
[Link](salaryLabel, gbc);
[Link] = 1;
[Link] = 2;
[Link](salaryField, gbc);
[Link] = 1;
[Link] = 1;
[Link] = 4;
[Link](updateButton, gbc);
[Link](panel, [Link]);
[Link](true);
[Link](new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
searchEmployee();
}
});
[Link](new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateEmployee();
}
});
}
private void searchEmployee() {
try {
String id = [Link]();
PreparedStatement stmt = [Link]("SELECT * FROM employee
WHERE id = ?");
[Link](1, id);
ResultSet rs = [Link]();
if ([Link]()) {
[Link]([Link]("name"));
[Link]([Link]("position"));
[Link]([Link]("salary"));
} else {
[Link](frame, "Employee not found!", "Error",
JOptionPane.ERROR_MESSAGE);
}
} catch (SQLException e) {
[Link]();
}
}
private void updateEmployee() {
try {
String id = [Link]();
String name = [Link]();
String position = [Link]();
String salary = [Link]();
PreparedStatement stmt = [Link]("UPDATE employee SET
name=?, position=?, salary=? WHERE id=?");
[Link](1, name);
[Link](2, position);
[Link](3, salary);
[Link](4, id);
int updated = [Link]();
if (updated > 0) {
[Link](frame, "Employee updated successfully!",
"Success", JOptionPane.INFORMATION_MESSAGE);
clearFields();
} else {
[Link](frame, "Update failed!", "Error",
JOptionPane.ERROR_MESSAGE);
}
} catch (SQLException e) {
[Link]();
}
}
private void clearFields() {
[Link]("");
[Link]("");
[Link]("");
[Link]("");
}
public static void main(String[] args) {
new EmployeeApp();
}
}
Database:
CREATE DATABASE employeeapp;
USE employeeapp;
CREATE TABLE employee (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100),
position VARCHAR(100),
salary DECIMAL(10,2)
);
INSERT INTO employee (name, position, salary) VALUES
('Shae Smith', 'Software Engineer', 70000.00),
('Third Lee', 'Project Manager', 90000.00),
('Lorn West', 'HR Manager', 80000.00);
Sample Output: