Using DBConnect Class to get a ResultSet Object

ACUCOBOL-GT's "CVM.jar" package contains a class for connecting to JDBC data sources, and querying those data sources for ResultSet objects. The class is called DBConnect.

The DBConnect class has two public static methods called "connect" and "query". The "connect" method takes two string parameters: a JDBC driver string, and a JDBC connection string, and returns a java.sql.Connection object. The "query" method takes two parameters: a query string, and a java.sql.Connection object, and returns a ResultSet object. The ResultSet object can then be used to access and update the data.

Here is an example of using the DBConnect class in COBOL:

MOVE "sun.jdbc.odbc.JdbcOdbcDriver" to DB-DRIVERSTR.

MOVE "jdbc:odbc:DefaultDir=D:\cobol7\bin;Driver={Microsoft Text Driver (*.txt; *.csv)};DriverId=27;UID=admin;Initial Catalog=D:\cobol7\bin" to DB-CONNECTSTR.

MOVE "SELECT * FROM dataFile.csv" to DB-QUERY.
    
CALL "C$JAVA" USING CJAVA-NEW, "java/lang/Object", "()V" GIVING DB-CONNECT.

CALL "C$JAVA" USING CJAVA-NEW, "java/lang/Object", "()V" GIVING DB-RESULTSET.
    
CALL "C$JAVA" USING "com/acucorp/acucobolgt/DBConnect", "connect", "(XX)Ljava/sql/Connection;", DB-DRIVERSTR, DB-CONNECTSTR, DB-CONNECT GIVING STATUS-VAL.
     
CALL "C$JAVA" USING "com/acucorp/acucobolgt/DBConnect", "query", "(XLjava/sql/Connection;)Ljava/sql/ResultSet;", DB-QUERY, DB-CONNECT, DB-RESULTSET GIVING STATUS-VAL
     
CALL "C$JAVA" USING CJAVA-CALL, DB-RESULTSET, "java/sql/ResultSet", "next", "()Z", FIELD-BOOLRET GIVING STATUS-VAL.
     
CALL "C$JAVA" USING CJAVA-CALL, DB-RESULTSET, "java/sql/ResultSet", "getRow", "()I", FIELD-RET GIVING STATUS-VAL.
     
MOVE 1 to FIELD-INT.
    
CALL "C$JAVA" USING CJAVA-CALL, DB-RESULTSET, "java/sql/ResultSet", "getString", "(I)X", FIELD-INT, FIELD-STRINGRET GIVING STATUS-VAL.

CALL "C$JAVA" USING CJAVA-DELETE, DB-CONNECT GIVING STATUS-VAL.

CALL "C$JAVA" USING CJAVA-DELETE, DB-RESULTSET GIVING STATUS-VAL.

In this example, the JDBC driver used was the jdbc:odbc bridge that ships with the Java SDK. The ODBC driver is the Microsoft Text driver, and a text CSV file was used as the data source. Two new object handles are created to contain the Connection and ResultSet handles. The ResultSet method "next" is called to move to the first row, the "getRow" method is called to find the current row number, and the "getString" method is called to get the value in column one which happens to be of type string. Finally, the two objects are deleted.