Is there a way we can print prepared statement as a string with all the placeholders replaces with its actual values
- I'm using Oracle 11g and Java 1.6
- Using log4j packages for logging
Is there a way we can print prepared statement as a string with all the placeholders replaces with its actual values
you can use log4j.properties
log4j.logger.java.sql.PreparedStatement=DEBUG
Maybe you can describe better what you have in your project for get better answers
If you're executing the query and expect that you'll have a definite ResultSet then the following call ResultSet's getStatement()
method will definitely help.
ResultSet resultSet = preparedStatement.executeQuery();
String executedQuery = resultSet.getStatement().toString();
You either use System.out.println(executedQuery)
or log4J
as you state, that's your choice of implementation.
Hope this helps!
As you are using Oracle's JDBC driver, you will get OraclePreparedStatement
object when you call connection.preparedStatement(sql)
. Here's the javadoc for it and as it does not override toString()
method, you will see a hash
of the object on calling toString()
(default implementation).
In this case, the only option is to write your own method that accepts String
sql and parameters
and prints pretty String
.
As you are using plain jdbc with Oracle driver, I will try a configuration similar to the one described in this oracle post:
https://docs.oracle.com/cd/B28359_01/java.111/b31224/diagnose.htm
Another thing you can do is install JProfiler, there is a Database configuration there that you can use to trace every call that your application is doing to your database.
You also can set the log4j property that was described in another response. If you do it this way, remember to set the log4j root logger into TRACE/DEBUG mode.