how to get the query from the Statement object

This is a discussion on how to get the query from the Statement object within the JDBC JAVA forums in Framework and Interface Programming category; Is there a way to get the query from the Statement object? I couldn't find the API on that purpose. Please help. Thanks....

Go Back   Application Development Forum > Framework and Interface Programming > JDBC JAVA

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 01-15-2008, 03:35 PM
Steve
Guest
 
Default how to get the query from the Statement object

Is there a way to get the query from the Statement object? I couldn't
find the API on that purpose.

Please help. Thanks.
Reply With Quote
  #2  
Old 01-15-2008, 04:55 PM
RedGrittyBrick
Guest
 
Default Re: how to get the query from the Statement object

Steve wrote:
> Is there a way to get the query from the Statement object? I couldn't
> find the API on that purpose.
>


Generally if it's not in the API docs then there isn't.

Presumably you mean Interface java.sql.Statement and the String passed
to it's executeQuery method.

Perhaps you could write your own class that implements Statement,
encapsulates the Statement obtained by Connection.createStatement and
which keeps track of the most recent query?

Connection connection = DriverManager.getConnection( ... );
Statement statement = connection.createStatement();
MyStatement myStatement = new MyStatement(statement);
String sql = "select count(*) from aristocrat";
ResultSet rs = myStatement.executeQuery(sql);
...
String lastUsedQuery = myStatement.getLastQuery();
if (lastUsedQuery.equals(sql))
System.out.println("I'm astonished!");

This makes no sense to me, if it doesn't to you either, I suggest you
explain a bit about the background :-)
Reply With Quote
  #3  
Old 01-15-2008, 07:30 PM
Lew
Guest
 
Default Re: how to get the query from the Statement object

RedGrittyBrick wrote:
> Steve wrote:
>> Is there a way to get the query from the Statement object? I couldn't
>> find the API on that purpose.
>>

>
> Generally if it's not in the API docs then there isn't.
>
> Presumably you mean Interface java.sql.Statement and the String passed
> to it's executeQuery method.
>
> Perhaps you could write your own class that implements Statement,
> encapsulates the Statement obtained by Connection.createStatement and
> which keeps track of the most recent query?
>
> Connection connection = DriverManager.getConnection( ... );
> Statement statement = connection.createStatement();
> MyStatement myStatement = new MyStatement(statement);
> String sql = "select count(*) from aristocrat";
> ResultSet rs = myStatement.executeQuery(sql);
> ...
> String lastUsedQuery = myStatement.getLastQuery();
> if (lastUsedQuery.equals(sql))
> System.out.println("I'm astonished!");
>
> This makes no sense to me, if it doesn't to you either, I suggest you
> explain a bit about the background :-)


It's kind of a strange question, because Statements (as opposed to
PreparedStatements) live *very* close to the Strings that represent their
queries. There is no "setSql()" method - you directly
"execute{,Query,Update}( String sql )", then you're straight into results.

So the String is right there with the Statement, right at the point where you
invoke it. You just use that String again, if you need it.

What is the scenario that necessitates retrieving the String at a separate
place or time?

--
Lew
This post contains one request for information, and echoes RedGrittyBrick's
request for essentially the same information.
follow-up set to clj.databases
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 08:39 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.