I'm trying for four days to pass a value from my (xadmin.xhtml) to my (JadminBeans.java) but no luck so far. what i want to do is when i push the delete button inside a row using datatable all the data inside that row should be deleted form my datatable and mysql database. so this is a part of my code : Ps: i'm new to java EE and 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();
}
}
userBean:
public void deleteUser(String user){ UserDAO.DeleteUser(user); }
xhtml file:
<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(); } }
This is the way that i fix this problem ;)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.