Handler プロパティの例 (VJ++)

この例では、RDS.DataControl オブジェクトの Handler プロパティの機能を示します。詳細については、「DataFactory のカスタマイズ」を参照してください。

サーバー上のパラメータ ファイル MSDFMAP.INI に、次のセクションがあるとします。

[connect AuthorDataBase]
Access=ReadWrite
Connect="DSN=Pubs;UID=sa;PWD=;"
[sql AuthorById]
SQL="SELECT * FROM Authors WHERE au_id = ?"

コードは次のようになります。SQL プロパティに割り当てられたコマンドは識別子 AuthorById に一致するので、Michael O'Leary という author の行を取得します。コード中の Connect プロパティは Pubs データ ソースを指定していますが、このデータ ソースは、MSDFMAP.INI の connect セクションにより上書きされます。DataControl オブジェクトの Recordset プロパティは、コーディングの便宜上、接続されていない Recordset オブジェクトに割り当てられます。

import com.ms.wfc.data.*;
import com.ms.wfc.data.rds.*;
import java.io.* ;

public class HandlerX
{
    // The main entry point for the application.

    public static void main (String[] args)
    {
        HandlerX();
        System.exit(0);
    }

    // HandlerX function

    static void HandlerX()
    {

        // Define ADO Objects.
        Recordset rstAuthors = null;

        // Declarations.
        BufferedReader in = 
            new BufferedReader (new InputStreamReader(System.in));
        int intCount = 0;
        int intDisplaysize = 15;

        try
        {
            IBindMgr dc = (IBindMgr) new DataControl();
            dc.setHandler("MSDFMAP.Handler");
            dc.setServer("http://tcsp636");
            dc.setConnect("Data Source=AuthorDatabase");
            dc.setSQL("AuthorById('267-41-2394')");
            dc.Refresh();                        // Retrieve the record.
            // Use another recordset as a convenience.
            rstAuthors = (Recordset)dc.getRecordset();
            System.out.println("Author is '" +
                               rstAuthors.getField("au_fname").getString() +
                               " " +
                               rstAuthors.getField("au_lname").getString() +
                               "'");

            // Cleanup objects before exit.
            rstAuthors.close();
            System.out.println("\nPress <Enter> to continue..");
            in.readLine();
        }
        catch( AdoException ae )
        {
            // Notify user of any errors that result from ADO.

            // As passing a Recordset, check for null pointer first.
            if (rstAuthors != null)
            {
                PrintProviderError(rstAuthors.getActiveConnection());
            }
            else
            {
                System.out.println("Exception: " + ae.getMessage());
            }
        }

        // System read requires this catch.
        catch( java.io.IOException je)
        {
            PrintIOError(je);
        }
        catch(java.lang.UnsatisfiedLinkError e)
        {
            System.out.println("Exception: " + e.getMessage());
        }
        catch(java.lang.NullPointerException ne)
        {
            System.out.println(
            "Exception: Attempt to use null where an object is required.");
        }
    }

    // PrintProviderError Function

    static void PrintProviderError( Connection Cnn1 )
    {
        // Print Provider errors from Connection object.
        // ErrItem is an item object in the Connection’s Errors collection.
        com.ms.wfc.data.Error  ErrItem = null;
        long nCount = 0;
        int  i      = 0;

        nCount = Cnn1.getErrors().getCount();

        // If there are any errors in the collection, print them.
        if( nCount > 0);
        {
            // Collection ranges from 0 to nCount - 1
            for (i = 0; i< nCount; i++)
            {
                ErrItem = Cnn1.getErrors().getItem(i);
                System.out.println("\t Error number: " + ErrItem.getNumber()
                    + "\t" + ErrItem.getDescription() );
            }
        }

    }

    // PrintIOError Function

    static void PrintIOError( java.io.IOException je)
    {
        System.out.println("Error \n");
        System.out.println("\tSource = " + je.getClass() + "\n");
        System.out.println("\tDescription = " + je.getMessage() + "\n");
    }
}