Categories
Java SQL

Insert a Java object into a SQL table, with automatic mapping

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

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
Java

Sort a list of objects in Java, in 1 line of code with method reference

In this tutorial, we will explain how to sort a list of Java objects based on a field of the object. Firstly, we will see a method using the method reference operator :: that takes only 1 line of code. Then we will see a more generic method based on a lambda comparator that needs […]

Categories
Java

Extract text between two strings with Java regex

Java is a general purpose language that has a library for almost anything. Using the Java Regex library, you can easily extract the text between two predefined strings. To extract the text between two strings tag1 and tag2 from the string content, we only need 4 lines of code: You can print the variable extracted […]

Categories
Python

Extract text between two strings with Python regex

Python is very light and convenient for manipulating regex. To extract the text between two strings tag1 and tag2 from the string content, we can use the Python regex library. We only need 2 lines of code: pattern = “(?<=tag1).*(?=tag2)” extracted = re.search(pattern, content).group(0) re is the Python regex library, you will need to import […]

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

Categories
Scala

Extract text between two strings with Scala regex

Scala as a very light syntax compared to Java. To extract the text between two strings tag1 and tag2 from the string content, we can use the Scala regex syntax. We only need 2 lines of code: You can print extracted in a main: When you execute the code above, you will get the following […]

Categories
Java

Deploy a Java Tomcat war on AWS with SSL in 5 minutes

In this tutorial we will deploy a Java Tomcat war to AWS Elastic Beanstalk. Also we will plug it to our own domain, with full SSL support. Finally we will add HTTP to HTTPS redirection, so that no unsecure connection to our app can happen. If you want to deploy a Python Django webapp on […]