Follow

Dynamically Connect to a database from EBP?

You use the same techniques for dynamically connecting to a database from a COBOL program within EBP as you do on a mainframe JES/JCL environment, using the following components

  • IKJEFT01 -- TSO command line interpreter
  • DSNTIAD -- Database command line interpreter
  • COBOL -- Pre-opened, default database connection

Here's a sample program broken into annotated steps.  Recombine or use the attached JCL deck to run as a single job.

Step 1:  pre-define or create the IKJEFT.properties file that defines connection information to the IKJEFT01 (TSO) command.  See the EBP Standard Utilities Programmers Reference Manual.

//* setup required: Postgres server running locally with user
//* 'postgres' and password 'abc000' and previously created 'tickets'
//* EBP configured with Postgres
//* driver in one of the classlib1..9 configurations
//* create a properties file with postgres connect info
//INIT01 EXEC PGM=IEFBR14
//D01 DD DSN=IKJEFT.properties,DISP=(,DELETE)
//INIT02 EXEC PGM=IEBGENER
//SYSUT2 DD DSN=IKJEFT.properties,DISP=NEW
//SYSUT1 DD *
# Heirloom Computing - Elastic Batch Platform - IKJEFT Properties
P003.url=jdbc:postgresql://localhost/tickets
P003.user=postgres
P003.password=abc000
/*

 

Step 2: Pre-compile or use the following steps to compile a COBOL module (Java JAR file) that contains COBOL programs to create a table and load a table.  These programs are "linked" into the MAMMOTH.JAR library used in later steps.

//* run the Elastic COBOL Compiler on a program that creates
//* table test40
//EC01 EXEC PROC=HCECOBOL,PGMID=CREATEDB,LINK=NO
//* :....1....:....2....:....3....:....4....:....5....:....6....:....7.
//COPY.SYSUT1 DD *
01     IDENTIFICATION DIVISION.
02     PROGRAM-ID. CREATEDB.
03    *
04    * create table TEST40 with 1 INT column
05    *
06    * NOTE: assume IKJEFT01 has connected the DEFAULT database
07    *
08     Working-storage section.
09     EXEC SQL INCLUDE SQLCA END-EXEC.
10
11     procedure division.
12     main-0000.
13     display "TEST40 - create a table" upon sysout.
14    *
15    * create the table
16     EXEC SQL
17     create table test40(a integer)
18     END-EXEC
19     IF SQLCODE NOT = 0 THEN
20         display "FAILURE on create table:"
21             SQLCODE " " SQLERRM upon sysout
22         stop run
23     end-if.
24    *
25    * commit
26     EXEC SQL
27     commit work
28     END-EXEC
29     IF SQLCODE NOT = 0 THEN
30         display "FAILURE on commit work:"
31             SQLCODE " " SQLERRM upon sysout
32         stop run
33     end-if.
34     display "PASS" upon sysout.
35     end program.
/*
//COMPILE.SYSOUT DD SYSOUT=*
//*
//* run the Elastic COBOL Compiler on a subprogram
//* that loads the table test40
//EC02 EXEC PROC=HCECOBOL,PGMID=LOADSUB,LINK=NO
//* :....1....:....2....:....3....:....4....:....5....:....6....:....7.
//COPY.SYSUT1 DD *
01     IDENTIFICATION DIVISION.
02     PROGRAM-ID. LOADSUB.
03    *
04    * load table TEST40 with a few records
05    *
06    * NOTE: assume IKJEFT01 has connected the DEFAULT database
07    *
08     Working-storage section.
09     EXEC SQL INCLUDE SQLCA END-EXEC.
10
11     procedure division.
12     sub-0000.
13    * display "TEST40 - load a table" upon sysout.
14    *
15    * insert into the table
16     EXEC SQL
17     insert into test40 values(1)
18     END-EXEC
19     IF SQLCODE NOT = 0 THEN
20         display "FAILURE on insert table:"
21             SQLCODE " " SQLERRM upon sysout
22         stop run
23     end-if.
24    *
25    * insert into the table
26     EXEC SQL
27     insert into test40 values(2)
28     END-EXEC
29     IF SQLCODE NOT = 0 THEN
30         display "FAILURE on insert table:"
31             SQLCODE " " SQLERRM upon sysout
32         stop run
33     end-if.
34    *
35    * commit
36     EXEC SQL
37     commit work
38     END-EXEC
39     IF SQLCODE NOT = 0 THEN
40         display "FAILURE on commit work:"
41             SQLCODE " " SQLERRM upon sysout
42         stop run
43     end-if.
44    * end.
45     end program.
/*
//COMPILE.SYSOUT DD SYSOUT=*
//*
//* run the Elastic COBOL Compiler on a program that
//* calls the subprogram that loads the table test40
//EC03 EXEC PROC=HCECOBOL,PGMID=LOADDB,LINK=YES
//* :....1....:....2....:....3....:....4....:....5....:....6....:....7.
//COPY.SYSUT1 DD *
01     IDENTIFICATION DIVISION.
02     PROGRAM-ID. LOADDB.
03    *
04    * load table TEST40 with a few records
05    *
06    * NOTE: assume IKJEFT01 has connected the DEFAULT database
07    *
08     Working-storage section.
10
11     procedure division.
12     main-0000.
13     display "TEST40 - load a table" upon sysout.
14    *
15    * call the real loading subprogram
16     call 'LOADSUB'
17    *
18    * finished
19     display "PASS" upon sysout.
20     end program.
/*
//COMPILE.SYSOUT DD SYSOUT=*
//LINK.SYSOUT DD DISP=NEW,DSN=HCI00.MAMMOTH.JAR

 

Step 3: run the programs to create the table and load data into it.  The TSO utility IKJEFT1B uses the DSN command to set up the default (SYSTEM) database connection that maps to database P003, itself a synonym for connection attributes resident in the IKJEFT.properties file.  Then, the RUN PROGRAM TSO command will invoke CREATEDB or LOADB programs from the library linked earlier.  Programs can also be in a STEPLIB or JOBLIB.

//* run the CREATE COBOL program from IKJEFT1B
//* NOTE: IKJEFT1B provides the LIB() keyword on
//* the RUN command to provide a LOADLIB path
//* in which to find its program (CREATEDB)
//RUN1 EXEC PGM=IKJEFT1B
//SYSOUT DD SYSOUT=*
//SYSTSPRT DD SYSOUT=*
//SYSTSIN DD *
DSN SYSTEM(P003)
RUN PROGRAM(CREATEDB) LIB('HCI00.MAMMOTH.JAR')
END
/*
//*
//* run the LOAD COBOL program from IKJEFT1B
//* NOTE: STEPLIB/JOBLIB can also be used to
//* find the program (LOADDB) invoked by
//* the TSO RUN command
//RUN2 EXEC PGM=IKJEFT1B
//STEPLIB DD DISP=SHR,DSN=HCI00.MAMMOTH.JAR
//SYSOUT DD SYSOUT=*
//SYSTSPRT DD SYSOUT=*
//SYSTSIN DD *
DSN SYSTEM(P003)
RUN PROGRAM(LOADDB) LIB('HCI00.MAMMOTH.JAR')
END
/*

 

Step 4:  A final, optional, step can again use DSNTIAD to list out the rows of that table.

//* print out the rows added by the COBOL program
//DB02 EXEC PGM=IKJEFT1B
//SYSPRINT DD SYSOUT=*
//SYSTSPRT DD SYSOUT=*
//SYSTSIN DD *
DSN SYSTEM(P003)
RUN PROGRAM(DSNTIAD)
END
/*
//SYSIN DD *
select * from test40;
/*

 

 

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.
Powered by Zendesk