This directory contains example COBOL programs demonstrating Embedded SQL (EXEC SQL) with PostgreSQL.
- opensource COBOL 4J installed
- PostgreSQL server running
- A database and user created (defaults:
testdb/main_user/password)
- Edit
db-config.shto match your PostgreSQL connection settings:
DB_NAME=testdb
DB_HOST=localhost
DB_PORT=5432
DB_USER=main_user
DB_PASSWORD=password- Make sure the database exists:
createdb testdbRun all examples:
make allRun a specific example:
make sample # Basic INSERT/SELECT
make cursor # Cursor-based row iteration
make prepare # Prepared statements
make inserttbl # Populate the EMP table (run before fetchtbl)
make fetchtbl # Fetch and display the EMP table via a cursorinserttbl and fetchtbl share the EMP table, so inserttbl must run
first. make all already runs them in the correct order.
Clean up generated files:
make clean| File | Description |
|---|---|
sample.cbl |
Basic CONNECT, CREATE TABLE, INSERT, SELECT INTO, DROP, DISCONNECT |
cursor.cbl |
DECLARE CURSOR, OPEN, FETCH loop, CLOSE |
prepare.cbl |
PREPARE statement, EXECUTE with USING |
INSERTTBL.cbl |
Create the EMP table and INSERT rows with literals and host variables |
FETCHTBL.cbl |
SELECT COUNT(*), then fetch the EMP table via a cursor and display it (run after INSERTTBL) |
The COBOL source files contain placeholders like <|DB_NAME|> for database connection details. The Makefile copies each source file to a *_run.cbl file, replaces the placeholders with values from db-config.sh, then compiles and runs the program.
For more details on Embedded SQL in opensource COBOL 4J, see the ESQL Guide.