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:
- a data source builder for HikariCP connection pools,
- a query builder that handles statements, parameters, results and exceptions,
- row mapping from result rows to your own classes,
- an updater that sets up and patches your database schema from SQL files,
- checks for your patch files in your unit tests.
SADU supports PostgreSQL, MariaDB, MySQL and SQLite.
Requirements
| What | Version |
|---|---|
| Java | 21 or newer. SADU is compiled for Java 21. |
| JDBC driver | The driver of your database. SADU does not ship it. |
| SLF4J | SADU 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.
| Module | What it does |
|---|---|
sadu-postgresql, sadu-mariadb, sadu-mysql, sadu-sqlite | Support for one database. Pick at least one. See Databases. |
sadu-datasource | Builds a HikariCP data source. See Data source. |
sadu-queries | The query builder. See Queries. |
sadu-mapper | Maps rows to objects. The database modules and sadu-queries bring it. See Row mapping. |
sadu-updater | Deploys and patches your schema. The database modules bring it. See Updater. |
sadu-testing | JUnit checks for your updater files. See Test your patch files. |
sadu-core | Shared code. Every other module brings it. |
sadu | Everything 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:
- Gradle (Kotlin DSL)
- Gradle (Groovy)
- Maven
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")
}
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'
}
<dependencies>
<dependency>
<groupId>de.chojo.sadu</groupId>
<artifactId>sadu-postgresql</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>de.chojo.sadu</groupId>
<artifactId>sadu-datasource</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>de.chojo.sadu</groupId>
<artifactId>sadu-queries</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.7.14</version>
</dependency>
</dependencies>
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.