Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using PreparedStatement when the number of parameters will only be known at runtime

 
Sonny Gill
Ranch Hand
Posts: 1211
IntelliJ IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you use PreparedStatement for queries in which you do not how many parameters you will need until runtime.
Say, you have 3 optional parameters, you dont know which ones or how many the user will specify until runtime, but if they are set they have to be included.

This is how I am doing it now.

_____________________________________________




Am I missing something? Is there an easier way of doing this?(Except for the getParameterCount in JDK1.4 which eliminates the need of keeping count of the parameters set)
[ June 24, 2004: Message edited by: Sonny Gill ]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sonny,
I would do that in a very similar way. The only things I would change are:

1) Use a StringBuffer to concatenate the string (slightly faster)
2) Put the date comparison at the end of the sql string. Some databases may optimize the query better if the function comes later.

The idea of concatenating the string is definitely correct.
 
Sonny Gill
Ranch Hand
Posts: 1211
IntelliJ IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeanne,

wonderful! that's what I was looking for, a confirmation that I am on the right track.
And thanks for the tip on putting the date function at the end.

Sonny
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic