Connect a plain Java 21 application to PostgreSQL and H2 with JDBC, then inspect metadata and run prepared statements.
| Field | Detail |
|---|---|
| Repository | jdbc-lab |
| Primary language | Java |
| Topics | database, database-labs, devarent, docker, java, jdbc, lab, postgresql, sql |
| License | MIT |
| Branch | main |
| Maintainer | @devarent |
jdbc-lab is a hands-on repository focused on: Connect a plain Java 21 application to PostgreSQL and H2 with JDBC, then inspect metadata and run prepared statements.. Primary language: Java. Topics: database, database-labs, devarent, docker, java, jdbc, lab, postgresql, sql. The README below documents the project; use the table above for a quick overview.
🇮🇸 Iceland · 🇳🇿 New Zealand · 🇨🇭 Switzerland · 🇸🇮 Slovenia · 🇮🇪 Ireland · 🇦🇹 Austria · 🇵🇹 Portugal · 🇸🇬 Singapore · 🇫🇮 Finland
Connect a plain Java 21 application to PostgreSQL and H2 with JDBC, then inspect metadata and run prepared statements.
Connect a plain Java 21 application to PostgreSQL and H2 with JDBC, then inspect metadata and run prepared statements.
JDBC is the contract between Java applications and relational databases. This lab shows the smallest useful example: one connection, one table, a prepared insert, a query, and the database metadata. It also collects the JDBC URL patterns you need for the other engines in this collection.
docker compose up -d
# requires JDK 21 and Maven 3.9+
mvn -q compile exec:java -Dexec.mainClass=lab.JdbcLab
# or run against H2 without Docker:
JDBC_URL='jdbc:h2:mem:lab;DB_CLOSE_DELAY=-1' JDBC_USER=sa JDBC_PASSWORD='' mvn -q compile exec:java -Dexec.mainClass=lab.JdbcLab| Field | Value |
|---|---|
| PostgreSQL URL | jdbc:postgresql://localhost:5432/lab |
| PostgreSQL user | lab |
| PostgreSQL password | labpassword |
| H2 in-memory URL | jdbc:h2:mem:lab;DB_CLOSE_DELAY=-1 |
| H2 user | sa |
| H2 password | (empty) |
| File | What it shows |
|---|---|
docker-compose.yml |
Engine, ports, and persistent volume. |
.github/workflows/ci.yml |
Builds the Java project with Maven on every push. |
examples/schema.sql |
Table used by the Java example and by the PostgreSQL container on first start. |
src/main/java/lab/JdbcLab.java |
A complete JDBC workflow: connect, create, insert, query, print metadata. |
pom.xml |
Java 21 build with the PostgreSQL and H2 drivers and the exec plugin. |
run.ps1 |
Windows helper. Starts nothing; run docker compose up -d first. |
run.sh |
Linux/macOS helper. Starts nothing; run docker compose up -d first. |
- Change
JDBC_URLto the H2 URL and run the same code without PostgreSQL. - Print
DatabaseMetaData.getDriverName()andgetJDBCMajorVersion(). - Use
addBatch()andexecuteBatch()to insert 100 rows in one round trip. - Try a wrong password and catch
SQLException, printing the SQLState and vendor code.
- Requires JDK 21 and Maven 3.9+. The CI workflow builds with
mvn -B -q -DskipTests package. - The PostgreSQL container creates the
lab_notestable on first start fromexamples/schema.sql. - JDBC URL patterns for other engines are listed in the README table below.
- This is a local lab. Do not use these credentials anywhere public.
This lab publishes its ports on 127.0.0.1 only. To use it from another country, do not open the database port to the internet. Pick one:
- VPS + private tunnel: run the lab on a small Ubuntu VPS, keep the port closed, and connect through WireGuard or
ssh -L. - Managed service: use a managed free tier for this engine and point the same examples at its connection string.
- HTTP UI only: if the engine has a web UI, put it behind Caddy with TLS and basic auth.
The shared checklist, provider map, and commands are in REMOTE-ACCESS.md.
docker compose down -v-v also removes the named volume, so the next start is a clean slate.
Part of the database-labs collection: 26 database engines, one index.
MIT (c) 2026 Arya Fatthurahman. See LICENSE.
Full Tech Stack Wall
Connection names shown in the DBX sidebar - decorative reference badges.