用户注册时出错,更改用户而不是输入

时间:2016-11-09 14:17:46

标签: java mysql model-view-controller jdbc dao

我的问题如下,我看到当我编辑注册表时,它工作,注册表更改,但当我想注册一个新用户,它属于编辑条件,它编辑我是的注册表信息之前更改,不要离开注册新用户。

负责验证您是否正在编辑的变量。

 Private int codeEdit = 0;

负责在表中提取用户代码的方法。

    JButton button_3 = new JButton ( "Edit");
    Button_3.addActionListener (new ActionListener () {
    Public void actionPerformed (ActionEvent e) {

    TabbedPane.setSelectedIndex (0);

    // Variable responsible for capturing the selected line
    Int selectedRow = -1;
    // Search for selected row and store in variable
    // rowSelected (tr)
    //
    selectedRow = table.getSelectedRow();

    // Validate if a row was selected
    If (selectedRow >= 0) {
    // Stores information from the first row column
    // selected (td)
    // column 0 employee code
    Int code_employee = (int) table.getValueAt(selectedRow, 0);

    FindEmployee(code_employee);
    } Else {
    JOptionPane.showMessageDialog (null, "Select a" + "line to change Employee!");
    }
    }

});

接收员工代码作为参数的方法,将用户信息放在注册屏幕上进行更改。

// receives as parameter the employee code
Protected void searchEmployee (int cod_employee)
Control.Employees employees = new control.Employees();
// foreach loop that traverses the arraylist referencing the employee's code
For (Employees emp: employees.listEmployees(cod_employee + "", 1)) {


// Set information on the registration screen
Name.setText (emp.getName ());
Email.setText (emp.getEmail ());
Address.setText (emp.getAdress());
Phone.setText (emp.getTelephone());
Cpf.setText (emp.getCpf_employee());



// Variable that receives the value of the user code
// important for change
codeEdit = emp.getCode_employee();

}

register.setVisible (true);
Listing.setVisible (false);

}

负责保存或更换员工的方法。

Protected void save () {
        // Capture information that the user entered
        String employee_name = name.getText();
        String cpf_employee = cpf.getText();
        String address_employee = address.getText();
        String email_employee = email.getText();
        String phone_employee = phone.getText();
        String employee_password = password.getText();
        String rep_password_employee = repemployee.getText();
        If (email_employee.equals ( "") && cpf_employee.equals ( "") && phone_employee.equals ( "") && name_employee.equals ( "")) {
        JOptionPane.showMessageDialog (null, "Fill in the fields");

        } Else if (! rep_password_employee.equals (employee_password)) {
        JOptionPane.showMessageDialog (null, "Passwords are different");

        // validate if passwords are equal
        } Else if (address_employee.equals ( "")) {
        JOptionPane.showMessageDialog (null, "The Address field is Required!");
        }

                      // Creating the object

 Employees emp = new Employees ();
        
        emp.setName (name_employee);
        emp.setEmail (email_employee);
        emp.setTelephone (phone_employee);
        emp.setPassword (employee_password);
        emp.setAdress(address_employee);
        emp.setCpf_employee(cpf_employee);
        emp.setCode_employee(codeEdit);
        
        Control.Employees maintenance = new control.Employees();

// validate if we are registering or editing the records
        If (codeEdit == 0) {
        Maintenance.insert(emp);
        
        } Else {
        Maintenance.change (emp);
        
        }
        
        CleanInformations ();
        TabbedPane.setSelectedIndex (1);
        
        }
        }

Class dao

Public class Employees {

    Private int code_employee;
    Private String cpf_employee;
    Private String email;
    Private String address;
    Private String name;
    Private String password;
    Private String phone;

    Public int getCode_employee () {
        Return function_code;
    }
    Public void setCode_employee (int code_employee) {
        This.code_employee = code_employee;
    }
    Public String getCpf_employee () {
        Return cpf_employee;
    }
    Public void setCpf_employee (String cpf_employee) {
        This.cpf_employee = cpf_employee;
    }
    Public String getEmail () {
        Return email;
    }
    Public void setEmail (String email) {
        This.email = email;
    }
    Public String getAddress () {
        Return address;
    }
    Public void setAddress (String address) {
        This.address= address;
    }
    Public String getName () {
        Return name;
    }
    Public void setName (String name) {
        This.name = name;
    }
    Public String getPassword () {
        Return password;
    }
    Public void setPassword (String password) {
        This.password = password;
    }
    Public String getTelephone () {
        Return phone;
    }
    Public void setTelephone (String phone) {
        This.telephone = phone;
    }
}

保存员工类控制包的方法

Public void insert (dao.Employees emp) {
    If (emp! = Null) {
    // DB connection variable
    Java.sql.Connection conn = null;

    try {
    Conn = ConnectDB.connectionDB ();

    Java.sql.PreparedStatement pstm;

    Pstm = conn.prepareStatement (INSERT);
    Pstm.setString (1, emp.getCpf_employee());
    Pstm.setString (2, emp.getEmail ());
    Pstm.setString (3, emp.getPassword ());
    Pstm.setString (4, emp.getName ());
    Pstm.setString (5, emp.getAddress ());
    Pstm.setString (6, emp.getTelephone ());

    // Send to database
    Boolean test;
    Test = pstm.execute ();

    // Validate insertion in the database
    If (! Test) {
    JOptionPane.showMessageDialog (null, "Registered Successfully!");

    } Else {
    JOptionPane.showMessageDialog (null, "Error registering the employee!");
    }

// Close the connection to the database
ConnectDB.CloseClose (conn);

} Catch (Exception e) {
JOptionPane.showMessageDialog (null, "Error registering the official!");

}

}

}

更改员工类包控制的方法

// parameter object of the.functional
Public void change (dao.Employee emp) {
Java.sql.Connection conn = null;

try {
Conn = ConnectDB.conexaoDB ();

// prepare the query
Java.sql.PreparedStatement pstm;

Pstm = conn.prepareStatement (UPDATE);
Pstm.setString (1, emp.getCpf_employee ());
Pstm.setString (2, func.getEmail ());
Pstm.setString (3, func.getName ());
Pstm.setString (4, func.getAddress ();
Pstm.setString (5, func.getTelephone ());
Pstm.setString (6, func.getPassword ());
Pstm.setInt (7, func.getCode_employee());

// Send to database
Boolean test;
Test = pstm.execute ();

// Validate insertion in the database
If (! Test) {
JOptionPane.showMessageDialog (null, "Successfully changed employee!");

} Else {
JOptionPane.showMessageDialog (null, "Error while changing the official!");
}

// Close the connection to the database
ConnectDB.CloseClose (conn);

} Catch (SQLException e) {
JOptionPane.showMessageDialog (null, "Error while changing the official!" + E.getMessage ());

}



}

0 个答案:

没有答案