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();
            }












    }




}
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.