Highlighted
huanglei Absent Member.
Absent Member.
1533 views

the connection reference between java and cobol

Jump to solution
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  reference 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.

------------------------------------------------------------------
here,after change "value" to "reference", an error is happened "class cobolclass method cobolclass(ObjectRef) type ObjectRef is not exsit "
now , how to deal with it
0 Likes
1 Solution

Accepted Solutions
huanglei Absent Member.
Absent Member.

RE: the connection reference between java and cobol

Jump to solution

this is right source and tested.

-------------------------------------jvm cobol source-----------------------------------------------------------------------

method-id execute.

      local-storage section.

      linkage section.

      01 myconnection object.

      01 WK-TEST PIC X(9).

      procedure division using by value myconnection.

     *jvmcobolから呼び出してdbを接続の場合、下記ようにを書かなければならない

       exec sql bind connection myconnection to :myconnection

       end-exec.

       IF SQLCODE NOT = 0

            DISPLAY "CONNECTION ERROR"SQLCODE

       END-IF.

       EXEC SQL UNBIND CONNECTION :myconnection

       END-EXEC.

end method.

0 Likes
2 Replies
huanglei Absent Member.
Absent Member.

RE: the connection reference between java and cobol

Jump to solution

add question:

  how to do the sql transation between java and cobol(jvm)/cobol

0 Likes
huanglei Absent Member.
Absent Member.

RE: the connection reference between java and cobol

Jump to solution

this is right source and tested.

-------------------------------------jvm cobol source-----------------------------------------------------------------------

method-id execute.

      local-storage section.

      linkage section.

      01 myconnection object.

      01 WK-TEST PIC X(9).

      procedure division using by value myconnection.

     *jvmcobolから呼び出してdbを接続の場合、下記ようにを書かなければならない

       exec sql bind connection myconnection to :myconnection

       end-exec.

       IF SQLCODE NOT = 0

            DISPLAY "CONNECTION ERROR"SQLCODE

       END-IF.

       EXEC SQL UNBIND CONNECTION :myconnection

       END-EXEC.

end method.

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.