Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I actually go about deleting multiple users using checkboxes? I have a page whereby it shows all created users with a checkbox beside each row created base on the number of created users. For now, I am only able to delete one checkbox at a time.

I understand that for multiple deletion, i need to make use of String []. But my code only does grabs the first checkbox to delete.

My servlet code:
Java
String []userID = request.getParameterValues("deleteUser");
				
				
				for(int i=0;i<userID.length;i++){
					int userId = Integer.parseInt(request.getParameter("deleteUser")); 
					User users = new User();
					users.setUserID(userId);
					DAOManager mgr = new DAOManager();
					mgr.deleteUsers(users);
					response.sendRedirect("reg.jsp");


DAO Code:
Java
public boolean deleteUsers(User user)
	{
		String sql = "DELETE FROM user WHERE userID=?";
		try {
		
		Connection conn = getConnection();
		
		PreparedStatement ps = conn.prepareStatement(sql);
		
		ps.setInt(1, user.getUserID());
		
		ps.executeUpdate();
		return true;
		} catch (Exception e) {
		e.printStackTrace();
		return false;
		}
		}


JSP code:
Java
	<%
DAOManager mgr = new DAOManager();
User[] users = mgr.getAllUsers();
for(int i = 0; i < users.length; i++)
 {
%>
										<tr>
												<td><input
													type="checkbox" name="deleteUser"
													value=<%=users[i].getUserID()%>></td>
												<td><%=users[i].getUserID()%></td>
												<td><%=users[i].getUserName()%></td>										</tr>
										
										<%
}
%>
Posted
Updated 7-Jul-13 6:52am
v3

1 solution

Java
int userId = Integer.parseInt(userID[i]); 
 
Share this answer
 
Comments
kellycx 8-Jul-13 11:16am    
Thank you so much Shubhashish! It worked!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900