best way to dynamically filter SQL statement?

Hi: this is in essence a two-pronged question.

I have a set of 6 checkboxes on a form that I'm trying to query a results set for.

Currently, the 6 checkboxes have different names, but the DB values are either Y or N.

1) I'm wondering, is it better to name them all the same, and assign different values, and make it an array on the ensuing JSP page that returns the result set, or am I better off leaving them w/ different names since the DB values are all either Y or N?

2) what's the best way to filter these in returning the result set? I also am returning about 15 other values, in addition to querying for these 6.

It seems like a very, hairy dynamic SQL statement in the works for verifying if they check boxes 1 & 2, or 1,2, 4, 6, or 3, 4 & 6, etc.

Having trouble with the SQL statement and best approach. Any feedback is welcomed...here is the code.

Statement selstmt = connection.createStatement();

String preparedQuery ="SELECT DISTINCT AID, ACTIVE, REQUESTOR_NAME, REQUESTOR_EMAIL,BUS_CONTACT " +

",DEPT,LOCATION,DATE_REQ,BUSVP,VP_PHONE,VP_DATE,BRANCH,PRIORITY" +

",STATUS1,STATUS2 " +

",EXIST_SYS_FLD_CHG, BUS_RULES_CHG, NEW_CODING, NEW_BUS_RULE,NEW_SYS_FIELD,REQ_TYPE_OTHER " +

" FROM CHANGE_CONTROL_ADMIN a INNER JOIN CHANGE_CONTROL_USER b " +

"ON a.CHANGE_CTRL_ID = b.CHANGE_CTRL_ID " +

" WHERE EXIST_SYS_FLD_CHG = CASE WHEN = 'Y' THEN EXIST_SYS_FLD_CHG ELSE END BUS_RULES_CHG = 'Y' ORDER BY AID";

then

try{

while (rslts.next()){

int aidn = rslts.getInt(1); ....

...

String status2 = rslts.getString(15);

String exis_sys_fld = rslts.getString(16);

String bus_rul_chg = rslts.getString(17);

String reqnewc = rslts.getString(18);

String reqnewbr = rslts.getString(19);

String reqnewsfd = rslts.getString(20);

String reqotr = rslts.getString(21);

Of course I wouldn't be making all the fields like BUS_RULES_CHG = 'Y' but rather dynamically write these to the SQL statement (hopefully!) as the request params were checked off.

Also, this would effect the results set String params created. So that in some cases, if check boxes were left blank for example,

String reqotr = rslts.getString(21) might be String reqotr = rslts.getString(18) another time would it not?

Again, thanks.

***********************************************

I'd thought it might look like this,

" FROM CHANGE_CONTROL_ADMIN a INNER JOIN CHANGE_CONTROL_USER b " +

"ON a.CHANGE_CTRL_ID = b.CHANGE_CTRL_ID WHERE ";

<%if (existfld.equals("Y")){ %>

+"EXIST_SYS_FLD_CHG = 'Y'";

<%} %>

<%if (reqbizrule.equals("Y")){ %>

+"BUS_RULES_CHG = 'Y' ";

<%} %>

<%if (reqnewcode.equals("Y")){ %>

+"NEW_CODING = 'Y' ";

<%} %>

+" ORDER BY AID";

where the existfld, reqnewcode & reqbizrule are set as request.getParameter values of the checkboxes.

I get illegal expression errors trying it this way.

Message was edited by:

bpropes20

[3989 byte] By [bpropes20a] at [2007-10-3 2:01:25]
# 1

The simplest way for SQL construction would be like this:

String sql = "select ..... WHERE 1=1;

if ("Y".equals(existfld)){

sql = sql + " AND EXIST_SYS_FLD_CHG = 'Y'";

}

if ("Y".equals(reqbizrule)){

sql = sql + " AND BUS_RULES_CHG = 'Y'";

}

It would work better if there was a standard WHERE option that could be put in place of "1=1" - thats just there so you don't worry about whether you put a "WHERE" or an "AND" at the start of each condition. Of course it wouldn't take much change to make it smarter.

--

Re: reorganising the checkboxes.

This approach works, and is simple to understand. Why change it?

Having said that, consider this approach:

Give the checkboxes all the same name.

Have the value of each checkbox be the field that should be filtered Y/N

If a field is present in the request, it is filtered on, otherwise not.

What you end up with is an array of merely the fields that should be filtered on.

The sql construction then becomes a simple for loop

for (int i=0; i<filterFields.length; i++){

sql += " AND " + filterField[i] + " = 'Y'";

}

Its a little more complicated, but also a little more elegant in my view.

Cheers,

evnafets>

evnafetsa at 2007-7-14 19:00:09 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Thanks, evnafets, I'll give that a shot!
bpropes20a at 2007-7-14 19:00:09 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Thanks, evnafets!!Worked like a champ! Sometimes it takes someone else to have me put the glasses on and look at things in a different light. I was trying to box the logic really in a backwards way!!Thanks!!
bpropes20a at 2007-7-14 19:00:09 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...