This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes JDBC and the fly likes Conditional 'where' condition Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "Conditional Watch "Conditional New topic
Author

Conditional 'where' condition

meera rao
Ranch Hand

Joined: Jun 30, 2005
Posts: 67
I have a situation , where there are 5 text boxes and the User can enter values in any number of the text boxes and Query for the result.

What could be a possible solution in this case. Is it possible to have selective 'where' conditions.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


Is it possible to have selective 'where' conditions.

Absolutely. Its just a matter of manipulating the SQL string you use (i.e. append more to it if a checkbox is checked).


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
meera rao
Ranch Hand

Joined: Jun 30, 2005
Posts: 67
Can you explain more clearly, how can we append conditions
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Well this depends on whether you are doing this as a PreparedStatement or a Statement, but below is some partial pseudo-code to build sql for a Statement, using conditional where clause entries:
meera rao
Ranch Hand

Joined: Jun 30, 2005
Posts: 67
I am using a callable Statement. Can you help me with that?
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
You just need to pass parameters to stored procedure, depends on definition of SP, but in general it will be the same work as Paul mentioned above.


SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
meera rao
Ranch Hand

Joined: Jun 30, 2005
Posts: 67
I didn't understand how to implement it . Canyou give me an example
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35241
    
    7
A callable statement is not really applicable in this case, as the number of parameters is not fixed. Construct your SQL directly like in the example above (and of course take care of SQL injection vulnerabilities).


Android appsImageJ pluginsJava web charts
meera rao
Ranch Hand

Joined: Jun 30, 2005
Posts: 67
My Sql statement is a combination of many left joins. And I shouldn't be using sql stmt in my java code " according to the rules".

Is there any other way?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Originally posted by meera rao:
My Sql statement is a combination of many left joins. And I shouldn't be using sql stmt in my java code " according to the rules".

Is there any other way?


Unless it makes sense to use a Statement - for example when the sql is mutable.
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
Now I'm confused what you are trying to do. Are you using stored procedures? If not, why you are using CallableStatement?

You cannot use Statement or PreparedStatement?
Padma Lalwani
Ranch Hand

Joined: Nov 02, 2004
Posts: 49
You can implement your stored procedure with 5 input params (assuming you have max of 5 checkboxes), and pass null or 0, when the corresponding checkbox is not checked
In the SP, you would tweak the query as Paul has illustrated, appending condition to query if input param is not null. You need to be careful with the 'where' and 'and' clauses

If you need variable number of params, a standard way would be to use delimited input string, and parse the string in SP to retrieve individual values, unless your database provides support for arrays or collections

Padma
meera rao
Ranch Hand

Joined: Jun 30, 2005
Posts: 67
since I am using a stored procedure. I wrote a function that returns a variable that is needed to append to the where clause.

say the variable 'qry' returns ' where txtbx1 = I_bx1 and txtbx2 = I_bx2'
where I_bx1 and I_bx2 are variables that will be passed to the stored procedure.


I am not able to append it to my sql query . How should i do it.

I tired " select abc from tblabc || qry ".
meera rao
Ranch Hand

Joined: Jun 30, 2005
Posts: 67
can anybody help me out plz
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

I don't think any of us understand why you are using a Stored Procedure to do what you are trying to do. Typically, Stored Procedures will represent a repeatable bit of query logic where only the input parameters are allowed to change. You seem to be trying to write a Stored Procedure which itself changes, if that is the case, a Statement makes more sense. Is there a specific reason you are using a procedure?
Bryan Scarbrough
Ranch Hand

Joined: Aug 08, 2005
Posts: 49
Paul Sturrock said:


How can you remove the trailing " and "? Actually I want to remove a trailing comma from a StringBuffer, but I am sure the principle is the same.


Bryan Scarbrough<br /> <br />Consistency is the last resort of the unimaginative!
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26184
    
  66

Bryan,
You can use the delete() method on StringBuffer. It takes the indexes you want to delete as parameters, but you can find those using lastIndexOf().


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Bryan Scarbrough
Ranch Hand

Joined: Aug 08, 2005
Posts: 49
Thanks for the reply! I figured out a solution - I am taking the count() of the StringBuffer value and then using the deleteCharAt(countValue) and removing the last character, which happens to be the comma value.

I will definitely look into your post however since I will probably need to do this function more often.

Thanks again.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Conditional 'where' condition
 
Similar Threads
retaining value in java script
Wrapping of select box
How can this be done (lines with panels)
enter button doesn't work on select boxes
providing hint below text boxes