how to get the query from the Statement object

Discussion in 'Java' started by Steve, Jan 15, 2008.

  1. Steve

    Steve Guest

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

    Please help. Thanks.
    Steve, Jan 15, 2008
    #1
    1. Advertising

  2. 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 :)
    RedGrittyBrick, Jan 15, 2008
    #2
    1. Advertising

  3. Steve

    Lew Guest

    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
    Lew, Jan 16, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    789
  2. Jay McGavren
    Replies:
    11
    Views:
    1,112
    Alan Krueger
    Jan 16, 2006
  3. tedsuzman
    Replies:
    2
    Views:
    7,062
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  4. Ted
    Replies:
    1
    Views:
    451
    Duncan Booth
    Jul 22, 2004
  5. bthumber
    Replies:
    5
    Views:
    408
    Alexey Smirnov
    Jan 29, 2009
Loading...

Share This Page