Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / June 2006

Tip: Looking for answers? Try searching our database.

Getting query SQL from a JDBC PreparedStatement

Thread view: 
Larry Coon - 05 Jun 2006 19:39 GMT
Using Sybase JConnect's implementation of JDBC in Java 1.5.

For logging/debug purposes, I want to get the SQL query text from
a PreparedStatement (which is a SybPreparedStatement in this
implementation).  Something like:

String query = "select xxx from mytable where aaa = ? and bbb = ?";

SybPreparedStatement s = (SybPreparedStatement)
connection.prepareStatement(query);

s.setString(1, "sarg1");
s.setString(2, "sarg2");

------

At this point I want to get the query text, i.e.:

  select xxx from mytable where aaa = "sarg1" and bbb = "sarg2"

From the prepared statement.  toString() doesn't return the query,
it returns a string like:

  com.sybase.jdbc2.jdbc.SybPreparedStatement@1f630dc

And I see no other methods that return a String.  What am I missing
here?
IchBin - 05 Jun 2006 20:17 GMT
> Using Sybase JConnect's implementation of JDBC in Java 1.5.
>
[quoted text clipped - 23 lines]
> And I see no other methods that return a String.  What am I missing
> here?

This maybe a backwards way of doing it. I would use a debugger and step
thru the instructions to find SybPreparedStatement and look it its content.

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)
joeNOSPAM@BEA.com - 05 Jun 2006 21:57 GMT
> Using Sybase JConnect's implementation of JDBC in Java 1.5.
>
[quoted text clipped - 23 lines]
> And I see no other methods that return a String.  What am I missing
> here?

Nothing. There is no standard JDBC or sybase-specific
method or variable which exposes the SQL you gave the
connection when asking for a PreparedStatement. The
statement certainly does have a version of the string,
possibly modified for the JDBC SQL escapes, but you
would have to decompile, alter, and recompile, and run
your modified driver to be able to get it programmatically.
Joe Weinstein at BEA Systems
Larry Coon - 06 Jun 2006 21:48 GMT
> Nothing. There is no standard JDBC or sybase-specific
> method or variable which exposes the SQL you gave the
[quoted text clipped - 3 lines]
> would have to decompile, alter, and recompile, and run
> your modified driver to be able to get it programmatically.

Thanks, Joe.  That wasn't the answer I was expecting or hoping
for.  Why wouldn't they provide that method?  I assume it was
intentionally not provided, since it seems so obvious -- what
was the reason?

The workaround is straightforward -- build the string myself
instead of using the setXxxxx() methods, and send the completed
string to createStatement() or prepareStatement().  Not as
clean, but at least I have access to the completed SQL....


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.