Connecting Visual COBOL to MySQL
$SET SQL(DBMAN=ODBC) **************************************************************** * Visual COBOL -> ODBC -> MySQL example. * * Retrieves records from the MySQL Employees sample database. * Prerequisites: Configure an ODBC data source for the Easysoft * MySQL ODBC driver that connects to the MySQL Employees database * **************************************************************** working-storage section. EXEC SQL INCLUDE SQLCA END-EXEC. 01 pempno pic x(5). 01 pfirstname pic x(20). 01 plastname pic x(20). EXEC SQL BEGIN DECLARE SECTION END-EXEC. procedure division. main-para. * Replace MySQLODBCDataSource with the name of an ODBC data * source that connects to the MySQL Employees database. Replace * mysqluser and mysqlpassword with the details for a user who has * permissions to access this database. EXEC SQL CONNECT TO "MySQLODBCDataSource" USER mysqluser USING mysqlpassword END-EXEC. loop-point. if sqlcode not = 0 display "Error: not connected" display sqlcode display sqlerrmc stop run end-if perform until exit display " " display "Enter Emp_No (Eg 10001, blank to end): " with no advancing accept pempno if pempno = SPACES exit perform end-if EXEC SQL SELECT first_name, last_name INTO :pfirstname, :plastname FROM employees WHERE emp_no = :pempno END-EXEC if sqlcode not = 0 if sqlcode = 100 display "No employee found" else display sqlcode display sqlerrmc end-if else display "First name for " pempno " is " pfirstname display "Last name for " pempno " is " plastname end-if end-perform EXEC SQL DISCONNECT CURRENT END-EXEC end program Program1.
Installing the MySQL ODBC driver
- Download the MySQL ODBC driver for your Windows platform.
- Install and license the MySQL ODBC driver on the machine where your client application is installed.
For example, if you want to analyse MySQL data in Excel, install the MySQL ODBC driver on the machine where Excel is installed.
To do this, execute the file distribution that you downloaded in the previous step, and follow the on-screen instructions.
The install program starts the Easysoft License Manager, because you cannot use your Easysoft product until a license is obtained.
The following types of license are available:
- Free time-limited trial license, which gives you free and unrestricted use of the product for a limited period (usually 14 days).
- Full license if you have purchased the product. On purchasing the product, you are given an authorization code, which you use to obtain a license.
- In Easysoft License Manager, enter your details.
Complete the Name, E-Mail Address, and Company fields.
The E-Mail Address must be the same as the address used to register and download from the Easysoft web site or you will be unable to obtain trial licenses.
- Choose Request License.
You're asked for a license type.
- Do one of the following:
- For a trial license, choose Time Limited Trial and then choose Next.
The License Manager asks what software you are licensing. Select your product from the drop-down list and then choose Next.
- If you have obtained an authorization code for a purchased license, select Non-expiring License and then choose Next.
The License Manager requests your authorization code.
Enter the authorization code and then choose Next.
- For a trial license, choose Time Limited Trial and then choose Next.
- The License Manager displays a summary of the information you entered and allows you to choose the method of applying for your license.
- Do one of the following:
- Choose On-line Request if your machine is connected to the internet and can make outgoing connections to port 8884.
The License Manager then sends a request to the Easysoft license server to activate your license key automatically. This is the quickest method and results in your details being entered immediately into our support database.
- The remaining three options (Email Request, Print Request, and View Request) are all ways to obtain a license if your machine is off-line (does not have a connection to the internet).
Each of these methods involves providing Easysoft with information including your machine number (a number unique to your machine) and then waiting to receive your license key.
Instead of emailing your details to Easysoft, you can enter them directly at the Easysoft web site and your license key will be emailed to you automatically.
To use this method, choose View Request, and then visit:
In the licensing page, enter your machine number (and authorization code for a purchased license), choose Submit and your license key will be emailed to you.
When you receive the license key, you can activate it either by double-clicking the email attachment or by choosing Enter License on the License Manager main screen and pasting the license key into the dialog box.
- Choose On-line Request if your machine is connected to the internet and can make outgoing connections to port 8884.
- Choose Finish to exit the License Manager.
The installation is complete.
Configuring an ODBC data source
Before you can use the MySQL ODBC driver to connect your application to MySQL, you need to configure an ODBC data source. An ODBC data source stores the connection details for the target database (in this case, MySQL) and the ODBC driver that is required to connect to it (in this case, the MySQL ODBC driver).
You configure ODBC data sources in ODBC Data Source Administrator, which is included with Windows. To run ODBC Data Source Administrator, in the Windows Run dialog box, enter:
%windir%\syswow64\odbcad32.exe
Use ODBC Data Source Administrator to create a MySQL ODBC driver data source:
- Choose the System DSN tab, and then choose Add.
- In the Create New Data Source dialog box, choose Easysoft ODBC-MySQL Driver, and then choose Finish.
- Complete these fields in the Easysoft ODBC-MySQL Driver DSN Setup dialog box:
Setting Value DSN MySQLODBCDataSource
Database Employees
Note This is the database that the code sample is designed to work with.
User Name The name of your MySQL user. Password The password for your MySQL user. Server The host name or IP address of the machine on which your MySQL server is running. - Use the Test button to verify that you can successfully connect to your MySQL database.
Connecting Visual COBOL to MySQL
Visual COBOL uses an ODBC driver to interact with an external data source. ODBC is a data access technology, the Microsoft implementation of which is included with Windows. You can use the MySQL ODBC driver to connect Visual COBOL to a MySQL Database, enabling you to work with MySQL data from a COBOL program.
- In Visual Studio, create a new Templates > COBOL > Native > Console Application.
- Replace the template code with the code shown at the start of this article.
- Run the program.