This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JSP and the fly likes Passing checkbox data from JSP to SELECT statement 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 "Passing checkbox data from JSP to SELECT statement" Watch "Passing checkbox data from JSP to SELECT statement" New topic
Author

Passing checkbox data from JSP to SELECT statement

Dave Jordan
Greenhorn

Joined: Nov 18, 2003
Posts: 5
Help please!
I have a form where the user checks any or all of 6 boxes, I then need to take the user's choices and use them in a SELECT statement in a query.
the JSP looks like:
<table border="2" style="background-color:#FFFF99;">
<tr>
<TD WIDTH=85% ALIGN="LEFT">
<B>Check Departments To Include:</B><br>
<INPUT TYPE=CHECKBOX NAME="dept" VALUE="csr">Customer Service<br>
<INPUT TYPE=CHECKBOX NAME="dept" VALUE="clm">Claims<br>
<INPUT TYPE=CHECKBOX NAME="dept" VALUE="bne">Billing & Enrollment<br>
<INPUT TYPE=CHECKBOX NAME="dept" VALUE="adj">Adjustments<br>
<INPUT TYPE=CHECKBOX NAME="dept" VALUE="apl">Appeals<br>
<INPUT TYPE=CHECKBOX NAME="dept" VALUE="sub">Subrogation<br>
<INPUT TYPE=CHECKBOX NAME="dept" VALUE="acg">Accounting<br>
</td></tr>
</table>
This is passed to a controller, which then calls a method which contains the following select statement:
sql.append("SELECT bla, bla, bla FROM bla, bla, bla");
sql.append("WHERE ( dept_code IN (?);

The selected department codes would replace the question mark. It seems like this should be easy, right? All help is Greatly appreciated!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Does your question regard how to get the form submission result, or how to format the SELECT statement?
bear
[ December 05, 2003: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tim Baker
Ranch Hand

Joined: Oct 04, 2003
Posts: 541
each checkbox needs it own name if your going to allow more than one to be checked. then you use getParameter and if it returns something then they checked it and if it returns null then they didnt. then use a prepared statement to set the boolean value in your SQL


Kim Jong II (North Korea's Dear Leader) said:Nuclear weapons don't kill people, people kill people.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

I'd change the following line:
each checkbox needs it own name if your going to allow more than one to be checked.
to:
if you want to make retrieval easy, give each checkbox its own name

but then again, based on my last paragraph, this should be:
sometimes retrieval is easier when giving each checkbox its own name - other times it's not

It's not a requirement to give mutually selectable checkboxes different names (unlike radio buttons).

form.html

foo.jsp


You'd just have to make each checkbox value be the primary key of the department, and append it into the "in clause" string you're constructing

In fact, that might be easier, than requesting each and every department, and having to decide whether to include it or not (which you'd need to do if each checkbox had a different name). Also, if you add a department, you'd need to change the processing jsp. With checkbox of the same name, only the initial list of departments need change.
[ December 05, 2003: Message edited by: Mike Curwen ]
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

As a follow-up.

If you need to have the full names of what the checkboxes mean, in the second page... that second page will query the database to retrieve the full name. If you weren't loading them from a database, then just use a bunch of if statements.

(This was prompted by a private message from the original poster: who has private messages turned off).
Dave Jordan
Greenhorn

Joined: Nov 18, 2003
Posts: 5
Yes, the full names are not coming from a database. Looks like I'll have to go with the multiple "if" statements. Thanks for your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing checkbox data from JSP to SELECT statement