Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java JSP Servlet
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:
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:
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:
	<%
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 7-Jul-13 5:11am
kellycx745
Edited 7-Jul-13 7:52am
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

int userId = Integer.parseInt(userID[i]); 
  Permalink  
Comments
kellycx at 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)

  Print Answers RSS
0 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Sergey Alexandrovich Kryukov 105
4 Afzaal Ahmad Zeeshan 82
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 8 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100