How do I display a prepared statement's variables via a Log Driver?
Author: Deron Eriksson
Description: This tutorial describes how to display a prepared statement including bind variable values using a Log Driver.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.4 || MySQL 5.0.27


Page: < 1 2

(Continued from page 1)

If we execute our LogDriverTest class (running against a databaseW we set up in a different tutorial), we observe the following:

0 [main] DEBUG net.rkbloom.logdriver.LogDriver  - Real JDBC driver Class: com.mysql.jdbc.Driver
0 [main] DEBUG net.rkbloom.logdriver.LogDriver  - Real JDBC connection string: jdbc:mysql://localhost:3306/testdatabase
0 [main] DEBUG net.rkbloom.logdriver.LogDriver  - Trying to find: jdbc:mysql://localhost:3306/testdatabase
281 [main] DEBUG net.rkbloom.logdriver.LogConnection  - Opening connection: com.mysql.jdbc.Connection@da3a1e
SQL Statement:
	SELECT * FROM employees where last_name=? and first_name like ?
296 [main] DEBUG net.rkbloom.logdriver.LogPreparedStatement  - executing PreparedStatement: 'SELECT * FROM employees where last_name=? and first_name like ?' with bind parameters: {1=Doe, 2=%n%}
ID: 1, First Name: John, Last Name: Doe
ID: 3, First Name: Jane, Last Name: Doe
296 [main] DEBUG net.rkbloom.logdriver.LogConnection  - Closing connection: com.mysql.jdbc.Connection@da3a1e

Notice that when the prepared statement is executed, the statement including the bind variable values is displayed on the "executing PreparedStatement" line (ie, the '296 [main] DEBUG net.rkbloom.logdriver.LogPreparedStatement' line).

Cool, huh? Being able to view prepared statements with bind variable values is extremely useful when trying to debug difficult database issues. It can even be used under technologies like HibernateW (since at the bottom this boils down to connecting to a database with a JDBCW driver).

Page: < 1 2