PROCESS PGMNAME(MIXED),XREF(FULL),VBREF,MAP
       Identification Division.
       Program-ID. "EmpList_List_Employees".
       Environment Division.
       Input-Output Section.
       File-Control.
           Select Emp-File Assign to EmpFile
            Organization is Indexed
            Access is Sequential
            Record Key is Emp-SSN
            File Status is WS-File-Status.
       Data Division.
       File Section.
       FD  Emp-File
           Data Record Emp-Record.
       01  Emp-Record.
           05  Emp-SSN                 PIC X(9).
           05  Emp-Name                PIC X(20).
           05  Emp-DOH                 PIC 99/99/9999.
           05  Emp-Position-Code       PIC 9(3).
           05  Emp-Title               PIC X(20).
           05  Emp-Dept-Code           PIC X(3).
           05  Emp-Dept-Title          PIC X(20).
           05  Emp-Location-Code       PIC X(3).
       Working-Storage Section.
       77  WS-File-Status              PIC X(2).
           88  IO-OK                   Value '00'.
       77  WS-EOF-Flag                 PIC 9 Usage Comp-4.
           88  Not-EOF                 Value 0.
           88  EOF-reached             Value 1.
       77  NumberOfRecords             PIC 9(8) Usage Comp-4.
       Linkage Section.
      *   Each non-static method in Java has a this reference
      *   which is an implicit parameter to the method.  Don't
      *   mess with this.
       01  this                        PIC 9(8).
      *   If method call was successful, the number of records
      *   retrieved will be returned to caller.  Otherwise,
      *   0 will be returned.
       01  LS-Result                   PIC 9(8) Usage Comp-4.
           88  Request-Failed          Value 0.
       01  LS-Emp-Reference            Pointer.
       01  LS-Emp-Record.
           05  Employee Occurs 10.
             10  LS-Emp-SSN            PIC X(9).
             10  LS-Emp-Name           PIC X(20).
             10  LS-Emp-DOH            PIC 99/99/9999.
             10  LS-Emp-Position-Code  PIC 9(3).
             10  LS-Emp-Title          PIC X(20).
             10  LS-Emp-Dept-Code      PIC X(3).
             10  LS-Emp-Dept-Title     PIC X(20).
             10  LS-Emp-Location-Code  PIC X(3).
       Procedure Division Using this LS-Emp-Reference
                          Returning LS-Result.

           Set IO-OK to True
           Open Input Emp-File
           If IO-OK Then
              Move 0 to NumberOfRecords
              Set Not-EOF to True
              Set Address of LS-Emp-Record to LS-Emp-Reference
              Read Emp-File
                 At End Set EOF-Reached to True
              End-Read
              Perform Until EOF-Reached OR NumberOfRecords > 10
      
                 Add 1 to NumberOfRecords
                 Move Emp-Record to Employee (NumberOfRecords)
                 Read Emp-File
                    At End Set EOF-Reached to True
                 End-Read
              End-Perform
              Close Emp-File
              Move NumberOfRecords to LS-Result
           Else
              Set Request-Failed to True
           End-If
           GoBack.
       End Program "EmpList_List_Employees".

Listing 3. ListEmp.cbl

Return to Article


/**
 * JavaToCOBOL - illustrates the calling of a VisualAge for COBOL
 *               program to retrieve records from a COBOL
 *               indexed file.
 *
 * @version 1.00 28 January 1997
 * @author Wilbert T. Kho
 */


public class JavaToCOBOL
{
   public static final int work_length = 88;
  
   public static void main(String args[])
   {
      int result, i, j, k;
      byte work_area[] = new byte[10*work_length];
      EmpList anEmployeeList = new EmpList();
      System.out.println("In JavaToCOBOL");
      result = anEmployeeList.List_Employees(work_area);
      System.out.println("Number of records returned: " + result);
      System.out.println();
      int number_bytes = result * work_length;
      for (i = 0; i < result; i++)
      {
        System.out.print("\n\nRecord: " + i);
        k = 0;
        for (j = i * work_length; j < ( (i * work_length) + work_length); j++)
        {
            switch (k) {
             case 0: System.out.print("\nSSN: "); break;
             case 9: System.out.print("\nName: "); break;
             case 29: System.out.print("\nDate of Hire: "); break;
             case 39: System.out.print("\nPosition Code: "); break;
             case 42: System.out.print("\nTitle: "); break;
             case 62: System.out.print("\nDept. "); break;
             case 85: System.out.print("\nLocation: "); break;
            }
            System.out.print( (char) work_area[j]);
            k++;
        }
      };
      System.out.println();
   }
}

Listing 4. JavaToCobol.java

Return to Article


/**
 * JavaToCOBOLApplet - illustrates the calling of a VisualAge for COBOL
 *                     program to retrieve records from a COBOL indexed
 *                     file and display the data in a browser.
 *
 * @written using JDK 1.1 Beta 3
 *
 * @version 1.00 1 February 1997
 * @author Wilbert T. Kho
 */

import java.awt.*;
import java.util.*;
import java.applet.Applet;
import java.awt.event.*;

public class Java2COBOLApplet extends Applet
                              implements ActionListener
{
   public static final int work_length = 88;
   public static final int ssn_length = 9;
   public static final int name_length = 20;
   public static final int doh_length = 10;
   public static final int pos_code_length = 3;
   public static final int title_length = 20;
   public static final int dept_code_length = 3;
   public static final int dept_title_length = 20;
   public static final int loc_code_length = 3;
   public static final int dept_length = dept_code_length+dept_title_length+1;
   
   int result, currentRecord;
   byte work_area[];
   EmpList anEmployeeList;
   
   Label ssnl, namel, dohl, poscodel, titlel, deptl, locl;
   TextField ssn, name, doh, poscode, title, dept, loc;
   Button nextButton, quitButton;
   StringBuffer work_stringBuffer = new StringBuffer(dept_code_length + dept_title_length);
  
   public void init() {
      Font f = new Font("Times Roman",Font.BOLD,20);
      setLayout(new BorderLayout());
      
      Label welcomel = new Label("Welcome to the Java to COBOL Center");
      welcomel.setFont(f);
      Panel s = new Panel();
      s.setLayout(new FlowLayout());
      s.add(welcomel);       
      add("North",s);
      
      ssnl = new Label("SSN: ");
      namel = new Label("Name: ");
      dohl = new Label("Date of Hire: ");
      poscodel = new Label("Position: ");
      titlel = new Label("Title: ");
      deptl = new Label("Department: ");
      locl = new Label("Location: ");
      ssn = new TextField(ssn_length);
      name = new TextField(name_length);
      doh = new TextField(doh_length);
      poscode = new TextField(pos_code_length);
      title = new TextField(title_length);
      dept = new TextField(dept_length);
      loc = new TextField(loc_code_length);
      ssn.setEditable(false);
      name.setEditable(false);
      doh.setEditable(false);      
      poscode.setEditable(false);
      title.setEditable(false);
      dept.setEditable(false);
      loc.setEditable(false);
      
      Panel t = new Panel();
      t.setLayout(new GridLayout(7,2,8,8));
      t.add(ssnl);
      t.add(ssn);
      t.add(namel);
      t.add(name);
      t.add(dohl);
      t.add(doh);
      t.add(poscodel);
      t.add(poscode);
      t.add(titlel);
      t.add(title);
      t.add(deptl);
      t.add(dept);
      t.add(locl);
      t.add(loc);
      add("Center",t);
             
      nextButton = new Button("Next");
      nextButton.addActionListener(this);
      Panel control = new Panel();
      control.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
      control.add(nextButton);
      add("South",control);

      work_area = new byte[10*work_length];
      anEmployeeList = new EmpList();
      System.out.println("In JavaToCOBOL");
      result = anEmployeeList.List_Employees(work_area);
      System.out.println("Number of records returned: " + result);
      System.out.println();
      
      currentRecord = 0;
      fillText(currentRecord);      
   }
   
   /** 
    * fills the text fields with COBOL record information
    * @param recordNumber - the nth record to be displayed
    */
   private void fillText(int recordNumber) {
      int i, j, k, offset;
      k = recordNumber * work_length;
      for (i = k; i < (k + work_length);)
      {
       if (k == 0) {
          offset = i;
       }
       else {
          offset = i%k;
       }
       switch(offset) {
         case 0:
           work_stringBuffer.setLength(ssn_length);
           for (j = 0; j < ssn_length; j++, i++)
             work_stringBuffer.setCharAt(j, (char) work_area[i]);
           ssn.setText(work_stringBuffer.toString());
           break;
         case 9:
           work_stringBuffer.setLength(name_length);
           for (j = 0; j < name_length; j++, i++)
             work_stringBuffer.setCharAt(j, (char) work_area[i]);
           name.setText(work_stringBuffer.toString());
           break;
         case 29:
           work_stringBuffer.setLength(doh_length);
           for (j = 0; j < doh_length; j++, i++)
             work_stringBuffer.setCharAt(j, (char) work_area[i]);
           doh.setText(work_stringBuffer.toString());
           break;
         case 39:
           work_stringBuffer.setLength(pos_code_length);
           for (j = 0; j < pos_code_length; j++, i++)
             work_stringBuffer.setCharAt(j, (char) work_area[i]);
           poscode.setText(work_stringBuffer.toString());
           break;  
         case 42:
           work_stringBuffer.setLength(title_length);
           for (j = 0; j < title_length; j++, i++)
             work_stringBuffer.setCharAt(j, (char) work_area[i]);
           title.setText(work_stringBuffer.toString());
           break;  
         case 62:
           work_stringBuffer.setLength(dept_length);
           for (j = 0; j < dept_code_length; j++, i++)
             work_stringBuffer.setCharAt(j, (char) work_area[i]);
           work_stringBuffer.insert(j, "/");
           for (++j; j < dept_length; j++, i++)
             work_stringBuffer.setCharAt(j, (char) work_area[i]);
           dept.setText(work_stringBuffer.toString());
           break;  
         case 85:
           work_stringBuffer.setLength(loc_code_length);
           for (j = 0; j < loc_code_length; j++, i++)
             work_stringBuffer.setCharAt(j, (char) work_area[i]);
           loc.setText(work_stringBuffer.toString());
           break;
         default:
           break;
        }   
       }
   }
   
   public void actionPerformed(ActionEvent event) {
      Object source = event.getSource();
      if (source == nextButton) { 
         currentRecord++;
         if (currentRecord >= result) 
            currentRecord = 0;
         fillText(currentRecord);        
      }
      if (source == quitButton)
         System.exit(0);
   }
   
   public static void main(String args[])
   {
      Frame f = new Frame("Java to COBOL Example");
      Java2COBOLApplet j2cob = new Java2COBOLApplet();
      j2cob.init();
      f.add("Center",j2cob);
      f.pack();
      f.resize(f.preferredSize());
      f.show();     
   }
}

Listing 5. Java2CobolApplet.java

Return to Article


javacob.gif

Figure 1

Return to Article


# Makefile for Java calls COBOL example on Windows 95

JAVA_HOME    = C:\jdk11\java    # Java home directory
MAINPROG     = JavaToCOBOL      # .java file to define main program
APPLETPROG   = Java2COBOLApplet # .java file to define applet
NEWCLASS     = EmpList          # .java file to define class for native methods
CBL_PROGRAM  = ListEmp          # .cbl file to define native method
CREATEFILE   = CreatEmp         # .cbl file to create the COBOL indexed file


#  this is what will be created
all:    $(MAINPROG).class $(APPLETPROG).class $(NEWCLASS).class $(NEWCLASS).dll

LIBDIR       = $(JAVA_HOME)\lib
JAVAH   = javah -classpath $(LIBDIR)\classes.zip;.

CFLAGS       =  /Mc /q+   \
                -I$(JAVA_HOME)\include -I$(JAVA_HOME)\include\win32

COB_FLAGS = -qlib -qnoexit -qnoadata -qthread $(COBDEBUG)

all:    $(MAINPROG).class $(NEWCLASS).class $(NEWCLASS).dll

.SUFFIXES:      .cbl .java .class

.java.class:
        javac $<

$(CBL_PROGRAM).obj: $(CBL_PROGRAM).cbl
        cob2 -qentryint(cdecl) $(COB_FLAGS) -c $(CBL_PROGRAM).cbl

.c.obj:
        icc $(CFLAGS) -c $<


$(NEWCLASS).c:  
        $(JAVAH) -stubs $(NEWCLASS)

$(NEWCLASS).dll: $(CBL_PROGRAM).obj $(NEWCLASS).c
        icc -c $(CFLAGS) /Ge- $(NEWCLASS).c
        cob2 -dll $(NEWCLASS).obj $(CBL_PROGRAM).obj \
                $(LIBDIR)\javai.lib

clean:
   erase  *.exe
   erase  *.lst
   erase  *.class
   erase  $(NEWCLASS).c
   erase  $(NEWCLASS).h
   erase  *.obj
   erase  *.exp
   erase  *.lib
   erase  *.dll

runit:
        set EmpFile=EmpFile.dat
        java $(MAINPROG)
        
viewit:
        set EmpFile=EmpFile.dat 
        appletviewer $(APPLETPROG).HTM
        
seedit:
        cob2 $(COB_FLAGS) $(CREATEFILE).cbl
        set EmpFile=EmpFile.dat
        set EmpSeed=Empseed.txt
        $(CREATEFILE)

Listing 6. Makefile

Return to Article


PROCESS XREF(FULL),VBREF,MAP
       Identification Division.
       Program-ID.  CreatEmp.
      *Author.  Wilbert Kho
       Environment Division.
       Input-Output Section.
       File-Control.
           Select Emp-File Assign to EmpFile
            Organization is Indexed
            Access is Sequential
            Record Key is Emp-SSN of Emp-Record
            File Status is WS-File-Status.

           Select Emp-Seed Assign to EmpSeed
            Organization is Line Sequential.
       Data Division.
       File Section.
       FD  Emp-File
           Data Record Emp-Record.
       01  Emp-Record.
           05  Emp-SSN                 PIC X(9).
           05  Emp-Name                PIC X(20).
           05  Emp-DOH                 PIC 99/99/9999.
           05  Emp-Position-Code       PIC 9(3).
           05  Emp-Title               PIC X(20).
           05  Emp-Dept-Code           PIC X(3).
           05  Emp-Dept-Title          PIC X(20).
           05  Emp-Location-Code       PIC X(3).
       FD  Emp-Seed
           Data Record Seed-Record.
       01  Seed-Record.
           05  Emp-SSN                 PIC X(9).
           05  Emp-Name                PIC X(20).
           05  Emp-DOH                 PIC 99/99/9999.
           05  Emp-Position-Code       PIC 9(3).
           05  Emp-Title               PIC X(20).
           05  Emp-Dept-Code           PIC X(3).
           05  Emp-Dept-Title          PIC X(20).
           05  Emp-Location-Code       PIC X(3).
       Working-Storage Section.
       77  EOF-Flag                    PIC 9 Binary.
           88  Not-EOF                 Value 0.
           88  EOF-Reached             Value 1.
       01  WS-File-Status              PIC X(2).
           88  IO-OK                   Value '00'.
       Procedure Division.
            Open Input Emp-Seed
                 Output Emp-File
            Set Not-EOF to True
            Read Emp-Seed At End Set EOF-Reached to True End-Read
            Perform Until EOF-Reached
              Move Corresponding Seed-Record to Emp-Record
              Display Emp-Record
              Write Emp-Record
              Read Emp-Seed At End Set EOF-Reached to True End-Read
            End-Perform
            Close Emp-Seed Emp-File
            GoBack.
       End Program CreatEmp.

Listing 7. CreateEmp.cbl

Return to Article


234655234V.W. Xenophon       07/23/1975010STSM                T8TProgramming         SJS
332741234A.B. Childers       05/05/1990008Advisory Programmer T8TProgramming         SJS
332745234M.N. Olafsson       06/23/1975008Advisory Programmer T8TProgramming         SJS
332854534D.E. Francovich     04/05/1980010Senior Manager      D8TPlanning            SJS
334751434G.H. Illiad         02/13/1965011V.P. Development    E8AInformation Tech.   SJS
432445234P.Q. Raley          07/23/1995006Sen. Assoc. Program.T8TProgramming         SJS
614744444J.K. Lawson         11/25/1970028Consulting Mktg.    M8FMarketting          SJS
852745234S.T. Umlauf         06/23/1996005Associate ProgrammerT8TProgramming         SJS

Listing 8. Empseed.txt

Return to Article

Home Page