我正在尝试将我的(xadmin.xhtml)中的值传递给我的(JadminBeans.java)四天,但到目前为止还没有运气。我想要做的是当我使用datatable推送行内的删除按钮时,该行内的所有数据应该从我的datatable和mysql数据库中删除。所以这是我的代码的一部分: P.s:我是java EE和jsf的新手
xadmin.xhtml
<h:dataTable value="#{Jadmin.messages}" var="o"
styleClass="order-table"
headerClass="order-table-header"
rowClasses="order-table-odd-row,order-table-even-row"
>
<h:column>
<f:facet name="header">username</f:facet>
#{o.username}
</h:column>
<h:column>
<f:facet name="header">password</f:facet>
#{o.password}
</h:column>
<h:column>
<f:facet name="header">permission</f:facet>
#{o.permission}
</h:column>
<h:column>
<f:facet name="header">delete</f:facet>
<h:commandLink value="Delete" action="#{Jadmin.delete(o.username)}" />
</h:column>
</h:dataTable>
JadminBeans.java
@ManagedBean(name = "Jadmin")
@SessionScoped
public class JadminBeans {
private String username, password, permission;
private JadminController JaC;
// insert getter setter here
public List<JadminBeans> getMessages() {
return JadminDAO.getAllUsers();
}
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 void delete(String usr) {
System.out.println("JadminBeans >> delete() ---------- username= "+usr);
JadminDAO.deleteUser(usr);
}
}
JadminDAO.java
public static List<JadminBeans> getAllUsers() {
List<JadminBeans> users = new ArrayList<JadminBeans>();
try {
Connection con = DBconnection.getConnection();
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("select * from users");
while (rs.next()) {
JadminBeans user = new JadminBeans();
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setPermission(rs.getString("permission"));
System.out.println("JadminDAO > getAllUsers(): "+rs.getString("username"));
users.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
}
return users;
}
public static void deleteUser(String id) {
try {
PreparedStatement preparedStatement = connection.prepareStatement("delete from users where username=?");
// Parameters start with 1
preparedStatement.setString(1, id);
preparedStatement.executeUpdate();
System.out.println("JadminDAO >> deleteUser ----------"+ id);
} catch (SQLException e) {
System.out.println("JadminDAO >> deleteUser----------- SQLException :(");
e.printStackTrace();
}
}
答案 0 :(得分:0)
的UserBean:
public void deleteUser(String user){
UserDAO.DeleteUser(user);
}
xhtml文件:
<p:commandButton id="confirm" value="Yes Sure"
oncomplete="confirmation.hide()"
actionListener="#{userBean.deleteUser(userBean.selectedUser.user)}"
update=":form1:overviewTableUser">
</p:commandButton>
UserDAO:
public static void DeleteUser(String user) {
Connection con = null;
PreparedStatement ps = null;
try {
con = Database.getConnection();
String sql="delete from userinfo where user=?";
ps= con.prepareStatement(sql);
// Parameters start with 1
ps.setString(1,"user");
ps.executeUpdate();
System.out.println("user deleted successfully");
} catch (SQLException e) {
System.out.println(" SQLException :(");
e.printStackTrace();
}
}
这是解决这个问题的方法;)