This example demonstrates the Count property with two collections in the Employees database. The property obtains the number of objects in each collection, and sets the upper limit for loops that enumerate these collections. Another way to enumerate these collections without using the Count property would be to use For Each...Next
statements.
import com.ms.wfc.data.*;
import java.io.* ;
public class CountX
{
// The main entry point for the application.
public static void main (String[] args)
{
CountX();
System.exit(0);
}
// CountX function
static void CountX()
{
// Define ADO Objects.
Recordset rstEmployees = 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 intLoop;
int intDisplaySize = 20;
int recCount=0;
try
{
rstEmployees = new Recordset();
// Open recordset with data from Employees table.
rstEmployees.open("employee", strCnn,
AdoEnums.CursorType.FORWARDONLY,
AdoEnums.LockType.READONLY,
AdoEnums.CommandType.TABLE);
// Print information about Fields collection.
System.out.println(rstEmployees.getFields().getCount() +
" Fields in Employees");
for ( intLoop = 0; intLoop <
rstEmployees.getFields().getCount(); intLoop++)
{
System.out.println("\t" +
rstEmployees.getFields().getItem(intLoop).getName());
}
System.out.println("\n\nPress <Enter> to continue..");
in.readLine();
// Print information about Properties collection.
System.out.println(rstEmployees.getProperties().getCount() +
" Properties in Employees");
for ( intLoop = 0; intLoop <
rstEmployees.getProperties().getCount(); intLoop++)
{
System.out.println("\t" +
rstEmployees.getProperties().getItem(intLoop).getName());
recCount++;
if ( recCount >= intDisplaySize)
{
System.out.println("\n\nPress <Enter> to continue..");
in.readLine();
recCount = 0;
}
}
System.out.println("\n\nPress <Enter> to continue..");
in.readLine();
// Cleanup objects before exit.
rstEmployees.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());
}
else
{
// 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");
}
}