MDAC 2.5 SDK - ADO


 

MaxRecords Property Example (VJ++)

See Also

This example uses the MaxRecords property to open a Recordset containing the 10 most expensive titles in the Titles table.

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

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

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

static void MaxRecordsX()
{
   //  Define ADO Objects
   Recordset rstTemp = null;

   try
   {
      // Declarations
      BufferedReader in = 
         new BufferedReader(new InputStreamReader(System.in));

      // Open recordset containing the 10 most expensive
      // titles in the Titles table.
      String strCnn = " Provider=sqloledb;Data Source=srv;"+
         " Initial Catalog=Pubs;User Id=sa;Password=;";
      rstTemp = new Recordset();
      rstTemp.setMaxRecords(10);
      rstTemp.open("select title,price from Titles" + 
         " order by price desc", strCnn,AdoEnums.CursorType.FORWARDONLY, 
         AdoEnums.LockType.READONLY, AdoEnums.CommandType.TEXT);

      // Display the contents of the recordset.
      System.out.println("Top Ten Titles by Price:\n");
      while (!rstTemp.getEOF())
      {
         System.out.println(" "+ rstTemp.getField("title").getString() +
            " - " + rstTemp.getField("Price").getString());
         rstTemp.moveNext();
      }
      // Cleanup objects before exit.
      rstTemp.close();

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

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

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

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