druvu-lib-jdbc

SQL first. Write the statement, bind the parameters, map the row. A thin layer over Spring JDBC where the SQL stays in view instead of behind an ORM.

Spring JDBC comes along as a dependency. Published to GitHub Packages, which needs a one-time token to read. See Install.

View on GitHub


Try it

DbConfig config = DbConfig.of("myDb",
    "jdbc:postgresql://localhost/mydb", "user", "pass",
    "org.postgresql.Driver", "SELECT 1");
DbAccess db = DbAccessFactory.create(config);

List<Map<String, Object>> users = db.select(
    SimpleSql.fromString("SELECT * FROM users WHERE status = ?").with("active"));

Optional<Map<String, Object>> user = db.selectOne(
    SimpleSql.fromString("SELECT * FROM users WHERE id = ?").with(userId));

Named parameters, and a row mapped to your own type:

Optional<User> user = db.selectOne(
    SimpleSql.named("SELECT * FROM users WHERE id = :id")
        .with("id", userId)
        .map((rs, rowNum) -> new User(rs.getInt("id"), rs.getString("name"))));

Two statements in one transaction:

db.runInTransaction(tx -> {
    tx.update(SimpleSql.fromString("UPDATE accounts SET balance = balance - ? WHERE id = ?").with(amount, fromAccount));
    tx.update(SimpleSql.fromString("UPDATE accounts SET balance = balance + ? WHERE id = ?").with(amount, toAccount));
});

What you get

  • positional ? and named :param parameters, bound fluently
  • SQL from a string, a class-path resource or a file; %s includes compose one file from others
  • ??? expands to as many ? as the collection has: an IN clause without string glue
  • lambda row mappers; selectOne returns an Optional and throws on more than one row, selectFirst takes the first
  • rows streamed one at a time for large results
  • transactions with or without a return value; toDebugString() shows the SQL with its values filled in

One thing to know: a loaded SQL file that contains %s is processed as a format string, so a literal % followed by s (LIKE '%smith') must be written %%. Better, pass LIKE patterns as parameters.


Install (GitHub Packages)

Java 21 or newer. GitHub Packages needs Maven authentication even for a public package. Once per machine: a token with the read:packages scope and a server entry in ~/.m2/settings.xml:

<servers>
  <server>
    <id>github</id>
    <username>YOUR_GITHUB_USERNAME</username>
    <password>YOUR_GITHUB_TOKEN</password>
  </server>
</servers>

Then in the project:

<repositories>
    <repository>
        <id>github</id>
        <url>https://maven.pkg.github.com/DenissLarka/druvu-lib-jdbc</url>
    </repository>
</repositories>
<dependency>
    <groupId>com.druvu</groupId>
    <artifactId>druvu-lib-jdbc</artifactId>
    <version>1.0.0</version>
</dependency>

The full reference, every statement form and the utilities, is the README.


Something it almost does?

Open an issue, or write to me. If it is small and useful to more than you, I will often just build it.

Apache License 2.0.


Back to top

Copyright © 2026 druvu.com. All rights reserved.

This site uses Just the Docs, a documentation theme for Jekyll.