title: How do I get back a PreparedStatement query string when using Spring's JDBCTemplate?
body: <p>I'm using Spring's <code>JdbcTemplate</code> (Spring version 4.1.4). With my class' <a href="http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jdbc/core/JdbcTemplate.html" rel="nofollow noreferrer">JdbcTemplate</a> instance, I'm doing a select query as a prepared statement. However, my query unexpectedly returns no results. As such, I really need to debug my query and make sure that the query is what I expect it ought to be.</p>
  
  <p>How can I get back the actual SQL that is executed against the DB as part of the <code>JdbcTemplate</code>'s internal <code>PreparedStatement</code>?</p>
  
  <p>I'm familiar with the using the <a href="https://stackoverflow.com/questions/2683214/get-query-from-java-sql-preparedstatement">PreparedStatement's toString() method</a> to accomplish this, but as <code>JDBCTemplate</code> uses <code>PreparedStatement</code> internally, I'm not sure how feasible it is using Spring.</p>
  
  <p>Some sample code I am using is as follows:</p>
  
  <pre><code>private static final String PREPARED_QUERY =
      "select\n" +
          "  spm.amount\n" +
          "from\n" +
          "  search_price_modifier spm\n" +
          "where\n" +
          "  spm.search_id = ?\n" +
          "  and spm.search_date &gt; to_date(?, 'MM-DD-YYYY HH24:MI:SS')\n" +
          "  and spm.search_date &lt; to_date(?, 'MM-DD-YYYY HH24:MI:SS')\n";
  
  public void runQuery(String searchId, String strSearchDateInfimum,
      String strSearchDateSupremum) {
  
    SqlRowSet amounts = this.jdbcTemplate.queryForRowSet(
        PREPARED_QUERY_FOR_FLAT_MARKUP_VERIFICATION,
        searchId, strSearchDateInfimum, strSearchDateSupremum);
    while (amounts.next()) {
      float amount = amounts.getFloat("AMOUNT");
      LOGGER.debug("amount=" + amount);
    }
  }
  </code></pre>