Using Database Operations

The Table

A table OTD represents a database table. It consists of fields and methods. Fields correspond to the columns of a table while methods are the operations that you can apply to the OTD. This allows you to perform query, update, insert, and delete SQL operations in a table. The ability to update via a resultset is called “Updatable Resultset”, which is a feature supported by this adapter if the Type 4 Universal driver is used (for alternate methods for the Type 2 Legacy driver refer to Prepared Statement).


Note –

The DB2 Connect Universal Driver only supports Updatable Resultsets for Update and Delete. The Insert operation is not supported. You can use a Prepared Statement to perform the Insert operation.


By default, the Table OTD has UpdatableConcurrency and ScrollTypeForwardOnly. Normally you do not have to change the default setting.

The type of result returned by the select() method can be specified using:

The Query (Select) Operation

ProcedureTo perform a query operation on a table

  1. Execute the select() method with the “where” clause specified if necessary.


    Note –

    The content of the input.getText() file may contain null, meaning it will not have a “where” clause or it can contain a “where” clause such as empno > 50.


  2. Loop through the ResultSet using the next() method.

  3. Process the return record within a while() loop.

    For example:


    package prjDB2Connect_JCDjcdALL;
    
    
    public class jcdTableSelect
    {
    
        public com.stc.codegen.logger.Logger logger;
    
        public com.stc.codegen.alerter.Alerter alerter;
    
        public com.stc.codegen.util.CollaborationContext collabContext;
    
        public com.stc.codegen.util.TypeConverter typeConverter;
    
        public void receive( com.stc.connector.appconn.file.FileTextMessage
     input, dtd.otdOutputDTD1325973702.DB_Employee otdOutputDTD_DB_Employee_1,
     otdDB2Connect.OtdDB2ConnectOTD otdDB2Connect_1, com.stc.connector.
    appconn.file.FileApplication FileClient_1 )
            throws Throwable
        {
            FileClient_1.setText( "Selectiong records from db_employee table
     via Table Select........" );
            FileClient_1.write();
            otdDB2Connect_1.getDb_employee().select( input.getText() );
            while (otdDB2Connect_1.getDb_employee().next()) {
                otdOutputDTD_DB_Employee_1.setEmpNo( typeConverter.shortToString(
     otdDB2Connect_1.getDb_employee().getEMP_NO(), "#", false, "" ) );
                otdOutputDTD_DB_Employee_1.setLastname( otdDB2Connect_1.
    getDb_employee().getLAST_NAME() );
                otdOutputDTD_DB_Employee_1.setFirstname( otdDB2Connect_1.
    getDb_employee().getFIRST_NAME() );
                otdOutputDTD_DB_Employee_1.setRate( otdDB2Connect_1.
    getDb_employee().getRATE().toString() );
                otdOutputDTD_DB_Employee_1.setLastDate( typeConverter.
    dateToString( otdDB2Connect_1.getDb_employee().getLAST_UPDATE(),
     "yyyy-MM-dd hh:mm:ss", false, "" ) );
                FileClient_1.setText( otdOutputDTD_DB_Employee_1.marshalToString() );
                FileClient_1.write();
            }
            FileClient_1.setText( "Table Select Done." );
            FileClient_1.write();
        }
    
    }

The Update Operation

ProcedureTo perform an update operation on a table

  1. Execute the update() method.


    Note –

    The content of the input.getText() file may contain null, meaning it will not have a “where” clause or it can contain a “where” clause such as empno > 50.


  2. Using a while loop together with next(), move to the row that you want to update.

  3. Assign updating value(s) to the fields of the table OTD

  4. Update the row by calling updateRow().


    package prjDB2Connect_JCDjcdALL;
    
    
    public class jcdUpdate
    {
    
    public com.stc.codegen.logger.Logger logger;
    public com.stc.codegen.alerter.Alerter alerter;
    public com.stc.codegen.util.CollaborationContext collabContext;
    public com.stc.codegen.util.TypeConverter typeConverter;
    
        public void receive( com.stc.connector.appconn.file.FileTextMessage
     input, otdDB2Connect.OtdDB2ConnectOTD otdDB2Connect_1, com.stc.connector.
    appconn.file.FileApplication FileClient_1 )
            throws Throwable
        {
            FileClient_1.setText( "Updating the Rate and Last_update fields .. " );
            FileClient_1.write();
            otdDB2Connect_1.getDb_employee().update( input.getText() );
            while (otdDB2Connect_1.getDb_employee().next()) {
            otdDB2Connect_1.getDb_employee().setLAST_NAME( "Krishna" );
            otdDB2Connect_1.getDb_employee().setFIRST_NAME( "Kishore" );
            otdDB2Connect_1.getDb_employee().updateRow();
            }
            FileClient_1.setText( "Update Done." );
            FileClient_1.write();
        }
    
    }

The Delete Operation

ProcedureTo perform a delete operation on a table

  1. Execute the delete() method.


    Note –

    The content of the input.getText() file may contain null, meaning it will not have a “where” clause or it can contain a “where” clause such as empno > 50.


    In this example DELETE an employee.


    package prjDB2Connect_JCDjcdALL;
    
    public class jcdDelete
    {
    public com.stc.codegen.logger.Logger logger;
    public com.stc.codegen.alerter.Alerter alerter;
    public com.stc.codegen.util.CollaborationContext collabContext;
    public com.stc.codegen.util.TypeConverter typeConverter;
    
        public void receive( com.stc.connector.appconn.file.FileTextMessage
     input, otdDB2Connect.OtdDB2ConnectOTD otdDB2Connect_1, com.stc.connector.
    appconn.file.FileApplication FileClient_1 )
            throws Throwable
        {
            FileClient_1.setText( "Deleting record............" );
            FileClient_1.write();
            otdDB2Connect_1.getDb_employee().delete( input.getText() );
            FileClient_1.setText( "Delete Done." );
            FileClient_1.write();
        }
    }