I am trying to write an utility method which will take CallableStatement object and log SQL. I am able to get all parameter metadata information from getParameterMetadata method but main challenge is how to fetch IN parameter values which we set explicitly.
This probably isn't possible directly. The JDBC drivers generally don't let you read back parameters that were put in.
There is a possibility to wrap the JDBC classes with your own, which would take care of the logging. You can either do it on your own (as described here), assuming your requirements are fairly simple, or use an existing library, such as Log4JDBC (there might be others).
Joined: Jul 03, 2014
Thanks martin for quick response, but both solution need code change for all existing codebase. I were looking for some direct approach.
The approach you strive for is not feasible, as far as I know. I've been trying to achieve the same, and ended up implementing my own wrappers around the JDBC objects (the first approach from my previous post).
I haven't studied it in detail, but the Log4JDBC approach seems pretty lightweight. All you need is to add a jar (or two) to the project, load a different JDBC driver and modify the connection URL. I'm afraid it won't get much simpler than that. If you're deploying your application to a JEE application server, you might need some more tweaking to let it use the Log4JDBC driver, but it should be just the configuration, not code changes.
Other than that, if you need this just in one place of your code, a possibility would be to modify just that part of code to log the parameter values when it is passing them to the CallableStatement instance.