I have a general Java method with the following method signature:
private static ResultSet runSQLResultSet(String sql, Object... queryParams)
It opens a connection, builds a PreparedStatement
using the sql statement and the parameters in the queryParams
variable length array, runs it, caches the ResultSet
(in a CachedRowSetImpl
), closes the connection, and returns the cached result set.
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 sql
logs the template statement with ?'s instead of actual values. I want to log the actual statement that was executed (or tried to execute).
So... Is there any way to get the actual SQL statement that will be run by a PreparedStatement
? (Without building it myself. If I can't find a way to access the PreparedStatement's
SQL, I'll probably end up building it myself in my catch
es.)
Invoke the PreparedStatement. getParameterMetaData method to retrieve a ParameterMetaData object. Invoke ParameterMetaData. getParameterCount to determine the number of parameters in the PreparedStatement.
They are written in SQL and stored in the database rather than in the application code.
3 Answers. Show activity on this post. When you use prepared statement(i.e pre-compiled statement), As soon as DB gets this statement, it compiles it and caches it so that it can use the last compiled statement for successive call of same statement. So it becomes pre-compiled for successive calls.
Using prepared statements, there is no "SQL query" :
But there is no re-construction of an actual real SQL query -- neither on the Java side, nor on the database side.
So, there is no way to get the prepared statement's SQL -- as there is no such SQL.
For debugging purpose, the solutions are either to :
It's nowhere definied in the JDBC API contract, but if you're lucky, the JDBC driver in question may return the complete SQL by just calling PreparedStatement#toString()
. I.e.
System.out.println(preparedStatement);
At least MySQL 5.x and PostgreSQL 8.x JDBC drivers support it. However, most other JDBC drivers doesn't support it. If you have such one, then your best bet is using Log4jdbc or P6Spy.
Alternatively, you can also write a generic function which takes a Connection
, a SQL string and the statement values and returns a PreparedStatement
after logging the SQL string and the values. Kickoff example:
public static PreparedStatement prepareStatement(Connection connection, String sql, Object... values) throws SQLException { PreparedStatement preparedStatement = connection.prepareStatement(sql); for (int i = 0; i < values.length; i++) { preparedStatement.setObject(i + 1, values[i]); } logger.debug(sql + " " + Arrays.asList(values)); return preparedStatement; }
and use it as
try { connection = database.getConnection(); preparedStatement = prepareStatement(connection, SQL, values); resultSet = preparedStatement.executeQuery(); // ...
Another alternative is to implement a custom PreparedStatement
which wraps (decorates) the real PreparedStatement
on construction and overrides all the methods so that it calls the methods of the real PreparedStatement
and collects the values in all the setXXX()
methods and lazily constructs the "actual" SQL string whenever one of the executeXXX()
methods is called (quite a work, but most IDE's provides autogenerators for decorator methods, Eclipse does). Finally just use it instead. That's also basically what P6Spy and consorts already do under the hoods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With