Computing: DOS, OS/2 & Windows Programming

Accessing a MySQL database with Microfocus Visual COBOL.

Microfocus Visual COBOL is a modern COBOL development environment, that includes features like object-oriented programming, and that may be fully integrated with Visual Studio or Eclipse. The software is commercial, but there is a one-year trial license available; cf. my article Modern COBOL development using Microfocus Visual COBOL for details.

Visual COBOL supports several ways to connect to a relational database like MySQL. This tutorial is about the connection of Visual COBOL for Visual Studio Personal Edition 9.0 to MySQL Community Server 8.0 using ODBC. The included program sample has been written and tested on Windows 11. It should apply to older Windows releases, where these versions of Visual COBOL and MySQL are supported.

Prerequisites.

It is supposed

The screenshots below show the creation of a system DSN to be used to access the "world" database on MySQL 8.0. The important thing here is the data source name (I chose "MySQLWorld"); it's by this name that you'll reference the data source in the COBOL program. Note, that I entered the user name and password information only to test the connection, then I removed them again; in fact I will add them later in the COBOL source code.

Accessing MySQL with Visual COBOL: Creating an ODBC data source [1]
Accessing MySQL with Visual COBOL: Creating an ODBC data source [2]

My program sample test-mysql2.cbl asks for a country code and, if it exists in the "country" table , displays the name of the country and its capital. Click the following link to download the source code.

       $SET SQL(DBMAN=ODBC)
       *********************************************************
       * Test Visual Cobol access to MySQL database using ODBC *
       *********************************************************
       identification division.
       program-id. test-mysql2.
       data division.
       working-storage section.
          EXEC SQL INCLUDE SQLCA END-EXEC.
       01 country-code pic u(3).
       01 country-name pic u(52).
       01 country-capital pic u(35).
          EXEC SQL BEGIN DECLARE SECTION END-EXEC.
       procedure division.
       main section.
          EXEC SQL CONNECT TO "MySQLWorld" USER "allu"
             USING "password"
          END-EXEC.
          if sqlcode not = 0
             display "Cannot connect to 'world' database"
             display "SQL code = " sqlcode
             display sqlerrmc
             stop run
          end-if.
          move "XXX" to country-code.
          perform Do-country
             until country-code = spaces.
          EXEC SQL
             DISCONNECT CURRENT
          END-EXEC.
          stop run.
       Do-country.
          display "Enter country code (ex: USA; ENTER to terminate): "
             with no advancing.
          accept country-code.
          if country-code not = spaces
             EXEC SQL
                SELECT country.Name, city.Name
                   INTO :country-name, :country-capital
                   FROM country, city
                   WHERE country.Code = :country-code
                     AND country.Capital = city.ID
             END-EXEC
             if sqlcode = 0
                display "Country: " country-name
                display "Capital: " country-capital
                display " "
             else
                if sqlcode = 100
                   display "Unknown country code"
                   display " "
                else
                   display "Error when reading 'world' database"
                   display "SQL code = " sqlcode
                   display sqlerrmc
                end-if
             end-if
          end-if.

The screenshot below shows some executions of the program.

Accessing MySQL with Visual COBOL: Reading data from the 'world' database

Notes:

  1. The first execution of the program corresponds to the situation where the MySQL server is not running.
  2. If you have a look at the capital of Luxembourg, you can see that despite the usage of pic u(), the non-ASCII characters are displayed as "garbage". This is, however, not a problem of Visual COBOL, but a bad code page used with the "world" database tables (if you look at the table content in MySQL Workbench, you'll see "garbage" instead of accentuated characters, too). Thus, to really use the "world" database (with COBOL or whatever), you should drop it and recreate it using all UTF8. I did not do this, so I'm not 100% sure if the display of non-ASCII characters by my Visual COBOL program is correct (but, I suppose that it is)...


If you find this text helpful, please, support me and this website by signing my guestbook.