This example demonstrates different ways of using the ConnectionString property to open a Connection object. It also uses the ConnectionTimeout property to set a connection timeout period, and the State property to check the state of the connections. The GetState function is required for this procedure to run.
// BeginConnectionStringJ import com.ms.wfc.data.*; import java.io.* ; public class ConnectionStringX { // The main entry point for the application. public static void main (String[] args) { ConnectionStringX(); System.exit(0); } // ConnectionStringX function static void ConnectionStringX() { // Define ADO Objects. Connection cnConn1 = null; Connection cnConn2 = null; Connection cnConn3 = null; Connection cnConn4 = null; //Declarations. BufferedReader in = new BufferedReader (new InputStreamReader(System.in)); String line = null; String strTemp; try { // Open a connection using OLE DB syntax. cnConn1 = new Connection(); cnConn1.setConnectionString("provider=SQLOLEDB;" + "Data Source=MyServer;Initial Catalog=Pubs;" + "User Id=sa;Password=;"); cnConn1.setCommandTimeout(30); cnConn1.open(); // Open a connection using a DSN and ODBC tags. cnConn2 = new Connection(); cnConn2.setConnectionString("DSN=Pubs;UID=sa;PWD=;"); cnConn2.open(); // Open a connection using a DSN and OLE DB tags. cnConn3 = new Connection(); cnConn3.setConnectionString ("Data Source=Pubs;User ID=sa;Password=;"); cnConn3.open(); // Open a connection using a DSN and individual // arguments instead of a connection string. cnConn4 = new Connection(); cnConn4.open("Pubs", "sa", ""); // Display the state of the connections. strTemp = getState(cnConn1.getState()); System.out.println("CnConn1 state: " + strTemp); strTemp = getState(cnConn2.getState()); System.out.println("CnConn2 state: " + strTemp); strTemp = getState(cnConn3.getState()); System.out.println("CnConn3 state: " + strTemp); strTemp = getState(cnConn4.getState()); System.out.println("CnConn4 state: " + strTemp); System.out.println("\n\nPress <Enter> to continue.."); in.readLine(); // Cleanup objects before exit. cnConn1.close(); cnConn2.close(); cnConn3.close(); cnConn4.close(); } catch( AdoException ae ) { // Notify user of any errors that result from ADO. System.out.println("Exception: " + ae.getMessage()); } // System read requires this catch. catch( java.io.IOException je) { PrintIOError(je); } } // getState Function static String getState(int intState) { // Returns current state of the connection object. String strState=null; switch(intState) { case AdoEnums.ObjectState.CLOSED : strState = new String("adStateClosed"); break; case AdoEnums.ObjectState.OPEN : strState = new String("adStateOpen"); break; default : break; } return strState; } // 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"); } } // EndConnectionStringJ
Connection Object | ConnectionString Property | ConnectionTimeout Property | State Property
© 1998-2001 Microsoft Corporation. All rights reserved.