Skip to content

How to print JPA and jdbcTemplate SQL logs in SpringBoot apps?

1. The purpose of this post

This demo shows how to print SQL logs of your DAO when using SpringBoot apps.

2. Environments

  • SpringBoot 1.x and 2.x
  • Java 1.8+

3. JPA solution

Just add this to your application.properties:

application.properties
spring.jpa.show-sql=true

Then you would get this:

Terminal window
Hibernate: select student0_.id as id1_0_, student0_.branch as branch2_0_, student0_.email as email3_0_, student0_.name as name4_0_, student0_.percentage as percenta5_0_, student0_.phone as phone6_0_ from tbl_student student0_ limit ?

You can find detailed documentation of this setting in Data Access of SpringBoot Reference.

4. JdbcTemplate solution

Just add this to your application.properties:

application.properties
logging.level.=ERROR
logging.level.org.springframework.jdbc.core = TRACE

Then you would get this:

Terminal window
2019-05-11 12:11:10.708 DEBUG 8852 --- [nio-8080-exec-1] o.s.jdbc.core.JdbcTemplate : Executing SQL query [select * from tbl_city]

Summary

This post explained how to print SQL logs for both JPA and jdbcTemplate in SpringBoot applications. By configuring the application.properties file, you can easily enable SQL logging for debugging and monitoring purposes. For JPA, setting spring.jpa.show-sql=true displays Hibernate-generated SQL queries, while for jdbcTemplate, setting the logging level to TRACE captures SQL execution logs. These configurations are essential for developers to understand and optimize database interactions in SpringBoot applications.

Final Words + More Resources

My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!