Wrapper to simplify working with JDBC.
This project is at an early development stage and the API will change without backwards compatibility.
This project requires Java 17 or later.
Add dependency to your Gradle project:
dependencies {
implementation 'org.itsallcode:simple-jdbc:0.9.0'
}
Add dependency to your Maven project:
<dependency>
<groupId>org.itsallcode</groupId>
<artifactId>simple-jdbc</artifactId>
<version>0.9.0</version>
</dependency>
See features and API documentation in the API documentation.
See complete example code in ExampleTest.
import org.itsallcode.jdbc.ConnectionFactory;
import org.itsallcode.jdbc.SimpleConnection;
import org.itsallcode.jdbc.Transaction;
import org.itsallcode.jdbc.resultset.batch.BatchInsert;
import org.itsallcode.jdbc.resultset.SimpleResultSet;
import org.itsallcode.jdbc.resultset.generic.Row;
final ConnectionFactory connectionFactory = ConnectionFactory.create(Context.builder().build());
try (SimpleConnection connection = connectionFactory.create("jdbc:h2:mem:", "user", "password")) {
// Use connection...
}
// Define a model record or class
record Name(int id, String name) {
static void setPreparedStatement(final Name row, final PreparedStatement stmt) throws SQLException {
stmt.setInt(1, row.id);
stmt.setString(2, row.name);
}
}
connection.batchInsert(Name.class)
.into("NAMES", List.of("ID", "NAME"))
.rows(Stream.of(new Name(1, "a"), new Name(2, "b"), new Name(3, "c")))
.mapping(Name::setPreparedStatement)
.start();
This allows using batch inserts without creating objects for each row to avoid memory allocations.
try (BatchInsert batch = transaction.batchInsert().into("NAMES", List.of("ID", "NAME")).build()) {
for (int i = 0; i < 5; i++) {
final int id = i + 1;
batch.add(ps -> {
ps.setInt(1, id);
ps.setString(2, "name" + id);
});
}
}
Transaction
implements DbOperations
which provides most of the methods as SimpleConnection
. Transactions will be automatically rolled back during close unless you commit before.
try (Transaction transaction = connection.startTransaction()) {
// Use transaction
// ...
// Commit successful transaction
transaction.commit();
}
try (SimpleResultSet<Row> rs = connection.query("select * from names order by id")) {
final List<Row> result = rs.stream().toList();
assertEquals(3, result.size());
assertEquals(1, result.get(0).get(0).value());
}
try (SimpleResultSet<Name> result = connection.query("select id, name from names where id = ?",
ps -> ps.setInt(1, 2),
(rs, idx) -> new Name(rs.getInt("id"), rs.getString("name")))) {
final List<Name> names = result.stream().toList();
assertEquals(1, names.size());
assertEquals(new Name(2, "b"), names.get(0));
}
./gradlew dependencyUpdates
Install to local maven repository:
./gradlew publishToMavenLocal
To calculate and view test coverage:
./gradlew check jacocoTestReport
open build/reports/jacoco/test/html/index.html
./gradlew javadoc
open build/docs/javadoc/index.html
- Checkout the
main
branch, create a new branch. - Update version number in
build.gradle
andREADME.md
. - Add changes in new version to
CHANGELOG.md
. - Commit and push changes.
- Create a new pull request, have it reviewed and merged to
main
.
- Start the release workflow
- Run command
gh workflow run release.yml --repo itsallcode/simple-jdbc --ref main
- or go to GitHub Actions and start the
release.yml
workflow on branchmain
.
- Update title and description of the newly created GitHub release.
- After some time the release will be available at Maven Central.