Meaningless Drivel is fun!*
The moose likes JSP and the fly likes how to delete a row from database by selecting it in jsp page using checkbox? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "how to delete a row from database by selecting it in jsp page using checkbox?" Watch "how to delete a row from database by selecting it in jsp page using checkbox?" New topic
Author

how to delete a row from database by selecting it in jsp page using checkbox?

renu sogi
Greenhorn

Joined: Feb 06, 2012
Posts: 15
Hello everyone!!!
I want to delete a specific row from the Mysql database; which is been selected using checkbox. Now my problem is that; I am executing a query (select * from table_name where Account_No='100') . so when I execute this query ; it shows me the Account having Account_No=100. Now by selecting this row using the checkbox i want to delete it ; but couldn't find a way to do so......... Can anyone please help me out....... thanks in advance......
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Submit the id of the row to be removed back to a servlet, which can use the API provided by the model classes to cause the corresponding element to be removed.

Under no circumstances should any of this code be in a JSP. Indeed, there should never be any code in a JSP.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
renu sogi
Greenhorn

Joined: Feb 06, 2012
Posts: 15
Thank you ! Mr. Bear Bibeault for your quick reply.. The concept is kind of clear to me now. but just wanted to tell you ; that I don't have any code in jsp. Here is code fragment....
admin.jsp...
now this page takes an Account Number from user and request is sent to 'search servlet' given below..


so when execute this program ; it returns an account having the account no. entered by user. which looks like this....(image attached..) now when i select this row...
by clicking on delete button I want to delete this row...so what would you suggest sir! thank you!!


[Thumbnail for del.png]

jhon masco
Ranch Hand

Joined: May 13, 2010
Posts: 93
Hi Renu
That is simple!
When you build you second form, you must to have hidden html input text field with information like "id" or any other that you need for do Delete query in mysql.
Then when you press the delete button, it must submit all this data to other servlet (a servlet for delete).

Other thing: in my opinion you dont need the check box!! if you have only one register in the table.!

Ok, that all. Let me know if you have problem with my explanation.

Suerte!
renu sogi
Greenhorn

Joined: Feb 06, 2012
Posts: 15
thanks Mr. jhon ! yes , I understood the idea that you propose...what I didnt got is that what you saying regarding not having checkbox ;can you please make it clear .... also do i need to use request.getParametervalues() to get all the values in a selected row . What would you say? thanks in advance :beerchug:
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1473
    
    6

Are you trying to delete multiple records at a time?


Swastik
renu sogi
Greenhorn

Joined: Feb 06, 2012
Posts: 15
well ; when i check a row an entire row should get deleted the moment i click delete button. thats what i am trying to do.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1473
    
    6

That was not what I was asking. Can the user select multiple rows at a time and delete? If this is not the case, what is the use of checkbox here, you can always use radio buttons.
renu sogi
Greenhorn

Joined: Feb 06, 2012
Posts: 15
well yes ! i want to delete multiple rows . now what i am stuck at is that how to retrieve selected checkbox values in my servlet so that i can fire a delete query...thanks in advance... :)
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1473
    
    6

I believe you must be using some key (id,name) based on which your records are deleted. So, what you can do is you can make this key as the value of the check box. Now in the servlet you can use the following code.



The only thing you need to make sure is name of all the checkboxes are same in the page.
renu sogi
Greenhorn

Joined: Feb 06, 2012
Posts: 15
thank you very much !!! swastik dey! thats what i was thinking to do but wasnt sure . thanks for the advice...........
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1473
    
    6

You are welcome renu.
 
Consider Paul's rocket mass heater.
 
subject: how to delete a row from database by selecting it in jsp page using checkbox?