package javaapplication48;
/**
* @author user
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.sql.*;
public class JavaApplication48 extends JFrame {
private JTextField empIdField, empNameField, basicPayField, hraField, daField;
private JTextArea resultArea;
private Connection connection;
public JavaApplication48() {
setTitle("Employee Payslip System");
setSize(700, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10);
empIdField = addLabelAndTextField("Employee ID:", 0, gbc);
empNameField = addLabelAndTextField("Employee Name:", 1, gbc);
basicPayField = addLabelAndTextField("Basic Pay:", 2, gbc);
hraField = addLabelAndTextField("HRA:", 3, gbc);
daField = addLabelAndTextField("DA:", 4, gbc);
JButton insertButton = new JButton("Insert Data");
gbc.gridx = 0; gbc.gridy = 5; add(insertButton, gbc);
JButton submitButton = new JButton("Fetch Payslip");
gbc.gridx = 1; add(submitButton, gbc);
resultArea = new JTextArea(10, 40);
resultArea.setEditable(false);
addComponentWithScrollPane(resultArea, 6, gbc);
connectDatabase();
insertButton.addActionListener(e -> insertEmployeeData());
submitButton.addActionListener(e -> fetchEmployeeDetails(empIdField.getText()));
private JTextField addLabelAndTextField(String labelText, int yPos, GridBagConstraints gbc) {
gbc.gridx = 0; gbc.gridy = yPos; add(new JLabel(labelText), gbc);
JTextField textField = new JTextField(20);
gbc.gridx = 1; add(textField, gbc);
return textField;
private void addComponentWithScrollPane(JComponent component, int yPos, GridBagConstraints
gbc) {
gbc.gridx = 0; gbc.gridy = yPos; gbc.gridwidth = 2;
JScrollPane scrollPane = new JScrollPane(component);
add(scrollPane, gbc);
}
private void connectDatabase() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
connection = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:XE", // Replace XE with your Oracle SID
"system", // Replace with your Oracle username
"manager" // Replace with your Oracle password
);
} catch (Exception e) {
showError("Database Connection Failed!");
private void insertEmployeeData() {
try {
double basicPay = Double.parseDouble(basicPayField.getText());
double hra = Double.parseDouble(hraField.getText());
double da = Double.parseDouble(daField.getText());
double grossPay = basicPay + hra + da;
double netPay = grossPay - (0.05 * grossPay);
String query = "INSERT INTO empdetails (emp_id, emp_name, bp, hra, da, grosspay, netpay)
VALUES (?, ?, ?, ?, ?, ?, ?)";
PreparedStatement statement = connection.prepareStatement(query);
statement.setString(1, empIdField.getText());
statement.setString(2, empNameField.getText());
statement.setDouble(3, basicPay);
statement.setDouble(4, hra);
statement.setDouble(5, da);
statement.setDouble(6, grossPay);
statement.setDouble(7, netPay);
if (statement.executeUpdate() > 0) {
JOptionPane.showMessageDialog(this, "Employee data inserted successfully!");
} catch (Exception e) {
showError("Error inserting data!");
private void fetchEmployeeDetails(String empId) {
try {
String query = "SELECT emp_name, bp, hra, da, grosspay, netpay FROM empdetails WHERE
emp_id = ?";
PreparedStatement statement = connection.prepareStatement(query);
statement.setString(1, empId);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
resultArea.setText(String.format("Employee ID: %s\nName: %s\nBasic Pay: %.2f\nHRA:
%.2f\nDA: %.2f\nGross Pay: %.2f\nNet Pay: %.2f",
empId, resultSet.getString("emp_name"), resultSet.getDouble("bp"),
resultSet.getDouble("hra"), resultSet.getDouble("da"),
resultSet.getDouble("grosspay"), resultSet.getDouble("netpay")));
} else {
resultArea.setText("Employee not found!");
} catch (SQLException e) {
showError("Error fetching data!");
}
private void showError(String message) {
JOptionPane.showMessageDialog(this, message, "Error", JOptionPane.ERROR_MESSAGE);
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new JavaApplication48().setVisible(true));
desc empdetails;
Name Null? Type
----------------------------------------- -------- ----------------------------
EMP_ID NOT NULL VARCHAR2(10)
EMP_NAME VARCHAR2(100)
BP NUMBER(10,2)
HRA NUMBER(10,2)
DA NUMBER(10,2)
GROSSPAY NUMBER(10,2)
NETPAY NUMBER(10,2)