我有一个用这段代码更新员工表的 jsf 页面....我可以获取数据,但我不能用 primefaces 更新它。当我调试它时,它什么也没做。我需要帮助来更新数据。谢谢这是我的页面控制器
@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);
}
}
这是我的 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.
}
}
这是jsf页面..
<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>