sql2o
repository·master·Indexed 22 days ago
https://github.com/aaberg/sql2oA lightweight Java library that simplifies database interactions by mapping SQL query results directly to POJOs or Java Records. Unlike full ORMs, sql2o does not generate SQL, instead reducing boilerplate code for custom queries. Version 1.9.0+ requires Java 17, while 1.8.x and 1.6.x are compatible with Java 11 and Java 8 respectively.
What's inside sql2o
- Sql2o is a lightweight Java library designed to simplify database interaction. It provides automatic mapping of query results into POJO (Plain Old Java Object) objects. Unlike full ORMs, Sql2o does not generate SQL for you; it is intended to be used alongside your own SQL queries to reduce boilerplate code.
Check Java version compatibility for Sql2o
masterEnsure your Java runtime matches the version of Sql2o you are using:
- Sql2o 1.9.0 and newer: Requires Java 17 or later (adds support for Java Records).
- Sql2o 1.8.x: Compatible with Java 11.
- Sql2o 1.6.x: Compatible with Java 8.
Quick Start Example with Sql2o
masterThis example demonstrates how to initialize a
Sql2oinstance, open aConnection, execute an update (DDL/DML), and fetch a single object using named parameters and POJO mapping.Key steps:
- Instantiate
Sql2owith the JDBC URL, username, and password. - Open a
Connectionusing a try-with-resources block. - Use
createQuery(sql)to prepare statements. - Use
.addParameter(name, value)to bind named parameters (e.g.,:id). - Use
.executeUpdate()for changes or.executeAndFetchFirst(Class)to map a single result to a POJO.
import org.sql2o.*; public class Main { public static void main(String[] args) { String url = "jdbc:h2:mem:test"; // Example using H2 in-memory database try (Sql2o sql2o = new Sql2o(url, "username", "password"); Connection con = sql2o.open()) { con.createQuery("CREATE TABLE users (id INTEGER PRIMARY KEY, name VARCHAR(50))").executeUpdate(); con.createQuery("INSERT INTO users (id, name) VALUES (:id, :name)") .addParameter("id", 1) .addParameter("name", "Alice") .executeUpdate(); User user = con.createQuery("SELECT * FROM users WHERE id = :id") .addParameter("id", 1) .executeAndFetchFirst(User.class); System.out.println("User: " + user.name); } } } class User { public int id; public String name; }- Instantiate
Configure the Oracle XE 21c database service
masterThe
oracle-xe-21cservice uses thecontainer-registry.oracle.com/database/express:latestimage. It includes a persistent volume for data and exposes ports1521and5500.Key environment variables:
ORACLE_PWD: Sets the Oracle password.ORACLE_CHARACTERSET: Sets the database character set (e.g.,AL32UTF8).
oracle-xe-21c: image: 'container-registry.oracle.com/database/express:latest' volumes: - oracle-xe-21c-volume:/opt/oracle/oradata ports: - "1521:1521" - "5500:5500" environment: - ORACLE_PWD=testpassword - ORACLE_CHARACTERSET=AL32UTF8 networks: sql2o-network:Configure the Postgres database service
masterThe
postgres-dbservice uses thepostgres:latestimage. It is configured to map host port15432to container port5432. Default credentials and environment settings are provided via environment variables.services: postgres-db: image: 'postgres:latest' ports: - 15432:5432 environment: - POSTGRES_USER=testuser - POSTGRES_PASSWORD=testpassword networks: sql2o-network: