This example demonstrates the Provider property by opening three Connection objects using different providers. It also uses the DefaultDatabase property to set the default database for the Microsoft ODBC Provider.
import java.io.*;
import com.ms.wfc.data.*;
public class ProviderX
{
// The main entry point of the application.
public static void main (String[] args)
{
ProviderX();
System.exit(0);
}
// ProviderX Function
static void ProviderX()
{
// Define ADO Objects.
Connection cnn1 = null;
Connection cnn2 = null;
Connection cnn3 = null;
// Declarations.
BufferedReader in =
new BufferedReader(new InputStreamReader(System.in));
try
{
// Open a connection using the Microsoft ODBC Provider.
cnn1 = new Connection();
cnn1.setConnectionString("driver={SQL Server};"+
"server=srv;User id=sa;Password=;");
cnn1.open();
cnn1.setDefaultDatabase("Pubs");
// Display the provider.
System.out.println("\n\n\tCnn1 provider: "+ cnn1.getProvider());
// Open connection using the OLE DB Provider for Microsoft Jet.
cnn2 = new Connection();
cnn2.setProvider("Microsoft.Jet.OLEDB.3.51");
cnn2.open("C:\\Program Files\\Microsoft "
"Office\\Office\\Samples\\Northwind.mdb","admin","");
// Display the provider.
System.out.println("\n\n\tCnn2 provider: " +
cnn2.getProvider());
// Open a connection using the Microsoft SQL Server Provider.
cnn3 = new Connection();
cnn3.setProvider("sqloledb");
cnn3.open("Data Source=srv;Initial Catalog= Pubs;","sa","");
// Display the provider.
System.out.println("\n\n\tCnn3 provider: " +
cnn3.getProvider());
// Cleanup Objects before exit.
cnn1.close();
cnn2.close();
cnn3.close();
System.out.println("\n\n\tPress <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(cnn1 != null)
{
PrintProviderError(cnn1);
}
else
{
System.out.println("Exception: " + ae.getLocalizedMessage());
}
}
// System read requires needs 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");
}
}