Question

i have a jsf page that update employee table with this code.... i can get the data,but i can't update it with primefaces.and when i debug it,it doesn't do anything. i need help to update the data. thanks this is my page controller

@ManagedBean(name = "homebean")
@ViewScoped
public class HomeController implements Serializable {

    private Employee employee;
    private List<Employee> employees;
    private EmployeeeDAO employeeeDAO;

    public HomeController() {
        employeeeDAO = new EmployeeDAOImpl();
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    public List<Employee> getEmployees() {

        return employees;
    }

    public void setEmployees(List<Employee> employees) {
        this.employees = employees;
    }

    public void editEmployee() throws SQLException {
        Employee e = this.getEmployee();
        employeeeDAO.update(e);
    }

    @PostConstruct
    private void getListEmployees() {
        employees = employeeeDAO.employees(20, 10);
    }
}

and this is my DAO

public class EmployeeDAOImpl implements EmployeeeDAO, Serializable {

    private PreparedStatement ps;
    private ResultSet rs;

    @Override
    public void add(Employee emp) {
        try (Connection c = ConnectionHelper.getConnection()) {
            String sql = "UPDATE EMPLOYEES"
                    + "   SET FIRST_NAME = ?,"
                    + "       LAST_NAME = ?,"
                    + "       EMAIL = ? WHERE EMPLOYEE_ID=?";


            ps = c.prepareStatement(sql);
            ps.setString(1, emp.getFirstname());
            ps.setString(2, emp.getLastname());
            ps.setString(3, emp.getEmail());
            ps.setInt(4, emp.getEmployeeId());
            ps.executeUpdate();
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        } catch (SQLException ex) {
            Logger.getLogger(EmployeeDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void update(Employee emp) throws SQLException {
        try (Connection c = ConnectionHelper.getConnection()) {
            String sql = "UPDATE EMPLOYEES "
                    + "   SET FIRST_NAME = ?,"
                    + "       LAST_NAME = ?,"
                    + "       EMAIL = ? WHERE EMPLOYEE_ID=?";


            ps = c.prepareStatement(sql);
            ps.setString(1, emp.getFirstname());
            ps.setString(2, emp.getLastname());
            ps.setString(3, emp.getEmail());
            ps.setInt(4, emp.getEmployeeId());
            ps.execute();
        } catch (SQLException ex) {
            Logger.getLogger(EmployeeDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public List<Employee> employees(int size, int lastRow) {
        List< Employee> list = null;
        try (Connection c = ConnectionHelper.getConnection()) {
            String sql = "SELECT ROWNUM NUM,EMP.* FROM EMPLOYEES EMP ORDER BY ROWNUM";

            ps = c.prepareStatement(sql);
            rs = ps.executeQuery();
            list = new ArrayList<Employee>();
            while (rs.next()) {
                Employee e = new Employee();
                e.setEmployeeId(rs.getInt("EMPLOYEE_ID"));
                e.setFirstname(rs.getString("FIRST_NAME"));
                e.setLastname(rs.getString("LAST_NAME"));
                e.setEmail(rs.getString("EMAIL"));
                e.setHireDate(rs.getDate("hire_date"));
                e.setSalary(rs.getBigDecimal("salary"));
                list.add(e);
            }
        } catch (Exception e) {
        }
        return list;
        // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Employee getById(String id) {

        return null;
        //  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

and this is the jsf page..

            <p:dataTable id="employeetable" value="#{homebean.employees}" var="emp" rows="10" paginator="true"
                         selection="#{homebean.employee}">

                <p:column headerText="First Name">#{emp.firstname}</p:column>
                <p:column headerText="First Name">#{emp.lastname}</p:column>
                <p:column headerText="First Name"><h:outputText value="#{emp.salary}">
                        <f:convertNumber type="currency" currencySymbol="$ "/>
                    </h:outputText></p:column>
                <p:column headerText="First Name">#{emp.email}</p:column>
                <p:column width="50">
                    <p:commandButton value="Edit" oncomplete="editdialog.show();"
                                     partialSubmit="true" update="@([id$=display])">
                        <f:setPropertyActionListener value="#{emp}" target="#{homebean.employee}" />
                    </p:commandButton>
                </p:column>
            </p:dataTable> 

        </h:form>
        <h:form>
            <p:dialog widgetVar="editdialog" id="edit" modal="true" appendToBody="true" resizable="false">

                <h:panelGrid id="display" columns="2" cellspacing="4">
                    <h:outputText value="ID"/>
                    <p:inputText value="#{homebean.employee.employeeId}" disabled="true">
                        <f:convertNumber type="number"/></p:inputText>
                    <p:spacer/><p:spacer/>
                    <h:outputText value="First Name"/>
                    <h:outputText value="Last Name"/>
                    <p:inputText value="#{homebean.employee.firstname}"/>
                    <p:inputText value="#{homebean.employee.lastname}"/>
                    <h:outputText value="Email"/>
                    <h:outputText value="Salary"/>
                    <p:inputText value="#{homebean.employee.email}"/>

                </h:panelGrid>
                <p:separator/>
                <p:commandButton value="save" action="#{homebean.editEmployee()}" partialSubmit="true"
                                 update="@([id$=employeetable])" oncomplete="editdialog.hide();"/>
            </p:dialog>

        </h:form>
    </ui:define>
Was it helpful?

Solution

finally i get the answer....we need to make sure that the property of Employee is correct. and have match datatype with our database...thanks all

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top