Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to delete a row by selecting a check box using jdbc ?

 
gavin sharma
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a program using (MVC) framework in which the below code will fetch total no. of request made by a user and each with 7 columns. Now i want to do is delete a particular row using a check box which user selects. Can anyone tell me how can i detect which check box user has selected so that only that row is deleted ?

 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
Java MySQL Database Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags

You probably need getParameterValues in your servlet (or JSP) to get an array of your checkbox value/s and then do the required processing.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35266
383
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch! I've added the code tags.

Ahsan is correct that you need to call request.getParameterValues() to determine which checkboxes are selected. I see a few other problems in the code though:
1) The checkboxes need distinct values so you have a way of knowing which ones are submitted
2) You need an in clause in your SQL query since multiple checkboxes can be selected. (And if you only want one, it should be a radio button, not a checkbox).
3) If this is anything beyond a toy app, you should use a PreparedStatement to prevent SQL Injection. And even this is a toy app, you should read about SQL Injection so you know why to avoid it in real apps.
 
gavin sharma
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for pointing out the fault in the SQL code... However, i want to know how to relate each checkbox with a row so that i know which row is been selected ?
 
Paul Clapham
Sheriff
Posts: 21546
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the form is sent back to your servlet which is going to delete the row, you're going to need some information which tells you which row to delete, right? So that would be the primary key of the table, which if I read your code right is the employee ID.

That means that you have to send the employee ID in the request, in a place where you can relate it to the checkbox.
 
gavin sharma
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that i need to send a column which is a primary key to get that row deleted, but i dont know how to link (primary key ) column of each row with the checkbox so that when i select a particular checkbox, the (primary key ) column of that specific row is detected
 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
Java MySQL Database Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your employee id appears to be coming in `rs.getString(1)`. So why not have line 72 laid out like,


Disclaimer: Untested code and a bad practice. You are better off thinking your design by strictly embedding the presentation code in the JSP using JSTL and EL. Please read this very nicely articulated section by Bear which expounds on this.

Also read what Paul and Jeanne had to say; you probably want a radio button instead of a check box.
 
gavin sharma
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot buddy ..it wrkd.. am all set now
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic