*
The moose likes JDBC and the fly likes Is it possible to get a string representation of a PreparedStatement? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Is it possible to get a string representation of a PreparedStatement?" Watch "Is it possible to get a string representation of a PreparedStatement?" New topic
Author

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

SAFROLE YUTANI
Ranch Hand

Joined: Jul 06, 2001
Posts: 257
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
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

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

Joined: Jul 06, 2001
Posts: 257
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).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it possible to get a string representation of a PreparedStatement?
 
Similar Threads
PreparedStatement
Prepared Statement Compilation
JAXP/W3C DOM Question
A question about PreparedStatement and ORA-03115 error
moving cursor to the next row