z/OS Batch Runtime Planning and User's Guide
Previous topic | Next topic | Contents | Contact z/OS | Library | PDF


Example: Java code calling COBOL

z/OS Batch Runtime Planning and User's Guide
SA23-1376-00

Figure 1 shows an example of Java™ code calling COBOL.
Figure 1. Example: Java code calling COBOL (Part 1 of 2)
package com.ibm.zos.batch.container.test;

import java.sql.*;
import com.ibm.batch.spi.UserControlledTransactionHelper;
import com.ibm.ws.gridcontainer.exceptions.TransactionManagementException;

public class Sample
{
  //Native method declaration
  private native int CallCOBOL();
  //Load the library
  static {
  System.loadLibrary("c_to_cobol");
}

public static void main(String[] args)
{
Connection conn = DriverManager.getConnection(url);
  String url = "jdbc:default:connection";

  Statement stmt;
  int maxRows = 25;
  String pnumber = "";
  int pnum = 0;
  int rc = 0;
  String formatted; 

  try 
    {
      System.out.println ( "Establishing Connection to URL: " + url );

      conn = DriverManager.getConnection(url);
      System.out.println ( " successful connect" );
      stmt = conn.createStatement();
      System.out.println ( " Successful creation of Statement" );
      // Limit the number of rows to return
      stmt.setMaxRows ( maxRows );
Figure 2. Example: Java code calling COBOL (Part 2 of 2)
    // SELECT from an DB2 sample table
    String sqlText =
    "SELECT PHONENUMBER " +
    "FROM DSN8910.VEMPLP " +
    "WHERE EMPLOYEENUMBER = '000260'";
    ResultSet results = stmt.executeQuery ( sqlText );
    pnumber = results.getString ( "PHONENUMBER" );
    pnum = Integer.parseInt(pnumber.trim());
    pnum++;
    pnum = pnum % 10000;
    formatted = String.format("%04d", pnum); 

    sqlText =
    "UPDATE DSN8910.VEMPLP " +
    " SET PHONENUMBER = " + "'"+formatted+"'" +
    " WHERE EMPLOYEENUMBER = '000260' ";
    int updateCount = stmt.executeUpdate(sqlText);
    System.out.println ( "Successful execution of UPDATE. Rows updated= " + updateCount );

    // close ResultSet and Statement
    results.close();
    // Call COBOL via a C DLL 
    Sample call_cobol = new Sample();
    //Call native method
    rc = call_cobol.CallCOBOL();
    System.out.println ( "Returned from COBOL with a rc: " + rc );

    if (rc == 0)
      { 
        try 
        {
          UserControlledTransactionHelper.commit(); 
        } 
        catch (TransactionManagementException e) 
        {
          e.printStackTrace();
        } 
      }
    else
      { 
        try 
        {

        UserControlledTransactionHelper.rollback();
        } 
        catch (TransactionManagementException e) 
        {
          e.printStackTrace();
        } 
      }
    } 
    catch (SQLException ex)
      {
        System.out.println("SQLException information");
        while(ex!=null) {
        System.err.println ("Error msg: " + ex.getMessage());
        System.err.println ("SQLSTATE: " + ex.getSQLState());
        System.err.println ("Error code: " + ex.getErrorCode());
        ex.printStackTrace();
        ex = ex.getNextException(); 
      }
    }
  } 
} 

Go to the previous page Go to the next page




Copyright IBM Corporation 1990, 2014