Logging SQL queries with Spring

If you need to see the SQL executed by Spring jdbc classes, you can use log4j configuration to turn on debug logging level on the org.springframework.jdbc.core package

log4j.category.org.springframework.jdbc.core=DEBUG

This will leave in your log files very useful pieces of information:

  • the SQL
  • the values set for each query parameter
  • the number of rows affected

DEBUG core.JdbcTemplate - Executing SQL update [INSERT INTO analysisResult (ANALYZABLEID, ASIGNEEID, STARTDATE, ENDDATE, CODE, LIMIT, NUMINCIDENCES, NUMLIVEINCIDENCES, NUMLIVEINCIDENCESDAY, NUMLIVEINCIDENCESDAYANYONE) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)]

DEBUG core.StatementCreatorUtils - Setting SQL statement parameter value: column index 1, parameter value [5], value class [java.lang.Long], SQL type -5

DEBUG core.JdbcTemplate - SQL update affected 1 rows

Hopefully, this will save you some minutes someday.

About these ads

  1. 52lemurs

    Thanks. This is exactly what I needed.

  2. Vyas Sanzgiri

    What does category stand for ?

    Can you give a more generic template??

  3. “category” has been replaced by “logger” in recent log4j releases.

  4. lvm

    well, what code are you using to set the values?

  5. Just set the values as usual with the Spring API.

    Anyway, this is a very old post and I haven’t used Spring recently, so things might have changed a lot.

  6. Joe Blow

    Anyone know how to do this with Spring.NET?






Follow

Get every new post delivered to your Inbox.

%d bloggers like this: