Thread: How to get the SQL query from a PreparedStatement?

How to get the SQL query from a PreparedStatement?

From
"Andres Olarte"
Date:
Hi!

Is it possible to get the query string (including the formatted
parameters) from a PreparedStatement?

The idea is to get the query text, to send it into an stored procedure
to be executed within the sp (with some other stuff).  This is an idea
I had to simulate variable parameter lists in stored procs.  Is it
possible?

Thanks in advance,

Andres

Re: How to get the SQL query from a PreparedStatement?

From
Oliver Jowett
Date:
Andres Olarte wrote:

> Is it possible to get the query string (including the formatted
> parameters) from a PreparedStatement?

In general you can't because the query's not sent like that (the
parameter values are sent out of line from the query).

IIRC, PreparedStatement.toString() will give you an approximation, but
I'm not sure how accurate the results are (and it's not part of the spec)

-O

Re: How to get the SQL query from a PreparedStatement?

From
"Andres Olarte"
Date:
Then, is it possible to use the driver just to format the parameters
into the PG format?  My guess is that it should be done somewhere, for
example when using setXXX methods.  But I haven't looked deep enough
in the source.  Would it be possible to use this functionality so I
don't have to reinvent the wheel?

On Nov 6, 2007 1:45 AM, Oliver Jowett <oliver@opencloud.com> wrote:
> Andres Olarte wrote:
>
> > Is it possible to get the query string (including the formatted
> > parameters) from a PreparedStatement?
>
> In general you can't because the query's not sent like that (the
> parameter values are sent out of line from the query).
>
> IIRC, PreparedStatement.toString() will give you an approximation, but
> I'm not sure how accurate the results are (and it's not part of the spec)
>
> -O
>