java - How can I get the SQL of a PreparedStatement? -


i have general java method following method signature:

private static resultset runsqlresultset(string sql, object... queryparams) 

it opens connection, builds preparedstatement using sql statement , parameters in queryparams variable length array, runs it, caches resultset (in cachedrowsetimpl), closes connection, , returns cached result set.

i have exception handling in method logs errors. log sql statement part of log since it's helpful debugging. problem logging string variable sql logs template statement ?'s instead of actual values. want log actual statement executed (or tried execute).

so... there way actual sql statement run preparedstatement? (without building myself. if can't find way access preparedstatement's sql, i'll end building myself in catches.)

using prepared statements, there no "sql query" :

  • you have statement, containing placeholders
    • it sent db server
    • and prepared there
    • which means sql statement "analysed", parsed, data-structure representing prepared in memory
  • and, then, have bound variables
    • which sent server
    • and prepared statement executed -- working on data

but there no re-construction of actual real sql query -- neither on java side, nor on database side.

so, there no way prepared statement's sql -- there no such sql.


debugging purpose, solutions either :

  • ouput code of statement, placeholders , list of data
  • or "build" sql query "by hand".

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -