Skip to content

Latest commit

 

History

History

README.md

ESQL Examples for opensource COBOL 4J

This directory contains example COBOL programs demonstrating Embedded SQL (EXEC SQL) with PostgreSQL.

Prerequisites

  • opensource COBOL 4J installed
  • PostgreSQL server running
  • A database and user created (defaults: testdb / main_user / password)

Setup

  1. Edit db-config.sh to match your PostgreSQL connection settings:
DB_NAME=testdb
DB_HOST=localhost
DB_PORT=5432
DB_USER=main_user
DB_PASSWORD=password
  1. Make sure the database exists:
createdb testdb

Running the Examples

Run all examples:

make all

Run 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 cursor

inserttbl 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

Examples

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)

How It Works

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.