Logging DB Related Activities (Connection, Statement, Prepared Statement, Callable Statement)

Most of database drivers hides what they do internally. Especially PreparedStatement is one of the class which we would like to know whether exact parameters are set properly. Printing the created statement really helps in debugging such cases. Some drivers supports logging by setting logging level to DEBUG, some may not.

This article explain how to create a simple wrapper which can log all internal statements.

1) Create Wrappers for all DB Access Classes like Connections, Statements, PreparedStatement and CallableStatement.
2) Wrap your connection object with the new Connection Wrapper.

Your code will looks like

//con is the existing connection from DataSource/JDBC
ConnectionFactory.getConnection(con,DBType.DB2);

Related classes are


I created this classes as library, you can download at Library for Java 5 and 6