Search code examples
javamysqljdbcresultset

Is there any way to retrieve the executed query from java ResultSet?


I created a method called public static ResultSet ExecuteSQLQuery(String myQuery) now at run-time I need to retrieve the executed query from the ResultSet.

I wonder if it is possible to retrieve the executed query from the ResultSet ?


Solution

  • In short: No.

    It might be possible to use getStatement from the ResultSet. Some JDBC drivers may expose the query in the statement's toString method, but even if this is the case it should not be relied upon.

    You'll have to save the query and keep it around if you want to use it later.

    You could create a new type of ResultSet that includes the query and return that:

    public class ResultSetWithQuery extends ResultSet {
        private String query
        ...
    }
    
    public static ResultSetWithQuery ExecuteSQLQuery(String myQuery);