I threw together this little example that has a java main that connects to sqlserver using jdbc, executes a query and then passes the connection object to COBOL as a parameter in linkage.
COBOL then binds to the connection and executes a query itself using embedded sql and then unbinds from the object and returns to Java.
I hope it conveys the general idea of how this can be done...
Java program:
import java.sql.*; public class javamain { /** * @param args * @throws SQLException */ public static void main(String[] args) throws SQLException { // Create a variable for the connection string. String connectionUrl = "jdbc:sqlserver://localhost:1433;" + "databaseName=Northwind;user=chris;password=mypassword"; // Declare the JDBC objects. Connection con = null; Statement stmt = null; ResultSet rs = null; try { // Establish the connection. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); con = DriverManager.getConnection(connectionUrl); // Create and execute an SQL statement that returns some data. String SQL = "SELECT TOP 10 * FROM Customers"; stmt = con.createStatement(); rs = stmt.executeQuery(SQL); // Iterate through the data in the result set and display it. while (rs.next()) { System.out.println(rs.getString(1) + " " + rs.getString(2)); } cobolclass cc = new cobolclass(); cc.cobolclass(con); } // Handle any errors that may have occurred. catch (Exception e) { e.printStackTrace(); } finally { if (rs != null) try { rs.close(); } catch(Exception e) {} if (stmt != null) try { stmt.close(); } catch(Exception e) {} if (con != null) try { con.close(); } catch(Exception e) {} } } }
cobol program:
$set sql(dbman=jdbc) ilsmartlinkage program-id. cobolclass as "cobolclass". data division. working-storage section. exec sql include sqlca end-exec. 01 contact-name string. 01 company-name string. linkage section. 01 myconnection object. procedure division using by value myconnection. exec sql whenever sqlerror perform 999-sql-error end-exec exec sql bind connection to :myconnection end-exec exec sql declare mycursor cursor for select ContactName, CompanyName from Customers end-exec exec sql open mycursor end-exec perform until exit exec sql fetch mycursor into :contact-name, :company-name end-exec if sqlcode = 100 display "no more" exit perform else display contact-name display company-name end-if end-perform exec sql close mycursor end-exec exec sql unbind connection :myconnection end-exec goback. 999-sql-error. display "error = " sqlcode display sqlerrmc. end program cobolclass.