Skip to main content

SADU

SADU stands for "SQL and damn utilities". It takes the boilerplate out of plain JDBC and still lets you write your own SQL. It is not an ORM like Hibernate or JPA. You keep full control over every query.

What you get:

SADU supports PostgreSQL, MariaDB, MySQL and SQLite.

Requirements​

WhatVersion
Java21 or newer. SADU is compiled for Java 21.
JDBC driverThe driver of your database. SADU does not ship it.
SLF4JSADU logs through slf4j-api and brings it. Add an SLF4J binding (for example slf4j-simple or Logback) to see the log output.

Modules​

SADU is split into modules, so you only add what you need.

ModuleWhat it does
sadu-postgresql, sadu-mariadb, sadu-mysql, sadu-sqliteSupport for one database. Pick at least one. See Databases.
sadu-datasourceBuilds a HikariCP data source. See Data source.
sadu-queriesThe query builder. See Queries.
sadu-mapperMaps rows to objects. The database modules and sadu-queries bring it. See Row mapping.
sadu-updaterDeploys and patches your schema. The database modules bring it. See Updater.
sadu-testingJUnit checks for your updater files. See Test your patch files.
sadu-coreShared code. Every other module brings it.
saduEverything above except sadu-testing, in one artifact.

A database module only holds the parts that are special for that database. It does not include the query builder or the data source builder. Add those modules as well.

Add the dependency​

SADU is published to Maven Central. Snapshots are on EldoNexus (https://eldonexus.de/repository/maven-snapshots/).

This example adds PostgreSQL support, the data source builder and the query builder:

repositories {
mavenCentral()
}

dependencies {
implementation("de.chojo.sadu:sadu-postgresql:2.4.0")
implementation("de.chojo.sadu:sadu-datasource:2.4.0")
implementation("de.chojo.sadu:sadu-queries:2.4.0")
implementation("org.postgresql:postgresql:42.7.14")
}

The sadu artifact contains all modules except sadu-testing. It is handy to try things out. In a real project, add only the modules you use.

A first look​

Create a data source, configure the query builder once and run a query:

HikariDataSource dataSource = DataSourceCreator.create(PostgreSql.get())
.configure(config -> config
.host("localhost")
.port(5432)
.database("app")
.user("app")
.password("secret"))
.create()
.build();

QueryConfiguration.setDefault(QueryConfiguration.builder(dataSource).build());

Optional<String> name = Query.query("SELECT name FROM users WHERE id = ?")
.single(Call.of().bind(42))
.map(row -> row.getString("name"))
.first();

SADU opens the connection, sets the parameter, reads the result and closes everything again.

Where to go next​

  • Databases: the database modules and their column types.
  • Data source: build a HikariCP connection pool.
  • Queries: read and write data with the query builder.
  • Row mapping: turn rows into your own classes.
  • Updater: set up and patch your database schema.
  • Javadoc: every class and method.