Developing OTDs for Database Adapters

Creating a New DB2 OTD

The Database OTD Wizard generates OTDs by connecting to external data sources and creating corresponding Object Type Definitions. The OTD Wizard can create OTDs based on any combination of Tables and Stored Procedures or Prepared SQL Statements. Field nodes are added to the OTD based on the Tables in the external data source. Java method and parameter nodes are added to provide the appropriate JDBC functionality. For more information about Java methods, refer to your JDBC developer’s reference guide.


Note –

Database OTDs are not messagable. For more information on messagable OTDs, see the Enterprise Service Bus User’s Guide


The following steps are required to create a new OTD for the DB2 UDB Adapter.

Select Wizard Type

Select the type of wizard required to build an OTD in the New Object Type Definition Wizard.

ProcedureTo Select a Wizard Type

  1. On the project tree list, right click on the project and select Create an Object Type Definition from the shortcut menu.

  2. From the OTD Wizard Selection window, select the DB2 Database and click Next (see the following figure).

    Figure 1–1 OTD Wizard Selection

    Select Wizard Type

Connecting to a Database

ProcedureTo Connect to a Database

  1. Select the Connection type using the drop-down list (see the following figure). The rest of the Connection Information fields displayed will depend on your selection.

    Connect to Database
  2. Specify the applicable connection information (depending on the Connection type) for your database including:

    • Host Name - The server where DB2 resides.

    • Port - The port number of DB2.

    • Location (AS/400 and z/OS specific) - The name of the DB2 subsystem. To find the location of the DB2 subsystem, use the Database Query Tool to issue the following query: select current server from sysibm.sysdummy1.

    • Database (Windows/Unix specific) - The name of the database instance.

    • Collection (AS/400 and z/OS specific) - The name that identifies a group of packages. For more information on Packages, refer to Packages.

    • User Name - The user name that the adapter uses to connect to the database.

    • Password - The password used to access the database.

    • An Optional Parameters field (only for AS/400 and z/OS) will display in the OTD Wizard if the DB2_ConnectionInfo.txt file is present in the netbeans\usrdir\modules\ext\ db2adapter directory. This field allows additional connection parameters for the DB2 OTD wizard. Please contact Sun Microsystems, Inc. for more information on Optional Parameters.


      Note –

      This parameter is not specific to an OTD. It’s only specific to the session where Java CAPS IDE runs. When you edit the OTD, the current value that shows up is the value that was entered previously in the Wizard. It may or may not be the same value you used for creating the original OTD.


      Parameter examples include:

      • The showSelectableTables=false parameter can be set to false (default is true) to see additional tables listed under a user, such as the behavior in a previous Adapter which uses an older version of the driver.

      • The AlternateID=user1 parameter allows you to set the object owner to be user1 otherwise the user in the User Name field will be used.

      • Multiple parameters can be used and must be separated by a semi-colon (;).

      • Click Next. The Select Database Objects window appears.

Packages

This Adapter uses a DataDirect driver (previously known as Merant) to execute SQL calls in DB2. The DataDirect driver requires packages to be created in the DB2 System. Packages do not contain specific SQL statements like static SQL packages but rather dynamic sections, used like cursors to help facilitate the driver’s executing of dynamic SQL queries and returning results.

Creating packages on the server, also known as binding packages, needs only be done once. The first user of the OTD Wizard must have bind permission to create the packages. Without bind authority the user receives an error message when the driver attempts to bind the packages and they will be unable to issue any SQL call. Packages are created automatically, under the Collection ID, when the user fills in the Wizard entries. If the Collection ID is left as blank, it will generate the packages under NULLID.

The driver creates SQL packages on the database including: DDJC330A, DDJC330B, DDJC330C, etc. When connecting, the driver queries a system table to determine whether the default packages exist on the system. If none exist, the driver creates them.


Note –

SQL applications that execute dynamic SQL against DB2 need to have packages bound on the server. In the case of some IBM native tools this may not be obvious because the packages are already installed on the database by default.


Selecting Database Objects

ProcedureTo Select a Database

  1. When selecting Database Objects, you can select any combination of Tables, Views, Procedures, or Prepared Statements you would like to include in the .otd file. Click Next to continue (see the following figure).

    Select Database Objects
  2. Click Next to continue. The Select Tables/Views window appears.


    Note –

    Views are read-only and are for informational purposes only.


Selecting Table/Views/Aliases

ProcedureTo Select Tables/Views/Aliases

  1. In the Select Tables/Views/Aliases window, click Add (see the following figure).

    Tables/Views/Aliases
  2. In the Add Tables window, select the type of criteria to be used for your search, consisting of table data, view only data, or both. You can include system tables in your search by selecting the checkbox.

  3. From the Table/View Name drop down list, select the location of your database table and click Search. You can search for Table/View Names by entering a table name. The use of wildcard characters of ”?’, and ”*’ as part of your Table/View name search allow for greater search capabilities. For example, “AB?CD” or “AB*CD”.

  4. Select the table of choice and click OK. The table selected is added to the Selected window (see the following figure).

    Add Tables
  5. On the Selected Tables/Views window, review the table(s) you have selected. To make changes to the selected Table or View, click Change. If you do not wish to make any additional changes, click Next to continue.

  6. If you clicked Change on the Selected Tables/Views window, you can select or deselect your table columns on the Table/View Columns window. You can also change the data type for each table by highlighting the data type and selecting a different one from the drop down (see the following figure).

    Table View Columns
  7. Click Advanced to change the data type, precision/length, or scale. In general, do not change the precision/length or the scale. Once you have finished your table choices, click OK (see the following figure).

    Table View Column Advanced

Selecting Procedures

ProcedureTo Select Procedures

  1. On the Select Procedures and specify Resultset and Parameter Information window, click Add.

    Select Procedures
  2. On the Select Procedures window, enter the name of a Procedure or select a table from the drop down list. Click Search. Wildcard characters can also be used.

  3. In the resulting Procedure Selection list box, select a Procedure. Click OK.

    Add Procedures
  4. On the Select Procedures and specify Resultset and Parameter Information window click Edit Parameters to make any changes to the selected Procedure (see the following figure).

    Procedure Parameters
  5. To restore the data type, click Restore. When finished, click OK.

  6. To select how you would like the OTD to generate the nodes for the Resultset click Edit Resultsets.

  7. Click Add to add the type of Resultset node you would like to generate.

    Edit ResultSets

    The DBWizard provides three different ways to generate the ResultSet nodes of a Stored Procedure. They are the "By Executing", "Manually", and "With Assistance" modes.

    • By Executing Mode — “By Executing” mode executes the specified Stored Procedure with default values to generate the ResultSet(s). Depending on the business logic of the Stored Procedure, zero or more ResultSets can be returned from the execution. In the case that there are multiple ResultSets and "By Executing" mode does not return all ResultSets, one should use the other modes to generate the ResultSet nodes.

    • With Assistance Mode — "With Assistance" mode allows users to specify a query and execute it to generate the ResultSet node. To facilitate this operation, the DBWizard tries to retrieve the content of the specified Stored Procedure and display it. However, content retrieval is not supported by all types of Stored Procedures. We can roughly classify Stored Procedures into two types: SQL and external. SQL Stored Procedures are created using CREATE PROCEDURE SQL statements while external Stored Procedures are created using host languages (e.g. Java). Since external Stored Procedures do not store their execution plans in the database, content retrieval is impossible. When using "Assist" mode, highlight the execute statement up to and including the table name(s) before executing the query.

    • Manually Mode — "Manually" mode is the most flexible way to generate the result set nodes. It allows users to specify the node name, original column name and data type manually. One drawback of this method is that users need to know the original column names and data types. This is not always possible. For example, the column name of 3*C in this query.


      SELECT A, B, 3*C FROM table T

      is generated by the database. In this case, "With Assistance" mode is a better choice.

      If you modify the ResultSet generated with the by the “By Executing Mode” of the Database Wizard, you need to make sure the indexes match the Stored Procedure. This assures your ResultSet indexes are preserved.

  8. When using Prepared Statement packages, select Use fully qualified table/view names in the generated Java code.

Add Prepared Statements

A Prepared Statement OTD represents a SQL statement that has been compiled. Fields in the OTD correspond to the input values that users need to provide.

Prepared statements can be used to perform insert, update, delete and query operations. A prepared statement uses a question mark (?) as a place holder for input. For example: insert into EMP_TAB (Age, Name, Dept No) values (?, ?, ?)

To execute a prepared statement, set the input parameters and call executeUpdate() and specify the input values if any.

ProcedureTo Add Prepared Statements

When using a Prepared Statement, the ”ResultsAvailable()’ method will always return true. Although this method is available, you should not use it with a ”while’ loop. Doing so would result in an infinite loop at runtime and will stop all of the system’s CPU. If it is used, it should only be used with the ”if’ statement. You can process a resultset by looping through the next() method.

  1. On the Add Prepared Statements window, click Add.

    Figure 1–2 Prepared Statement

    Add Prepared Statement

  2. Enter the name of a Prepared Statement or create a SQL statement by clicking in the SQL Statement window. When finished creating the statement, click Save As giving the statement a name. This name appears as a node in the OTD. Click OK (see the following figure).

    Figure 1–3 Prepared SQL Statement

    Prepared Statement Screen

  3. On the Add Prepared Statement window, the name you assigned to the Prepared Statement appears. To edit the parameters, click Edit Parameters. You can change the datatype by clicking in the Type field and selecting a different type from the list.


    Note –

    When doing a Prepared Statement with two or more tables, where multiple tables have the same column name, you must put the table name qualifier in the Prepared Statement to build the OTD.


  4. Click Add if you want to add additional parameters to the Statement or highlight a row and click Remove to remove it. Click OK (see the following figure).

    Figure 1–4 Edit the Prepared Statement Parameters

    Prepared Statement Parameters

  5. To edit the Resultset Columns, click Edit Resultset Columns. Both the Name and Type are editable but it is recommend you do not change the Name. Doing so will cause a loss of integrity between the Resultset and the Database. Click OK (see the following figure).


    Note –

    The OTD Wizard fails to create OTDs with complex prepared statements that use the same column name in different tables. This problem is resolved by modifying the SQL statement to use column name aliases.


    Figure 1–5 ResultSet Columns

    ResultSet Columns

  6. On the Add Prepared Statements window, click OK.

Specifying the OTD Name

Specify the name that your OTD will display in the Java CAPS IDE.

ProcedureTo Specify the OTD Name

  1. Enter a name for the OTD. The OTD contains the selected tables and the package name of the generated classes (see the following figure).

    Specify the OTD Name
  2. Click Next.

Reviewing Selections

Review the selections made for the new OTD.

ProcedureTo Review Your OTD Selections

  1. View the summary of the OTD. If you find you have made a mistake, click Back and correct the information.

  2. If you are satisfied with the OTD information, click Finish to begin generating the OTD (see the following figure).

    Figure 1–6 Database Wizard - Summary

    DB Wizard Summary

    The resulting OTD appears on the Java CAPS IDE’s canvas.