JDBC vs. JPA

Greg L. Turnquist
6 min readAug 3, 2022

In the land of software development, we sure love acronyms. Our acronyms in the land of Java-apps-talking-to-database, there is no shortage there.

And maybe this leads to confusion: What do I use? Everyone else is using JPA? Should I? But I heard about Spring Data JDBC? And what about Spring Data JPA?

In this article, we’ll talk about JDBC, JPA, where they came from, and the tradeoffs that exist.

Once, long ago, there was JDBC

JDBC is the Java Database Connectivity API. This is an old standard, going all the way back to the days of Java 1.1 in 1997. And this API has served us well.

It’s “old data”. Get it?

Everything, and I mean EVERYTHING that talks to databases goes through JDBC. And that has its pros and cons. Basically, JDBC is the Java implementation of the standard dance ALL technologies have played to interact with databases.

  1. Open connection.
  2. Open cursor.
  3. Submit query.
  4. Iterate over result set.
  5. Close result set/cursor.
  6. Close connection.

And we’ve all probably suffered the fate of implementing twenty queries, and forgetting to CLOSE one of those resources along the way. And then, six weeks later, getting a scary stack trace because somehow our database has run out of connections or cursors.

We track down the issue. Patch it. Release it.

And then eight weeks later, we find another. This is what happens when we are on the hook for managing something that shouldn’t be that hard to manage.

And Spring saw this and took note. With one of its most popular and powerful coding patterns, Spring Framework release JdbcTemplate. A template that basically lets you focus on submitting queries and consuming the results.

All resource management was handled by the toolkit. Never again will you get a call at 3:00 AM because the database/your app has blown up.

Okay, maybe you still get those middle of the night calls. But not because you didn’t close a connection!

The thing is, once you’ve written two hundred queries, that amount of raw power capture in those strings becomes kind of…

--

--

Greg L. Turnquist

Sr. Staff Technical Content Engineer at CockroachDB • YouTube Content Creator at https://youtube.com/@ProCoderIO • Best-Selling Author • Coffee Lover