OpenSchema Method Example (VJ++)
This example uses the OpenSchema method to display the name and type of each table in the Pubs database.
import com.ms.wfc.data.*;
import java.io.*;
import com.ms.com.*;
public class OpenSchemaX
{
// The main entry point of the application.
public static void main (String[] args)
{
OpenSchemaX();
OpenSchemaX2();
System.exit(0);
}
// OpenSchemaX Function
static void OpenSchemaX()
{
// Define ADO Objects
Connection cnn1 = null;
Recordset rstSchema = null;
// Declarations
String strCnn;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int intDisplayRecords = 5;
int intRecordCount = 0;
try
{
cnn1 = new Connection();
strCnn = "Provider = Microsoft.Jet.OLEDB.4.0;" +
"Data Source=C:\\Program Files\\Microsoft " +
"Office\\Office\\Samples\\Northwind.mdb;" +
"User Id=admin;Password=;";
cnn1.open(strCnn);
rstSchema = cnn1.openSchema(AdoEnums.Schema.TABLES);
while (!rstSchema.getEOF())
{
System.out.println("Table Name: " +
chema.getField("TABLE_NAME").getString()+"\n"+
"Table Type: " +
chema.getField("TABLE_TYPE").getString()+"\n");
intRecordCount++;
if ( intRecordCount == intDisplayRecords)
{
System.out.println("Press <Enter> to continue..");
in.readLine();
intRecordCount = 0;
}
rstSchema.moveNext();
}
System.out.println("Press <Enter> to continue..");
in.readLine();
// Cleanup objects before exit
rstSchema.close();
cnn1.close();
}
catch(AdoException ae)
{
// Notify user of any errors that result from ADO.
// As passing a Recordset, check for null pointer first.
if(rstSchema != null)
{
PrintProviderError(rstSchema.getActiveConnection());
}
else
{
System.out.println("Exception: " + ae.getMessage());
}
}
// System read requires this catch.
catch(java.io.IOException je)
{
PrintIOError(je);
}
}
// OpenSchemaX2 Function
static void OpenSchemaX2()
{
// Define ADO Objects
Connection cnn2 = null;
Recordset rstSchema = null;
// Declarations
String strCnn;
BufferedReader in =
new BufferedReader(new InputStreamReader(System.in));
int intDisplayRecords = 5;
int intRecordCount = 0;
try
{
cnn2 = new Connection();
strCnn = "Provider = Microsoft.Jet.OLEDB.4.0;" +
"Data Source=C:\\Program Files\\Microsoft " +
"Office\\Office\\Samples\\Northwind.mdb;" +
"User Id=admin;Password=;";
cnn2.open(strCnn);
Variant[] va = new Variant[4];
va[0] = new Variant();
va[1] = new Variant();
va[2] = new Variant();
va[3] = new Variant("VIEW");
rstSchema = cnn2.openSchema(AdoEnums.Schema.TABLES,(Object[])va);
while (!rstSchema.getEOF())
{
System.out.println("Table Name: " +
rstSchema.getField("TABLE_NAME").getString()+"\n"+
"Table Type: " +
rstSchema.getField("TABLE_TYPE").getString()+"\n");
intRecordCount++;
if ( intRecordCount == intDisplayRecords)
{
System.out.println("Press <Enter> to continue..");
in.readLine();
intRecordCount = 0;
}
rstSchema.moveNext();
}
System.out.println("Press <Enter> to continue..");
in.readLine();
// Cleanup Objects before exit.
rstSchema.close();
cnn2.close();
}
catch(AdoException ae)
{
// Notify user of any errors that result from ADO.
// As passing a Recordset, check for null pointer first.
if(rstSchema != null)
{
PrintProviderError(rstSchema.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");
}
}