Skip to content

bowbahdoe/jdbc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JDBC

Utilities for working with the raw JDBC api.

Includes

  • Utilities for reading data from ResultSets
  • An UncheckedSQLException for when throwing a SQLException is inconvenient, but might need to be recovered later.
  • A SettableParameter interface, for when String Templates are re-previewed.

Dependency Information

Maven

<dependency>
    <groupId>dev.mccue</groupId>
    <artifactId>jdbc</artifactId>
    <version>2024.12.20</version>
</dependency>

Gradle

dependencies {
    implementation("dev.mccue:jdbc:2024.12.20")
}

Usage

These examples use sqlite.

Read nullable primitive types

ResultSets includes helpers for reading potentially null primitive types from a ResultSet

import dev.mccue.jdbc.ResultSets;

void main() throws Exception {
    var db = new SQLiteDataSource();
    db.setUrl("jdbc:sqlite:test.db");

    try (var conn = db.getConnection()) {
        try (var stmt = conn.prepareStatement("""
                SELECT number
                FROM widget
                LIMIT 1
                """)) {
            var rs = stmt.executeQuery();

            // Methods exist for all primitives except char 
            // (which doesn't have a method on ResultSet)
            var number = ResultSets.getIntegerNullable(rs, "number");
        }
    }
}

Read non-null primitive types

If you want to read a column that is primitive, but you assume is not null, there are helpers which will throw a SQLException early if that assumption is violated.

import dev.mccue.jdbc.ResultSets;

void main() throws Exception {
    var db = new SQLiteDataSource();
    db.setUrl("jdbc:sqlite:test.db");

    try (var conn = db.getConnection()) {
        try (var stmt = conn.prepareStatement("""
                SELECT number
                FROM widget
                LIMIT 1
                """)) {
            var rs = stmt.executeQuery();

            // Methods exist for all primitives except char 
            // (which doesn't have a method on ResultSet)
            var number = ResultSets.getIntegerNotNull(rs, "number");
        }
    }
}

Read a row as a Record

Often when going through a ResultSet you will want to materialize a whole row.

import dev.mccue.jdbc.ResultSets;

public record Widget(int number) {}

void main() throws Exception {
    var db = new SQLiteDataSource();
    db.setUrl("jdbc:sqlite:test.db");

    try (var conn = db.getConnection()) {
        try (var stmt = conn.prepareStatement("""
                SELECT number
                FROM widget
                LIMIT 1
                """)) {
            var rs = stmt.executeQuery();
            var widget = ResultSets.getRecord(rs, Widget.class);

            System.out.println(widget);
        }
    }
}

Read a row as a Record with customized mappings

If the name of a record component doesn't line up with what you want pulled from a ResultSet, you can use the @Column annotation.

import dev.mccue.jdbc.Column;
import dev.mccue.jdbc.ResultSets;

public record Widget(@Column(label = "number") int n) {
}

void main() throws Exception {
    var db = new SQLiteDataSource();
    db.setUrl("jdbc:sqlite:test.db");

    try (var conn = db.getConnection()) {
        try (var stmt = conn.prepareStatement("""
                SELECT number
                FROM widget
                LIMIT 1
                """)) {
            var rs = stmt.executeQuery();
            var widget = ResultSets.getRecord(rs, Widget.class);

            System.out.println(widget);
        }
    }
}