Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > how to get the query from the Statement object

Reply
Thread Tools

how to get the query from the Statement object

 
 
Steve
Guest
Posts: n/a
 
      01-15-2008
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
 
 
 
 
RedGrittyBrick
Guest
Posts: n/a
 
      01-15-2008
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
 
 
 
 
Lew
Guest
Posts: n/a
 
      01-16-2008
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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
My Switch statement get stuck on the last statement bthumber ASP .Net 5 01-29-2009 07:56 PM
if statement that, when false, skips first statement in its block, executes second? Jay McGavren Java 11 01-16-2006 05:49 PM
How do I do a conditional statement in a constant statement? tkvhdl@gmail.com VHDL 3 12-16-2005 06:13 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM



Advertisments