title: How can I get the SQL of a PreparedStatement?
body: <p>I have a general Java method with the following method signature:</p>
  
  <pre><code>private static ResultSet runSQLResultSet(String sql, Object... queryParams)
  </code></pre>
  
  <p>It opens a connection, builds a <code>PreparedStatement</code> using the sql statement and the parameters in the <code>queryParams</code> variable length array, runs it, caches the <code>ResultSet</code> (in a <code>CachedRowSetImpl</code>), closes the connection, and returns the cached result set.</p>
  
  <p>I have exception handling in the method that logs errors. I log the sql statement as part of the log since it's very helpful for debugging. My problem is that logging the String variable <code>sql</code> logs the template statement with ?'s instead of actual values. I want to log the <em>actual</em> statement that was executed (or tried to execute).</p>
  
  <p>So... Is there any way to get the actual SQL statement that will be run by a <code>PreparedStatement</code>? (<em>Without</em> building it myself. If I can't find a way to access the <code>PreparedStatement's</code> SQL, I'll probably end up building it myself in my <code>catch</code>es.)</p>