Logo

dev-resources.site

for different kinds of informations.

COBOL Tutorial Series: Working with Database - Session 5

Published at
10/19/2024
Categories
cobol
database
tutorial
programming
Author
ngtduc693
Author
9 person written this
ngtduc693
open
COBOL Tutorial Series: Working with Database - Session 5

Hello, I'm Duke

Today I will show you how to work with Database by COBOL. But, you should read all of the previous sessions to know more about the COBOL

Okay, let's start

1.Steps to set up a COBOL connection to MySQL

- Install MySQL Client

sudo apt install default-mysql-client
Enter fullscreen mode Exit fullscreen mode

- Create a Customer table in MySQL database

CREATE TABLE customers (
    customerNumber INT(11) NOT NULL,
    customerName VARCHAR(50) NOT NULL,
    contactLastName VARCHAR(50) NOT NULL,
    contactFirstName VARCHAR(50) NOT NULL,
    phone VARCHAR(50) NOT NULL,
    addressLine1 VARCHAR(50) NOT NULL,
    addressLine2 VARCHAR(50) DEFAULT NULL,
    city VARCHAR(50) NOT NULL,
    state VARCHAR(50) DEFAULT NULL,
    postalCode VARCHAR(15) DEFAULT NULL,
    country VARCHAR(50) NOT NULL,
    creditLimit DECIMAL(10,2) DEFAULT NULL,
    PRIMARY KEY (customerNumber)
);

INSERT INTO customers (customerNumber, customerName, contactLastName, contactFirstName, phone, addressLine1, addressLine2, city, state, postalCode, country, creditLimit) VALUES
(1, 'Nguyen Thanh Duc', 'Nguyen', 'Thanh Duc', '+84826540246', 'Nguyen Van Linh', 'Phuoc My', 'Da Nang', NULL, '550000', 'Viet Nam', 9000000.00),
(2, 'Tran Thi B', 'Tran', 'Thi B', '0123456788', '456 Nguyen Van Linh', NULL, 'Da Nang', NULL, '550000', 'Viet Nam', 2000.00),
(3, 'Le Van C', 'Le', 'Van C', '0123456787', '789 Bach Dang', 'Hoa Hai', 'Da Nang', NULL, '550000', 'Viet Nam', 1500.00),
(4, 'Hoang Thi D', 'Hoang', 'Thi D', '0123456786', '321 Nguyen Huu Tho', 'My An', 'Da Nang', NULL, '550000', 'Viet Nam', 1200.00),
(5, 'Phan Van E', 'Phan', 'Van E', '0123456785', '654 Tran Phu', 'Son Tra', 'Da Nang', NULL, '550000', 'Viet Nam', 1100.00),
(6, 'Ngo Thi F', 'Ngo', 'Thi F', '0123456784', '987 Hoang Sa', NULL, 'Da Nang', NULL, '550000', 'Viet Nam', 1400.00),
(7, 'Vo Van G', 'Vo', 'Van G', '0123456783', '159 Le Loi', 'Hai Chau', 'Da Nang', NULL, '550000', 'Viet Nam', 1300.00),
(8, 'Bui Thi H', 'Bui', 'Thi H', '0123456782', '753 Nguyen Van Troi', 'An Hai Dong', 'Da Nang', NULL, '550000', 'Viet Nam', 1250.00),
(9, 'Ngo Van I', 'Ngo', 'Van I', '0123456781', '258 Hoang Dieu', NULL, 'Da Nang', NULL, '550000', 'Viet Nam', 1150.00),
(10, 'Pham Thi J', 'Pham', 'Thi J', '0123456780', '852 Quang Trung', 'Nui Thanh', 'Da Nang', NULL, '550000', 'Viet Nam', 1050.00),
(11, 'Truong Van K', 'Truong', 'Van K', '0123456790', '963 Nguyen Thai Hoc', 'Khuong Ha', 'Da Nang', NULL, '550000', 'Viet Nam', 1350.00),
(12, 'Dang Thi L', 'Dang', 'Thi L', '0123456791', '147 To Hien Thanh', NULL, 'Da Nang', NULL, '550000', 'Viet Nam', 1450.00),
(13, 'Vu Van M', 'Vu', 'Van M', '0123456792', '258 Le Duan', 'My Khe', 'Da Nang', NULL, '550000', 'Viet Nam', 1550.00),
(14, 'Luu Thi N', 'Luu', 'Thi N', '0123456793', '369 Hoang Sa', 'Hoa Khue', 'Da Nang', NULL, '550000', 'Viet Nam', 1650.00),
(15, 'Nguyen Van O', 'Nguyen', 'Van O', '0123456794', '456 Tran Hung Dao', 'Nam Duong', 'Da Nang', NULL, '550000', 'Viet Nam', 1750.00),
(16, 'Ngoc Thi P', 'Ngoc', 'Thi P', '0123456795', '567 Hoang Dieu', 'Chinh Gian', 'Da Nang', NULL, '550000', 'Viet Nam', 1850.00),
(17, 'Tran Van Q', 'Tran', 'Van Q', '0123456796', '678 Nguyen Tri Phuong', NULL, 'Da Nang', NULL, '550000', 'Viet Nam', 1950.00),
(18, 'Le Thi R', 'Le', 'Thi R', '0123456797', '789 Nguyen Thi Minh Khai', 'Thanh Khe', 'Da Nang', NULL, '550000', 'Viet Nam', 2050.00),
(19, 'Phan Van S', 'Phan', 'Van S', '0123456798', '890 Phan Chu Trinh', 'Son Tra', 'Da Nang', NULL, '550000', 'Viet Nam', 2150.00),
(20, 'Vo Thi T', 'Vo', 'Thi T', '0123456799', '901 Hoang Sa', NULL, 'Da Nang', NULL, '550000', 'Viet Nam', 2250.00);

Enter fullscreen mode Exit fullscreen mode

- Test connection to MySQL database

mysql -h <host> -P <port> -u <user> -p<password> -e "SELECT 1"
Enter fullscreen mode Exit fullscreen mode

Test connection

2.Create mysql.cbl file

       IDENTIFICATION DIVISION.
       PROGRAM-ID. DUKE-CONNECT-MYSQL.

       ENVIRONMENT DIVISION.
       CONFIGURATION SECTION.
       SOURCE-COMPUTER. GNUCOBOL.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-COMMAND PIC X(1000).
       01 WS-RETURN-CODE PIC S9(4) COMP.
       01 WS-HOSTNAME PIC X(60) VALUE '<your hostname>'.
       01 WS-PORT PIC 9(6) VALUE <your port>.
       01 WS-USERNAME PIC X(10) VALUE '<your username>'.
       01 WS-PASSWORD PIC X(30) VALUE '<your password>'.
       01 WS-DBNAME PIC X(10) VALUE 'defaultdb'.
       01 WS-CUSTOMER-QUERY PIC X(30) VALUE 'SELECT * FROM customers'.

       PROCEDURE DIVISION.

       STRING
           'mysql '        DELIMITED BY SIZE
           '-h '          DELIMITED BY SIZE
           WS-HOSTNAME    DELIMITED BY SIZE
           ' -P '         DELIMITED BY SIZE
           WS-PORT        DELIMITED BY SIZE
           ' -u '         DELIMITED BY SIZE
           WS-USERNAME    DELIMITED BY SIZE
           ' -p'          DELIMITED BY SIZE
           WS-PASSWORD    DELIMITED BY SIZE
           ' -D '         DELIMITED BY SIZE
           WS-DBNAME      DELIMITED BY SIZE
           ' -e "'        DELIMITED BY SIZE
           WS-CUSTOMER-QUERY DELIMITED BY SIZE
           '"'            DELIMITED BY SIZE
           INTO WS-COMMAND
       END-STRING

       CALL "system" USING WS-COMMAND GIVING WS-RETURN-CODE.

       IF WS-RETURN-CODE = 0
          DISPLAY 'Connected and query executed!'
       ELSE
          DISPLAY 'Failed to execute command, error code: '
          WS-RETURN-CODE
       END-IF.

       STOP RUN.

Enter fullscreen mode Exit fullscreen mode

- Compile and run it

cobc -x mysql.cbl
./mysql
Enter fullscreen mode Exit fullscreen mode

And then, that is the result

result

My repository for this tutorial here

cobol Article's
30 articles in total
Favicon
Modernizing COBOL Applications for Free: A Practical Guide with GnuCOBOL
Favicon
COBOL Interpreter
Favicon
COBOL Tutorial Series: DB2 vs SQL Server Architecture Comparison - Session 7
Favicon
COBOL Tutorial Series: Install the DB2 on Windows/Linux - Session 6
Favicon
COBOL Tutorial Series: Working with Database - Session 5
Favicon
COBOL Tutorial Series: Condition (IF, ELSE) statement - Session 3
Favicon
COBOL Tutorial Series: A calculation program - Session 2
Favicon
COBOL Tutorial Series: Loop statements - Session 4
Favicon
COBOL Tutorial Series: Developing Without a Mainframe - Session 1
Favicon
Warum?
Favicon
Awesome COBOL!
Favicon
#12in24: Learning COBOL in 1h.
Favicon
The World Depends on 60-Year-Old Code No One Knows Anymore ๐Ÿ‘ด๐Ÿป๐Ÿš€
Favicon
Tests Everywhere - COBOL
Favicon
COBOL: El Lenguaje de Programaciรณn que Desafรญa el Paso del Tiempo
Favicon
Entiende de una vez los lenguajes de alto y bajo nivel
Favicon
How Did You Get Started with COBOL?
Favicon
How to make an XML element optional in a Cobol provider webservice (DFHLS2WS), based on a Cobol structure
Favicon
How to create a flexible number of xml-elements in a cobol provider webservice (DFHLS2WS), starting from a cobol-structure.
Favicon
Advent of Code, but differently
Favicon
Covid led me to my first open-source contribution
Favicon
Test Driven Design in COBOL
Favicon
OpenCobolIde "no compiler found or not working"
Favicon
Mainframe Interview Experience with XYZ Bank | Jan 2022
Favicon
100 Languages Speedrun: Episode 50: COBOL
Favicon
COBOL gets a hip replacement
Favicon
How to deal with COBOL REDEFINES?
Favicon
They're still talking COBOL
Favicon
Video and Q&A: Learn how COBOL and open source are used in modern mainframe ecosystem
Favicon
Programming language full forms and definition

Featured ones: