This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes JSP and the fly likes JSP, Servlet, DB help? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Reply locked New topic

JSP, Servlet, DB help?

Lars Ulrich

Joined: Dec 03, 2002
Posts: 3
I have a JSP page that displays rows of user/password with a check box in front of each user. The administrator will use this to delete users from the DB. How would I set up the Servlet to iterate through the list of checked users and delete them from the DB?
[ September 21, 2006: Message edited by: Bear Bibeault ]
Thomas Harrington

Joined: Mar 29, 2003
Posts: 22
I use a bean, so this may be different from what you are looking for but maybe it will help. I have an inner class in my bean which takes a string, and then has a Delete FROM XyourtableX WHERE XnameofvalueqeuriedX = "?". the XyourtableX should be the name of your table, and the XnameofvaluequeriedX should be the value you are using to find the enrty to delete. I use the primary key, as there can only be one and you will not delete multiple entries that way.
The JSP which processes the removal of the checked items should retrieve the values of the checked boxes, probably by using request.getParameter(), where you have some variable equal it and name that whatever you want to pass to the bean.
BTW, there was a great example of what you are trying to do with Begenning JSP Web Services Programming by Wrox. They have an example like what you are trying to do. I think you can download the sample code from their website.
nirav soni parmar

Joined: Aug 22, 2006
Posts: 15
Ok.. I will remember next Time.

Thank You.
Tom Joiner
Ranch Hand

Joined: Sep 19, 2006
Posts: 47
In my databases, I never actually "delete" anything. Why? Because you never know when you want to go back and get data, especially something small like a user list. Therefore, I would created your database with a "deleted" column which you them check when the user deletes themselsves. Then, all your queries against the database should obviously ignore the deleted users, unless you are doing some historical analysis of old users.

Karthik Srikanti

Joined: Sep 20, 2006
Posts: 11
Give all teh checkboxes the same name something like

<input type="checkbox" id="uid" name="checkValue" value="<primary id in your table>">

Then in the servlet


String [] checkValue = request.getParameterValues("checkValue");

String sql = "Delete from users where <primaryid> in ("

for(int i=0;i<checkValue.length;i++)
sql+=checkValue[id] + ",";
//Add logic to remove , for last entry

Then establish a connection to a database and do something like this


Hope that helps..

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63873

I don't really think there's a need to answer a 3 1/2 year old question.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: JSP, Servlet, DB help?
It's not a secret anymore!