John_358 0 Newbie Poster
import net.proteanit.sql.DbUtils;

import javax.swing.*;
import javax.xml.transform.Result;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class EmployeeRegistriation {
    private JPanel Main;
    private JTextField txtName;
    private JTextField txtSalary;
    private JTextField txtMobile;
    private JButton saveButton;
    private JTable table1;
    private JButton updateButton;
    private JButton deleteButton;
    private JButton searchButton;
    private JTextField txtId;
    private JScrollPane tabla_1;

    public static void main(String[] args) {
        JFrame frame = new JFrame("EmployeeRegistriation");
        frame.setContentPane(new EmployeeRegistriation().Main);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
    Connection con;
    PreparedStatement pst;

     public void connect()
    {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost/johnrichescompany", "root", "");


            System.out.println("Success");


        }
        catch (ClassNotFoundException ex)
        {
            ex.printStackTrace();

        }
        catch (SQLException ex)
        {
            ex.printStackTrace();
        }
    }

    private void forName(String s) {
    }


    public EmployeeRegistriation() {
        connect();
        table_load();

        saveButton.addActionListener(new ActionListener() {

            /**
             * Invoked when an action occurs.
             *
             * @param e the event to be processed
             */
            @Override
            public void actionPerformed(ActionEvent e) {

                String empname, salary, mobile;


                empname = txtName.getText();
                salary = txtSalary.getText();
                mobile = txtMobile.getText();


                try {
                    pst = con.prepareStatement("insert into employee_registration(empname,salary,mobile)values(?,?,?)");
                    pst.setString(1, empname);
                    pst.setString(2, salary);
                    pst.setString(3, mobile);
                    pst.executeUpdate();
                    JOptionPane.showMessageDialog(null, "Record Added!!!");
                    table_load();
                    txtName.setText("");
                    txtSalary.setText("");
                    txtMobile.setText("");
                    txtName.requestFocus();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }

            }
        });
        searchButton.addActionListener(new ActionListener() {
            /**
             * Invoked when an action occurs.
             *
             * @param e the event to be processed
             */
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    String employeeid = txtId.getText();

                    pst = con.prepareStatement("select empname, salary, mobile from employee_registration where employeeid = ?");
                    pst.setString(1, employeeid);
                    ResultSet rs = pst.executeQuery();

                    if (rs.next() == true) {
                        String empname = rs.getString(1);
                        String salary = rs.getString(2);
                        String mobile = rs.getString(3);


                        txtName.setText(empname);
                        txtSalary.setText(salary);
                        txtMobile.setText(mobile);


                    } else {
                        txtName.setText("");
                        txtSalary.setText("");
                        txtMobile.setText("");
                        JOptionPane.showMessageDialog(null, "Invalid Employee Id");
                    }
                } catch (SQLException ex) {
                }
            }
        });
        updateButton.addActionListener(new ActionListener() {
            /**
             * Invoked when an action occurs.
             *
             * @param e the event to be processed
             */
            @Override
            public void actionPerformed(ActionEvent e) {

                String employeeId, empname, salary, mobile;


                empname = txtName.getText();
                salary = txtSalary.getText();
                mobile = txtMobile.getText();
                employeeId = txtId.getText();


                try {
                    pst = con.prepareStatement("update employee_registration set empname = ?,salary + ?,mobile = ? where employeeId =?");
                    pst.setString(1, empname);
                    pst.setString(2, salary);
                    pst.setString(3, mobile);

                    pst.executeUpdate();
                    JOptionPane.showMessageDialog(null, "Record Updated!!!");
                    table_load();
                    txtName.setText("");
                    txtSalary.setText("");
                    txtMobile.setText("");
                    txtName.requestFocus();


                } catch (SQLException e1) {
                    e1.printStackTrace();
                }


            }
        });
        deleteButton.addActionListener(new ActionListener() {
            /**
             * Invoked when an action occurs.
             *
             * @param e the event to be processed
             */
            @Override
            public void actionPerformed(ActionEvent e) {
                String employeeid;



                employeeid = txtId.getText();




                try {
                    pst = con.prepareStatement("delete from employee_registration where employeeid = ?");
                    pst.setString(1, employeeid);
                    JOptionPane.showMessageDialog(null, "Record Deleted!!!");
                    table_load();
                    txtName.setText("");
                    txtSalary.setText("");
                    txtMobile.setText("");
                    txtName.requestFocus();

                }
                catch (SQLException e1) {
                    e1.printStackTrace();
                }








            }
        });
    }

    void table_load() {
            try {
                pst = con.prepareStatement("select * from employee_registration");
                ResultSet rs = pst.executeQuery();
                table1.setModel(DbUtils.resultSetToTableModel(rs));
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }












    }




}

Dani AI

Generated

— the symptoms you described usually come from simple SQL/PreparedStatement mistakes and from swallowing exceptions. The code you posted shows three likely culprits: the UPDATE SQL has a typo (it uses a plus where it should use an equals and you never set the id parameter), the DELETE path prepares a statement but does not call executeUpdate(), and some catch blocks suppress exceptions so failures are hidden. Also confirm the column name casing used in SQL matches the schema (for example employeeid vs employeeId).

Fixes to apply: correct the UPDATE statement so each column uses = ? and set the id as the last parameter before calling executeUpdate(); for DELETE, call executeUpdate() and check the returned row count to confirm deletion. Replace empty catch blocks with at least printStackTrace() or logging so SQL errors are visible. Use try-with-resources (or explicitly close ResultSet/PreparedStatement) to avoid resource leaks. If using a recent MySQL driver you can use the Connector/J 8 driver class or rely on JDBC autoloading; ensure the JDBC URL and timezone settings match the driver requirements.

Quick troubleshooting steps: run the failing UPDATE/DELETE directly in the MySQL client to verify SQL, print parameter values before execution, and inspect the integer returned by executeUpdate() to know whether rows changed. References: Java PreparedStatement usage (PreparedStatement (Java SE 8 API)), MySQL UPDATE syntax (MySQL UPDATE Syntax (MySQL 8.0 Reference Manual)), and Connector/J notes ().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.