sql2o

repository·master·Indexed 22 days ago

https://github.com/aaberg/sql2o

A 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.

Tokens
1.2K
Snippets
3
Records
5
Agent score
28%

What's inside sql2o

  1. Overview of Sql2o

    master
    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.
  2. Check Java version compatibility for Sql2o

    master

    Ensure 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.
  3. Quick Start Example with Sql2o

    master

    This example demonstrates how to initialize a Sql2o instance, open a Connection, execute an update (DDL/DML), and fetch a single object using named parameters and POJO mapping.

    Key steps:

    1. Instantiate Sql2o with the JDBC URL, username, and password.
    2. Open a Connection using a try-with-resources block.
    3. Use createQuery(sql) to prepare statements.
    4. Use .addParameter(name, value) to bind named parameters (e.g., :id).
    5. 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;
    }
  4. Configure the Oracle XE 21c database service

    master

    The oracle-xe-21c service uses the container-registry.oracle.com/database/express:latest image. It includes a persistent volume for data and exposes ports 1521 and 5500.

    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:
  5. Configure the Postgres database service

    master

    The postgres-db service uses the postgres:latest image. It is configured to map host port 15432 to container port 5432. 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: