ActiveConnection, CommandText, CommandTimeout, CommandType, Size, and Direction Properties Example (VJ++)

Microsoft ActiveX Data Objects (ADO)

ActiveConnection, CommandText, CommandTimeout, CommandType, Size, and Direction Properties Example (VJ++)

This example uses the ActiveConnection, CommandText, CommandTimeout, CommandType, Size, and Direction properties to execute a stored procedure.

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

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

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

    //.ActiveConnectionX function

    static void ActiveConnectionX()
    {
        // Define ADO Objects.
        Connection cnConn1 = null;
        Command cmdByRoyalty = null;
        Parameter prmByRoyalty = null;
        Recordset rstByRoyalty = null;
        Recordset rstAuthors = null;

        //Declarations.
        String strCnn;
        String strAuthorID;
        String strFName;
        String strLName;
        int intRoyalty ;
        BufferedReader in = new BufferedReader 
            (new InputStreamReader (System.in));
        String line = null;

        try
        {
            // Open a connection.

            strCnn = "Provider=sqloledb;Data Source=srv;"
                + "Initial Catalog=pubs;User Id=sa;Password=;";
            cnConn1 = new Connection();
            cnConn1.open(strCnn,"","",AdoEnums.CommandType.UNSPECIFIED);

            // Define a command object for stored procedure.
            cmdByRoyalty = new Command();
            cmdByRoyalty.setActiveConnection(cnConn1);
            cmdByRoyalty.setCommandText("byRoyalty");
            cmdByRoyalty.setCommandType(AdoEnums.CommandType.STOREDPROC);
            cmdByRoyalty.setCommandTimeout(15);

            //Define the stored procedure's input parameter.
            System.out.println ("\nEnter Royalty : ");
            line = in.readLine().trim();
            intRoyalty = Integer.parseInt(line);
            prmByRoyalty = new Parameter ();
            prmByRoyalty.setType(AdoEnums.DataType.INTEGER);
            prmByRoyalty.setSize(3);
            prmByRoyalty.setDirection(AdoEnums.ParameterDirection.INPUT);
            prmByRoyalty.setValue(new Integer(intRoyalty));
            cmdByRoyalty.getParameters().append(prmByRoyalty);

            // Create a recordset by executing the command.

            rstByRoyalty = cmdByRoyalty.execute();

            // Open the Authors table to get author names for display.
            rstAuthors = new Recordset ();
            rstAuthors.open("authors",strCnn,
                AdoEnums.CursorType.FORWARDONLY,
                AdoEnums.LockType.READONLY ,AdoEnums.CommandType.TABLE );

            // Print current data in the recordset,
            // adding author names from Authors table.
            System.out.println("\nAuthors with " + intRoyalty + 
                " percent royalty");
            while (!rstByRoyalty.getEOF())
            {
                strAuthorID =  rstByRoyalty.getField("au_id").getString();
                rstAuthors.setFilter("au_id ='" +  strAuthorID + "'");
                strFName = rstAuthors.getField("au_fname").getString();
                strLName = rstAuthors.getField("au_lname").getString();
                System.out.println("\t" + strAuthorID + ", " + strFName 
                    + " " + strLName);
                rstByRoyalty.moveNext();
            }
            System.out.println("\n\nPress <Enter> key to continue.");
            line = in.readLine();

            //Cleanup objects before exit.
            rstByRoyalty.close();
            rstAuthors.close();
            cnConn1.close();

        }
        catch( AdoException ae )
        {
            // Notify user of any errors that result from ADO.

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

        // This catch is required if input string cannot be converted to
        // Integer data type.
        catch ( java.lang.NumberFormatException ne)
        {
            System.out.println("\nException: Integer Input required.");
        }
        // 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");
    }
}