Reputation: 1305
I have another problem on my project I am working right now, I have searched everywhere to find some solution for this but couldn't get...
I'm having a bean class with id,firstName,lastName attributes having public getters and setters, and an updateEmployee method.
i'm using following jsf page to get database table values.
When i click on update button success page is shown but values are not changing in the database. Can any one tell me the reason that why vales are not getting change in the database?
JSF page:
<h:dataTable value="#{tableBean.employeeList}" var="employee" border="1">
<h:column>
<f:facet name="header">First name</f:facet>
<h:inputText value="#{employee.firstName}" />
</h:column>
<h:column>
<f:facet name="header">Last name</f:facet>
<h:inputText value="#{employee.lastName}" />
</h:column>
</h:dataTable>
<h:commandButton value = "update" action="#{employee.updateEmployee}"/>
Employee.java:
public String updateEmployee(){
String query = "update employee set firstName = ?,lastName = ? where id = 1";
pstmt = conn.prepareStatement(query);
pstmt.setString(2,this.firstName);
pstmt.setString(3,this.lastName);
pstmt.executeUpdate(); // execute update statement
conn.commit();
committed = true;
return "success.xhtml";
}catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try{
if (!committed) conn.rollback();
pstmt.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
Thanks, Raam
Upvotes: 0
Views: 709
Reputation: 1108782
The posted code is strange. You're not invoking the action method on the bean holding all edited employees, but you're instead invoking the action method on the bean which represents by itself a completely blank instance of an employee, which is not one of the employees shown in the table.
To start, replace
<h:commandButton value="update" action="#{employee.updateEmployee}" />
by
<h:commandButton value="update" action="#{tableBean.updateEmployees}" />
and it'll become more straightforward.
Upvotes: 1