Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / January 2008

Tip: Looking for answers? Try searching our database.

how to get the query from the Statement object

Thread view: 
Steve - 15 Jan 2008 20:35 GMT
Is there a way to get the query from the Statement object? I couldn't
find the API on that purpose.

Please help. Thanks.
RedGrittyBrick - 15 Jan 2008 21:55 GMT
> 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 :-)
Lew - 16 Jan 2008 00:30 GMT
>> Is there a way to get the query from the Statement object? I couldn't
>> find the API on that purpose.
[quoted text clipped - 20 lines]
> 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?

Signature

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.