Sample: Map a Table Join and Retrieve Rows from the Result Set

Objective
The objective of this sample demonstrates how to:

The sample database you will use must be set up before you run the sample. See Preparation for Samples for information.

Description
This sample does the following:

  1. Connects to the database.
  2. Opens a cursor on the table join using the open method to get department result set.
  3. Uses the fetchNext method to fetch a row.
  4. Uses the element method to get the contents of the row.
  5. Prints the row.
  6. Repeats steps 3 to 5 to retrieve and print each row of result set.
  7. Closes the cursor.
  8. Disconnects from the database.

Generated Data Access Classes Used by this Sample
This sample uses the following generated classes and methods, as defined in the Data Access mapping, minisamp.dax (the table join is called Query):

Some of the methods listed are inherited from the Data Access class library.

Running the Sample
The executable for the sample is called tabljoin. Since Data Access Builder generates code for three access methods (CLI, ESQL, and ODBC), a tabljoin executable is located in each of three corresponding directories: db2cli, db2esql, and odbc (all of these are subdirectories of samples\database\minisamp).

To run the sample, enter the following on the command line of the appropriate directory:

tabljoin

If the sample fails because it could not connect to the database, enter a user ID and password that is valid for accessing the sample database, as shown:

tabljoin userid password

The source code for the tabljoin executables is located in the samples\database\minisamp\source directory. If you make changes to the sample's source code, you must import the sample into an IDE project and rebuild it.

The Data Access Builder Mapping
The Data Access samples use a complex mapping, which is provided for you in a file called minisamp.dax. One minisamp.dax mapping file is provided in each of the three directories: db2cli, db2esql, and odbc. If you want to see the mapping or generate the classes for it, import the sample into the IDE; in the Data Access Launch view, select the part and click Edit Part.

Hints on How to Create A Mapping for This Sample
Rather than using the mapping provided with the samples (minisamp.dax), you can create your own mapping that will work with this sample (but not necessarily the other samples). To create a mapping for this sample, follow these steps:

  1. In the IDE, create a new project, an executable target, and a new Data Access part. When you choose to add a Data Access part, the Data Access SmartGuide will start up.
  2. Create the schema mapping in the SmartGuide pages as follows:
    1. On the overview page of the SmartGuide, click Next>> to turn the page.
    2. If you are using DB2, select DB2 Driver and select the database SAMPLE; if you are using dBase, select ODBC Driver and select the database CPPASAMP40.
    3. Select By entering an SQL Statement to specify the table join for the schema mapping method. Click Next>> to turn the page.
    4. If you are using DB2, select the desired database access method (DB2 CLI or DB2 ESQL). Click Next>> to go to the next page. If you are using ODBC, you will not see this page.
    5. Click Get Tables with Filter and when the list of tables is displayed, select DEPARTMENT and EMPLOYEE for DB2 or DEPT and EMPL for dBase. Click Next>> to turn the page.
    6. Enter "Query" for the Name for the SQL Query.
    7. Enter the following SQL statement for DB2:
      select d.deptname, e.firstnme, e.midinit, e.lastname from employee e,
      department d where d.deptno=e.workdept

      or the following for dBase:
      select d.deptname, e.firstnme, e.midinit, e.lastname from empl e,
      dept d where d.deptno=e.workdept

    8. Click Validate.

      Warning! If you are using DB2, the following lines must be in the DB2CLI.INI file in the SQLLIB directory, or else the validate action will return without an error message regardless of the SQL statement's actual validity:

      [common]
      DEFERREDPREPARE=0
    9. Click Finish. You will return to the IDE.
  3. Build the part (The build process will automatically generate the classes for the mapping).


Data Access Builder
Data Access Builder Workflow
Table Joins and Query Support


Data Access Class Library and Generated Class Reference
Data Access Builder Samples
Preparation for Samples