aspose file tools*
The moose likes JDBC and the fly likes How to fetch the sql query from the prepared statement at runtime? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to fetch the sql query from the prepared statement at runtime?" Watch "How to fetch the sql query from the prepared statement at runtime?" New topic
Author

How to fetch the sql query from the prepared statement at runtime?

Deepak Kumar
Ranch Hand

Joined: Nov 05, 2007
Posts: 62
Hi All,

Below there is an snippet of code ,
Assume con,ps,rs are defined properly.



Thanks & Regards,
Deepak Kumar G.S
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2510
    
  10

There is no out of the box solution for you, but this website: Save time debugging your database queries with DebuggableStatement has a solution: A DebuggableStatement .

It can print your sql statement, with bind parameters inlined into the query.


OCUP UML fundamental and ITIL foundation
youtube channel
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

Just use a debugging version of the driver. Many JDBC vendors provide a regular and debug version, with the debug version outputting the SQL statements. Also, its possible your current driver will output the SQL if you add some config parameters to the driver connection string. I don't recommend swapping the DebuggableStatement method for 2 reasons- you have to refactor (then later unfactor) your code to use it, and there's all ready a solution available by most, if not all, JDBC drivers. I take issue with JavaWorld post in that it invents a solution to a problem that all ready has a better solution available, one that doesn't require you to change your code.

If all else falls you can turn on tracing on the database server and monitor the queries on that end.


My Blog: Down Home Country Coding with Scott Selikoff
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2510
    
  10

I have also considered advising the debugging driver, but that does not output the sql statement with the bind parameters embedded, does it? That was what the OP was asking.
If the debugging driver does that, than I am willing to revoke my advise .
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2510
    
  10

Ah. I see it supports in-lining bind parameters by using property replace.bindParams.
I hereby revoke my proposal.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to fetch the sql query from the prepared statement at runtime?