• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jdbc

 
Chandrashekhar Telang
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

i urgently need to build a dynamic query builder using prepared statement
how an a dynamic select query having multiple parameters can be built?
For. e.g. select * from xyz where a=? AND|OR b=? AND|OR c=? where occurence of a,b,c can be known only at runtime from user selection of parameters to the query builder also the parameter types also could vary such as String, int or a double? also clauses AND or OR can occur in between the parameters. Any Help would be highly appreciated.
thanks in advance.

Chandrashekhar
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why using PreparedStatement is a must?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33713
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see this being easier with a Statement over a PreparedStatement. And a PreparedStatement caches the execution plans for when the dynamic SQL happens to be ths same.

Building a statement or prepared statement is just a matter of logic (in java) to assemble the pieces separated with AND/OR. I'm not sure I understand the question.
 
James Carman
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using PreparedStatements can be safer, though. Consider the case where you're substituting in values typed from the user on a webpage (or somewhere else). If they "Jim's Bar and Grill", you have to make sure you escape the ' character if you try to just build the SQL string dynamically. But, if you use a PreparedStatement and set the parameters, this is done for you.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was just asking the reason, nothing else.
If you can do it with Statement then you can also go with PreparedStatement. PreparedStatement is better.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic