Categories
Docker Java SQL

Run PostgreSQL with Java and Testcontainers

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 […]

Categories
Java SQL

Map JDBC result set to Java list of objects in 2 lines

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 […]

Categories
SQL

Generate a random UUID primary key during insert in PostgreSQL

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 […]

Categories
Java SQL

Limit PostgreSQL table size with a trigger and Java JDBC

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 […]