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
- That you have a working Visual COBOL installation on your computer. If not, and you need help to install it, please, have a look at the article mentioned above.
- That you have a MySQL server running at port 3306 on localhost, and the Connector/ODBC installed. Also, the sample database "world" is needed. If you used the all-in-one binary distribution, called MySQL Installer, to set up MySQL, and chose "Development default" as installation type, all this will automatically be done. You should also have created a MySQL user (my user is called "allu"), who has (at least) SELECT privileges on the "world" database. If you are not sure about how to proceed, my article Web development environment setup on MS Windows: MySQL database server should be helpful.
- That you have configured an ODBC data source. On Windows, this can easily be done using ODBC Data Source Administrator. For details about ODBC and the setup of a DSN, you might want to have a look at the second part of my article Connecting to a MySQL database using Lazarus/Free Pascal. The DSN used by my COBOL program is detailed below...
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.
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.
Notes:
- The first execution of the program corresponds to the situation where the MySQL server is not running.
- 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.