2

Possible Duplicate:
Seeing the underlying SQL in the Spring JdbcTemplate?

I am using jdbctemplate like this:

getJdbcTemplate.update("update ....... values (?,?,?....?)", myObject.getProperty1(), ...);

Is it possible for me to write out a logger.trace of the resulting sql sent to mysql?

It would make my debug process easier as I could see exactly what mysql is getting.

Community
  • 1
  • 1
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
  • Just pitching an alternative approach. In the case that you are running in a local or development environment, is there anything wrong with looking at your mysql query log? – Chris J Dec 11 '11 at 15:23
  • Chris J, no nothing wrong with that, just don't know mysql enough to do that yet. – Blankman Dec 11 '11 at 15:32

2 Answers2

6

In your jdbc url, you can dynamically add the following property to enable mysql statement logging:

jdbc:mysql://localhost:3306/sakila?profileSQL=true

You can read more about this here:

http://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html

Read up on the properties in the Debugging/Profiling section as a combination of these can be very flexible for log levels, log output etc. (eg. logger)

Another approach is to use a jdbc logging proxy library such as http://code.google.com/p/log4jdbc/

Slava Semushin
  • 14,904
  • 7
  • 53
  • 69
btiernay
  • 7,873
  • 5
  • 42
  • 48
2

JdbcTemplate logs queries at debug level. You should just need to enable debug logging on org.springframework.jdbc.core.JdbcTemplate.

You can also get query logging from your connection pool. For example, if you're using BoneCP, turn on logStatementsEnabled and set your com.jolbox.bonecp logger to log at the debug level. The advantage of using BoneCP's logging is that you get prepared statement placeholder values. Here's the output for the same query showing the difference, where PreparedStatementHandle is a BoneCP class.

[2011-12-11 11:43:25,749] DEBUG Executing prepared SQL query (JdbcTemplate)
[2011-12-11 11:43:25,753] DEBUG Executing prepared SQL statement [select * from player where player_id = ?] (JdbcTemplate)
[2011-12-11 11:43:25,794] DEBUG select * from player where player = 123 (PreparedStatementHandle) 
Emerson Farrugia
  • 11,153
  • 5
  • 43
  • 51