Java + JDBC
Generate Java code using standard JDBC APIs. Works with any JDBC-compatible database including SQLite, DuckDB, PostgreSQL, MySQL, and more.
Overview
Section titled “Overview”| Property | Value |
|---|---|
| Generator | java/sqlite, java/duckdb, or java/postgres |
| Runtime | JVM (Java 17+) |
| API Style | Synchronous |
| Driver | Any JDBC driver |
Installation
Section titled “Installation”Add SQG to your build process and include the appropriate JDBC driver:
# Install SQG globallypnpm add -g @sqg/sqgGradle Dependencies
Section titled “Gradle Dependencies”// SQLiteimplementation 'org.xerial:sqlite-jdbc:3.51.1.0'
// DuckDBimplementation 'org.duckdb:duckdb_jdbc:1.4.2.0'
// PostgreSQLimplementation 'org.postgresql:postgresql:42.7.0'Maven Dependencies
Section titled “Maven Dependencies”<!-- SQLite --><dependency> <groupId>org.xerial</groupId> <artifactId>sqlite-jdbc</artifactId> <version>3.51.1.0</version></dependency>
<!-- DuckDB --><dependency> <groupId>org.duckdb</groupId> <artifactId>duckdb_jdbc</artifactId> <version>1.4.2.0</version></dependency>
<!-- PostgreSQL --><dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.7.0</version></dependency>Example Configuration
Section titled “Example Configuration”version: 1name: my-app
sql: - files: - queries.sql gen: - generator: java/sqlite # or: java/duckdb, java/postgres output: ./src/main/java/com/myapp/db/ config: package: com.myapp.dbThe config.package option specifies the Java package name for generated classes.
Quick Start
Section titled “Quick Start”-
Write your SQL
-- MIGRATE 1CREATE TABLE users (id INTEGER PRIMARY KEY,name TEXT NOT NULL,email TEXT UNIQUE,active INTEGER DEFAULT 1);-- QUERY all_usersSELECT id, name, email, active FROM users ORDER BY name;-- QUERY get_user :one@set id = 1SELECT id, name, email, active FROM users WHERE id = ${id};-- QUERY find_active_users@set active = 1SELECT id, name, email FROM users WHERE active = ${active};-- EXEC create_user@set name = 'John Doe'INSERT INTO users (name, email) VALUES (${name}, ${email});-- EXEC update_user@set id = 1@set name = 'Jane Doe'UPDATE users SET name = ${name}, email = ${email} WHERE id = ${id};-- EXEC delete_user@set id = 1DELETE FROM users WHERE id = ${id}; -
Generate code
Terminal window sqg sqg.yaml -
Use the generated code
import com.myapp.db.MyApp;import java.sql.Connection;import java.sql.DriverManager;public class Main {public static void main(String[] args) throws Exception {// Connect to databaseConnection conn = DriverManager.getConnection("jdbc:sqlite:app.db");// Run migrationsfor (String migration : MyApp.getMigrations()) {conn.createStatement().execute(migration);}// Create query instanceMyApp queries = new MyApp(conn);// Insert data// Query datafor (MyApp.AllUsersRow user : queries.allUsers()) {System.out.println(user.name() + ": " + user.email());}// Get single rowMyApp.GetUserRow user = queries.getUser(1);if (user != null) {System.out.println("Found: " + user.name());}// Update data// Delete dataqueries.deleteUser(2);conn.close();}}
Generated Code Structure
Section titled “Generated Code Structure”The generator creates a single Java file with:
- Record types for each query result
- A class with methods for each query/exec
- Static
getMigrations()method returning migration SQL strings
// ...
public List<AllUsersResult> allUsers() throws SQLException { try ( var stmt = connection.prepareStatement( "SELECT id, name, email, active FROM users ORDER BY name;" ) ) { try (var rs = stmt.executeQuery()) { var results = new ArrayList<AllUsersResult>(); while (rs.next()) { results.add( new AllUsersResult( (Integer) rs.getObject(1), (String) rs.getObject(2), (String) rs.getObject(3), (Integer) rs.getObject(4) ) ); } return results; } } }
// ...Type Mapping
Section titled “Type Mapping”| SQL Type | Java Type |
|---|---|
INTEGER, INT | Integer |
BIGINT | Long |
REAL, DOUBLE | Double |
FLOAT | Float |
TEXT, VARCHAR | String |
BLOB | byte[] |
BOOLEAN | Boolean |
DATE | LocalDate |
TIMESTAMP | LocalDateTime |
TIME | LocalTime |
DECIMAL, NUMERIC | BigDecimal |
UUID | UUID |
All types are nullable (using wrapper classes).