Attributes and Name Properties Example (VJ++)

Microsoft ActiveX Data Objects (ADO)

Attributes and Name Properties Example (VJ++)

This example displays the value of the Attributes property for Connection, Field, and Property objects. It uses the Name property to display the name of each Field and Property object.

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

public class AttributesX
{
    // The main entry point for the application
    public static void main (String[] args)
    {
        AttributesX();
        System.exit(0);
    }

    // AttributeX Function

    static void AttributesX()
    {
        // Define ADO Objects.
        Connection cnConn1 = null;
        Recordset rstEmployees = null;
        Fields listOfFields = null;
        AdoProperties listOfProperties = null;

        //Declarations.
        BufferedReader in = 
            new BufferedReader (new InputStreamReader(System.in));
        String line = null;
        String strCnn = "Provider=sqloledb;Data Source=srv;"
            + "Initial Catalog=pubs;User Id=sa;Password=;";
        int recordDisplaySize = 15;
        int propertyCount = 0;
        try
        {
            // Open connection and recordset.
            cnConn1 = new Connection();
            cnConn1.open(strCnn);

            rstEmployees = new Recordset ();
            rstEmployees.open("employee", 
                cnConn1,AdoEnums.CursorType.FORWARDONLY, 
                AdoEnums.LockType.READONLY, AdoEnums.CommandType.TABLE);

            // Display the attributes of the connection.
            System.out.println("Connection attributes = " 
                + cnConn1.getAttributes());
            // Display the attributes of the Employees table's fields.
            System.out.println("\n\nField attributes : " + "\n");

            listOfFields = rstEmployees.getFields();

            for ( int i=0; i < listOfFields.getCount();i++)
            {
                System.out.println("\t\t" + listOfFields.getItem(i).getName()
                    + " = " + listOfFields.getItem(i).getAttributes());
            }

            // Display fields of the Employees table which are NULLABLE.
            System.out.println("\n\nNULLABLE Fields : " + "\n");
            for ( int i=0; i < listOfFields.getCount();i++)
            {
                if ((listOfFields.getItem(i).getAttributes() & 
                    AdoEnums.FieldAttribute.ISNULLABLE) > 0)
                    System.out.println("\t\t" + 
                    listOfFields.getItem(i).getName());

            }

            System.out.println ("\n\nPress <Enter> key to continue..");
            line = in.readLine();

            // Display the attributes of the Employees table's properties.
            System.out.println("\n\nProperty attributes : " );
            listOfProperties = rstEmployees.getProperties();
            for ( int i=0; i < listOfProperties.getCount() ;i++)
            {
                System.out.println("\t\t" + 
                    listOfProperties.getItem(i).getName()
                    + " = " + listOfProperties.getItem(i).getAttributes());

                if (propertyCount == recordDisplaySize)
                {
                    System.out.println ("\n\nPress <Enter> key to continue.");
                    line = in.readLine();
                    propertyCount = 0;
                }
                propertyCount++;
            }

            System.out.println ("\n\nPress <Enter> key to continue.");
            line = in.readLine();

            //Cleanup objects before exit.
            rstEmployees.close();
            cnConn1.close();
        }
        catch( AdoException ae )
        {
            // Notify user of any errors that result from ADO.

            // Check for null pointer for connection object.
            if (rstEmployees.getActiveConnection()==null)
                System.out.println("Exception: " + ae.getMessage());

            // As passing a Recordset, check for null pointer first.

            if (rstEmployees != null)
            {
                PrintProviderError(rstEmployees.getActiveConnection());
            }
            else
            {
                System.out.println("Exception: " + ae.getMessage());
            }
        }

        // System read requires this catch.
        catch( java.io.IOException je)
        {
            PrintIOError(je);
        }
    }

    // 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");
    }
}