participate


JavaServer Pages (JSP) and JSTL - problem with deleting multiple records
<<   Back to Forum  |   Give us Feedback
This topic has 9 replies on 1 page.
Limo2kz
Posts:84
Registered: 5/19/04
problem with deleting multiple records   
May 21, 2005 4:55 PM

 
I have a jsp page that displays records in a dynamic table with each record
having its own checkbox called select. When a check box is selected and the
form is submitted it goes to another page called delete.jsp which is shown
below. The code works but at the moment it will only delete one record. I would
like to be able to be able to use delete to remove all the checked items I
have selected but I cannot seem to get it to delete more than one record at a time.
I tested the page and printed out the string called selected and it printed the
string "1,2,3" which are the ID values that i selected. Does anyone have any
ideas where i am going wrong or maybe you know a better way that i could
delete more than one at a time.


<%  
 
	String deleteArray[] = request.getParameterValues("select");
	String selected="";
 
	if (deleteArray != null) {
		
		// multiple items selected
		for (int i = 0; i < deleteArray.length; i++) {
 
		selected += deleteArray[i];
 
		if( i < deleteArray.length -1) {
			selected += ",";
			}
	 } 
		
}
 
Driver DriverdeleteTopics = (Driver)Class.forName(MM_connQuestion_DRIVER).newInstance();
Connection ConndeleteTopics = DriverManager.getConnection(MM_connQuestion_STRING,MM_connQuestion_USERNAME,MM_connQuestion_PASSWORD);
PreparedStatement deleteTopics = ConndeleteTopics.prepareStatement("DELETE FROM Question.topic  WHERE Topic_ID IN ('" + selected + "')");
deleteTopics.executeUpdate(); 
%>


Any help would be much appreciated

Thanks
 
Limo2kz
Posts:84
Registered: 5/19/04
Re: problem with deleting multiple records   
May 22, 2005 4:31 AM (reply 1 of 9)  (In reply to original post )

 
I changed the code to the following to see if it would make a difference but it will still
only seem to delete one record when i make check multiple boxes. Can someone please tell me what the problem seems to be as i cannot seem to work it out.

<%  
 
	String deleteArray[] = request.getParameterValues("select");
	String selected="";
 
	if (deleteArray != null) {
		
		if(deleteArray.length == 1) {
 
			// Only one item is selected
			selected = deleteArray[0];
 
	 } else {
 
		// multiple items selected
		for (int i = 0; i < deleteArray.length; i++) {
 
		selected += deleteArray[i];
 
		if( i < deleteArray.length -1) {
			selected += ",";
}
 
 
}
 
}
 
 
}
 
%>
<% 
Driver DriverdeleteTopics = (Driver)Class.forName(MM_connQuestion_DRIVER).newInstance();
Connection ConndeleteTopics = DriverManager.getConnection(MM_connQuestion_STRING,MM_connQuestion_USERNAME,MM_connQuestion_PASSWORD);
PreparedStatement deleteTopics = ConndeleteTopics.prepareStatement("DELETE FROM Question.topic  WHERE Topic_ID IN ('" + selected + "')");
deleteTopics.executeUpdate(); 
%>


Thanks
 
evnafets
Posts:10,694
Registered: 4/29/03
Re: problem with deleting multiple records   
May 22, 2005 1:14 PM (reply 2 of 9)  (In reply to original post )

 
Try printing out the SQL statement you are about to execute:

ie:
String sql = "DELETE FROM Question.topic  WHERE Topic_ID IN ('" + selected + "')"
System.out.println("About to execute " + sql);


Check your database to see if the rows are actually deleted or not.

How does the JSP page of checkboxes get generated? Maybe the rows are being deleted from the database, but you need to update/refresh a java list elsewhere in memory as well?

Hope this helps,
evnafets
 
Limo2kz
Posts:84
Registered: 5/19/04
Re: problem with deleting multiple records   
May 24, 2005 6:02 AM (reply 3 of 9)  (In reply to #2 )

 
I printed the query and it looks like this

com.mysql.jdbc.ServerPreparedStatement[1] - DELETE FROM Question.topic WHERE Topic_ID IN ('2,5')
 


the row in the table on the jsp page that sets the values of the checkbox looks like this.

<td><input name="select" type="checkbox" id="select" value="<%=(((topicRecordset_data = topicRecordset.getObject("Topic_ID"))==null ||topicRecordset.wasNull())?"":topicRecordset_data)%>" /></td>


is it something to do with adding another "," on the end or is there something I am not doing something properly?
 
Limo2kz
Posts:84
Registered: 5/19/04
Re: problem with deleting multiple records   
May 25, 2005 7:50 AM (reply 4 of 9)  (In reply to #3 )

 
I added a comma onto the end of the selected string to see if it would make any difference but it still seems to only delete one record at a time. Can anyone tell me wot is wrong with my code or my SQL string as i can not seem to see any problems.

Thanks
 
raghav_132
Posts:11
Registered: 5/25/05
Re: problem with deleting multiple records   
May 25, 2005 8:11 AM (reply 5 of 9)  (In reply to #4 )

 
try this one

DELETE FROM Question.topic WHERE Topic_ID IN (" selected ")

Remove the Single Quote in the IN Clause.....
 
dummy-girl
Posts:44
Registered: 4/22/05
Re: problem with deleting multiple records   
May 25, 2005 8:52 PM (reply 6 of 9)  (In reply to #5 )

 
If ur TOPIC_ID is numeric, then u dont need single quotes. Try raghav_132's.

If not it should do like this:

DELETE FROM Question.topic WHERE Topic_ID IN ('2','5')

change ur 'for' loop to append single quotes also in your 'selected' string
 
unisol_amit
Posts:26
Registered: 5/25/05
Re: problem with deleting multiple records   
May 25, 2005 11:39 PM (reply 7 of 9)  (In reply to #6 )

 
Yes ! I agree to dummy-girl. She sounds quit logical.
As ur string values must be in single quotes and numeric values without quotes.
If Topic_ID is numeric (as it sounds like numeric) than u should not append single quotes.

try this out...
i hope it will help u..

regards
amit
 
Limo2kz
Posts:84
Registered: 5/19/04
Re: problem with deleting multiple records   
May 26, 2005 7:10 AM (reply 8 of 9)  (In reply to #7 )

 
Thanks for your help guys, really appreciate it! managed to get it working now so it will delete multiple records.

There was one other question that i was wondering about. At the moment i have a submit button at the bottom of the page and it looks a bit untidy so i would like to be able to sumit the delete form using an image on that is outside the form on the left hand side of the page. I looked into it and addedd some javascript that looks like this.

function submitform()
{
  document.deleteForm.submit();
}


and then i put this in the image tag

<a href="javascript:;" onclick="submitform()"><img src="../img/delete_topic.png" width="175" height="35" border="0" />


i though this would submit the form but it does not appear to do so. Doews anyone hagve any ideas about how to do this? im not sure but i think i need to use a hidden field somewhere but i am not sure what would need to go in it. Any help would be much appreciated.

Thanks

xxx
 
g00fy_m
Posts:102
Registered: 7/22/03
Re: problem with deleting multiple records   
May 26, 2005 7:19 AM (reply 9 of 9)  (In reply to #8 )

 
y not just

<input type="image" onclick="this.form.submit()" src="../img/delete_topic.png" width="175" height="35" />


hth

goofy
 
This topic has 9 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 24
  • Guests : 134

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums