A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Win a copy of
Murach's Java Servlets and JSP
this week in the
OO, Patterns, UML and Refactoring
I need Design advice...
Joined: Sep 18, 2005
Jun 18, 2006 21:18:00
Hi, I'm implementing a search module for my app. I was thinking if there is a good technique I could use for this.
if the user entered no search options( there are 3 options required like age preference, religion and citizenship). I was thinking of an implementation like this but it doesn't look elegant...
if (preferredAge.equals("") || religion.equals("") || citizenship.equals("") )
dao.search(preferredAge, religion, citizenship)
that's making an overloaded version of search method. Any better ideas? Thanks!
Joined: May 24, 2005
Jun 18, 2006 21:38:00
How about you encapsulate this into dao.search(age, religion, citizen).
If this is sql query:
query = "select * from user_table where <default_conditions> " +
age.equals("")? "" : "age = ? " +
religion.equals("")? "" : "religion = ? " +
citizen.equals("")? "" : "citizen = ? " ;
By no means this is complete code, but should give you the idea.
Joined: Jan 29, 2003
Jun 19, 2006 10:43:00
Years ago I had a complex search screen in an environment that did not allow dynamic SQL. I made a "bitmap" of which fields were entered by the user:
if first-name is not null, set bit 1
if last-name is not null, set bit 2
if city is not null, set bit 4
if state is not null, set bit 8
and then made a mapping of bitmap values to prepared sql statements
if bitmap = 1 query first name only else if bitmap = 3 query first name and last name else invalid combination of fields!
I might resort to the same trick if the combinations of fields were really tricky.
But it's simpler to
for a few valid ones. Today we do dynamic SQL and test for minimum required inputs:
if ( firstname and lastname ) ok else if ( lastname and state ) ok else throw "specify more search criteria!" build dynamic sql with whatever we got
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
It is sorta covered in the
JavaRanch Style Guide
subject: I need Design advice...
Clash of Civilizations!!
component layout not wrapping inside Panel
String split function and "||" String
Stored procedure which returns array
Using Sun's Migration Tool...
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2014