• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is it possible to get a string representation of a PreparedStatement?

 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The application that I am writing uses PreparedStatements heavily, but I have run into a situation where I might have to add an additional 'AND' or 'OR' clause to the statement depending on certain conditions. I want to get the PreparedStatement as a string, then append more SQL to it, then execute it as a regular Statement.
Is this possible? I dont see any direct method to get a string representation of a PreparedStatement.
SAF
 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no such method. You can only store the sql string as a String if you need to use it later(with the '?' in it).
Jamie
 
SAFROLE YUTANI
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, looks like you're right. Therefore, I took matters into my own hands and created my own class named DynamicStatement. It's very similar to PreparedStatement in that you have access to most of the frequently used set<type>() methods, such as setInt(), setString(), and setDate().
My class also gives the user the ability to append to the statement. So if the user wanted to add a NOT IN clause, or an AND conditional, no problem.
The internal query is managed by a StringBuffer, so it's easy to replace all the "?" with most data types you provide. Of course I didn't get complicated and provide methods like setBlob() and setAsciiStream(), but hell, I've never used those methods, so no need to add crap I wont be using.
The date formatting I used for setDate() is specific to Oracle, but I guess I can implement a way to make it flexible to work with any database.
Also, the set() methods I created dont require an index argument like they do in PreparedStatement. Just call setString("bite me") instead of PreparedStatement.setString( 1, "bite me"). Of course you must ensure that you call the set methods in the order that the parameters are expecting the correct data in your sql statement, but that's no problem since most people use PreparedStatement the same way.
I wrote this class really quick. I'm sure there are things I overlooked, but I tested it out and it works nice. Tell me what you think.


[This message has been edited by SAFROLE YUTANI (edited November 30, 2001).]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic