In this tutorial, we will see a simple method to insert a Java object as a row into a SQL table. Thanks to Apache DB Utils and Apache Bean Utils, our mapping between Java JDBC and SQL will be automatic. 1) The Java object Let’s consider the following Java class Customer: (@AllArgsConstructor and @Data are annotations from the Lombok […]
Category: SQL
In this tutorial, we will explain how to run PostgreSQL with Java and Testcontainers. Testcontainers is an awesome library that allows you to configure and start docker containers from Java. To run a PostgreSQL database, you need Docker on your machine. Add these dependencies to your pom.xml: Then, simply run: Replace databaseName, username and password […]
In this tutorial we will see how to very simply transform a JDBC result set into a list of Java objects. Let’s say you have the following SQL table customer: On the Java side, you have the following POJO called Customer: @NoArgsConstructor, @AllArgsConstructor and @Data are annotations from the Lombok project. They add constructors, getters and setters to the […]

It is very common to use a UUID as primary key for a SQL table. You can generate a unique random UUID on the client side and then insert it into the database. However, there is a simpler way: you can ask the database to automatically generate it on every insert. With PostgreSQL, it is […]

In this tutorial, we will explain how to easily limit the size of a PostgreSQL table (by size we mean number of rows). To achieve this, we will create a trigger on the table via Java JDBC. The trigger will be atomically executed after each SQL insert, and the insert that causes the trigger to […]