Member-only story
If you are starting a new Java project which requires persistence, I strongly recommend you use JPA or Hibernate instead of JDBC Template. But if you find yourself needing to fixing a legacy codebase that requires the use of JDBCTemplate, then this article is for you.
JDBCTemplate was confusing for me because the official documentation doesn’t provide examples, online tutorials were cluttered with XML and spelling mistakes, and there are so many options to pick from such as which query method to use:
By the end of this article, you will learn:
- What is JDBCTemplate
- How to do basic CRUD operations against a database using JDBCTemplate
- How to pick between JDBCTemplate or NamedParameterJDBCTemplate
- What are SQL query params, and how to map them
- How to handle the results of the query
- What are RowMappers, ResultSetExtractors, and other alternatives
- How to troubleshoot errors which aren’t easy to search up online
Prerequisites
- Basic understanding of Java 8
- Already have configured a database and schema to experiment with