我试图编辑用户数据,但我无法将更新后的值设置为管理bean。
我尝试在我的SQL更新中手动编写值并且它可以正常工作,但我现在没有选择,任何帮助将不胜感激:
XHTML:
<p:dataTable id="DT" value="#{Jadmin.messages}" var="o"
selection="#{Jadmin.selectedUser}" rowKey="#{o.id}"
style="margin-bottom:20px" editable="true">
<f:facet name="header">
Users List
</f:facet>
<p:column selectionMode="single" />
<p:column>
<f:facet name="header">
<h:outputText value="id" />
</f:facet>
<h:outputText value="#{o.id}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="username" />
</f:facet>
<p:inputText value="#{o.username}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="password" />
</f:facet>
<h:outputText value="#{o.password}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="permission" />
</f:facet>
<h:outputText value="#{o.permission}" />
</p:column>
<f:facet name="footer">
<p:commandButton value="Update" action="#{Jadmin.update}" />
<p:commandButton value="Delete"
action="#{Jadmin.delete}"
ajax="false"
update=":form:msgs"/>
</f:facet>
</p:dataTable>
更新我认为导致问题的数据库的方法 爪哇:
public static void updateUser(JadminController user) {
try {
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE users SET username = ?,password =?, permission =? WHERE username = ? ");
preparedStatement.setString(1, user.getUsername()); //updated value should be here
preparedStatement.setString(2, user.getPassword());//updated value should be here
preparedStatement.setString(3, user.getPermission());//updated value should be here
preparedStatement.setString(4, user.getUsername());//searching by user for now I will change it to id later
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
JadminController.java
package admin_page;
import java.io.Serializable;
public class JadminController implements Serializable {
private static final long serialVersionUID = 1L;
private String username, password, permission;
private long id;
boolean editable;
public JadminController() {}
public JadminController(String username, String password, String permission) {
this.username = username;
this.password = password;
this.permission = permission;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPermission() {
return permission;
}
public void setPermission(String permission) {
this.permission = permission;
}
}
Managed Bean:
public class JadminBeans implements Serializable{
private static final long serialVersionUID = 1L;
private String username, password, permission;
private JadminController selectedUser;
List<JadminController> userslist = new ArrayList<JadminController>();
public List<JadminController> getMessages() {
System.out.println("List<JadminController> getMessages()");
userslist= JadminDAO.getAllUsers();
return userslist;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPermission() {
return permission;
}
public void setPermission(String permission) {
this.permission = permission;
}
public JadminController getSelectedUser() {
return selectedUser;
}
public void setSelectedUser(JadminController selectedUser) {
this.selectedUser = selectedUser;
}
public void add(){
JadminController ADDUSER = new JadminController (this.username, this.password ,this.permission);
userslist.add(ADDUSER);
JadminDAO.addUser(ADDUSER);
}
public void update(){
JadminDAO.updateUser(selectedUser);
}
}