Thread: Retrieve Query

Retrieve Query

From
Hugo Sacramento
Date:
How do I to retrieve the string query prepared by a PreparedStatement??

i.e.:

-------------
PreparedStatement stmt = con.prepareStatement("select * from abc where x=?");
stmt.setString(1, x);
System.out.println("My Prepared Query:" + stmt.toString());
------------

but it don't work.




Yahoo! Acesso Grátis
Internet rápida e grátis. Instale o discador agora!

Re: Retrieve Query

From
Markus Schaber
Date:
Hi, Hugo,

Hugo Sacramento wrote:
> How do I to retrieve the string query prepared by a PreparedStatement??

> -------------
> PreparedStatement stmt = con.prepareStatement("select * from abc where
> x=?");
> stmt.setString(1, x);
> System.out.println("My Prepared Query:" + stmt.toString());
> ------------
>
> but it don't work.

There are at least two potential problems:

- The statement is prepared server-side, and the parameters are sent
separately (binding). Only the server has the "real" representation of
the complete statement. So the prepared statement can only give an
approximation, which is what pgjdbc does.

- Most connection pooling and datasource mechanisms (e. G. in jboss)
wrap your connection and all other classes (including the statement),
but don't delegate the toString() method, so you'll only get something
like "org.jboss.pool.wrappedStatement@4711".

HTH,
Markus
--
Markus Schaber | Logical Tracking&Tracing International AG
Dipl. Inf.     | Software Development GIS

Fight against software patents in EU! www.ffii.org www.nosoftwarepatents.org

Re: Retrieve Query

From
Achilleus Mantzios
Date:
O Markus Schaber έγραψε στις Feb 27, 2006 :

> Hi, Hugo,
>
> Hugo Sacramento wrote:
> > How do I to retrieve the string query prepared by a PreparedStatement??
>
> > -------------
> > PreparedStatement stmt = con.prepareStatement("select * from abc where
> > x=?");
> > stmt.setString(1, x);
> > System.out.println("My Prepared Query:" + stmt.toString());
> > ------------
> >
> > but it don't work.
>
> There are at least two potential problems:
>
> - The statement is prepared server-side, and the parameters are sent
> separately (binding). Only the server has the "real" representation of
> the complete statement. So the prepared statement can only give an
> approximation, which is what pgjdbc does.
>
> - Most connection pooling and datasource mechanisms (e. G. in jboss)
> wrap your connection and all other classes (including the statement),
> but don't delegate the toString() method, so you'll only get something
> like "org.jboss.pool.wrappedStatement@4711".

In jboss there is getUnderlyingStatement() method
so you could

PreparedStatement st=...
.....
org.jboss.resource.adapter.jdbc.local.LocalPreparedStatement jst =
(org.jboss.resource.adapter.jdbc.local.LocalPreparedStatement) st;

org.postgresql.jdbc3.Jdbc3PreparedStatement pst =
(org.postgresql.jdbc3.Jdbc3PreparedStatement) jst.getUnderlyingStatement();

System.out.println(pst.toString());

>
> HTH,
> Markus
>

--
-Achilleus


Re: Retrieve Query

From
Hugo Sacramento
Date:
I got jdbc source code and make it with follow change:

org.postgresql.jdbc3.Jdbc3PreparedStatement.java:

    public String toString() {
        return preparedQuery.toString(preparedParameters);
    }

Works fine.


Achilleus Mantzios escreveu:
> O Markus Schaber έγραψε στις Feb 27, 2006 :
>
>
>> Hi, Hugo,
>>
>> Hugo Sacramento wrote:
>>
>>> How do I to retrieve the string query prepared by a PreparedStatement??
>>>
>>> -------------
>>> PreparedStatement stmt = con.prepareStatement("select * from abc where
>>> x=?");
>>> stmt.setString(1, x);
>>> System.out.println("My Prepared Query:" + stmt.toString());
>>> ------------
>>>
>>> but it don't work.
>>>
>> There are at least two potential problems:
>>
>> - The statement is prepared server-side, and the parameters are sent
>> separately (binding). Only the server has the "real" representation of
>> the complete statement. So the prepared statement can only give an
>> approximation, which is what pgjdbc does.
>>
>> - Most connection pooling and datasource mechanisms (e. G. in jboss)
>> wrap your connection and all other classes (including the statement),
>> but don't delegate the toString() method, so you'll only get something
>> like "org.jboss.pool.wrappedStatement@4711".
>>
>
> In jboss there is getUnderlyingStatement() method
> so you could
>
> PreparedStatement st=...
> .....
> org.jboss.resource.adapter.jdbc.local.LocalPreparedStatement jst =
> (org.jboss.resource.adapter.jdbc.local.LocalPreparedStatement) st;
>
> org.postgresql.jdbc3.Jdbc3PreparedStatement pst =
> (org.postgresql.jdbc3.Jdbc3PreparedStatement) jst.getUnderlyingStatement();
>
> System.out.println(pst.toString());
>
>
>> HTH,
>> Markus
>>
>>
>
>



_______________________________________________________
Yahoo! Acesso Grαtis - Internet rαpida e grαtis. Instale o discador agora!
http://br.acesso.yahoo.com