/******************************************************************************* ** ** Source File Name = dbmconn.c ** ** Licensed Materials - Property of IBM ** ** (C) COPYRIGHT International Business Machines Corp. 1995, 2000 ** All Rights Reserved. ** ** US Government Users Restricted Rights - Use, duplication or ** disclosure restricted by GSA ADP Schedule Contract with IBM Corp. ** ** ** PURPOSE : ** Shows how connect to/disconnect from multiple databases. ** ** For more information about these samples see the README file. ** ** For more information on programming in CLI see the: ** - "Building CLI Applications" section of the Application Building Guide, and the ** - CLI Guide and Reference. ** ** For more information on the SQL language see the SQL Reference. ** *******************************************************************************/ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <sqlcli1.h> #include "utilcli.h" #include "utilapi.h" int TwoDBconnectType1( SQLHANDLE , char *, char *, char *, char * ) ; int TwoDBconnectType2( SQLHANDLE , char *, char *, char *, char * ) ; #define MAX_CONNECTIONS 2 /******************************************************************* ** main *******************************************************************/ int main( int argc, char * argv[] ) { SQLRETURN sqlrc = SQL_SUCCESS; int rc = 0; SQLHANDLE henv; /* environment handle */ SQLHANDLE hdbc; /* connection handle */ char dbAlias[SQL_MAX_DSN_LENGTH + 1] ; char user[MAX_UID_LENGTH + 1] ; char pswd[MAX_PWD_LENGTH + 1] ; char remoteNodeName[ 10 ] ; char db2Name[SQL_MAX_DSN_LENGTH + 1] = "SAMPLE2" ; char db2Alias[SQL_MAX_DSN_LENGTH + 1] = "SAMPLE2" ; /* checks the command line arguments */ rc = CmdLineArgsCheck2( argc, argv, dbAlias, user, pswd, remoteNodeName ); if ( rc != 0 ) { return( 1 ) ; } printf("\n\nDATABASES: HOW TO CONNECT TO/DISCONNECT FROM\n"); printf(" MULTIPLE DATABASES.\n"); /* allocate an environment handle */ sqlrc = SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv ) ; if ( sqlrc != SQL_SUCCESS ) { printf( "\n--ERROR while allocating the environment handle.\n" ) ; printf( " sqlrc = %d\n", sqlrc); printf( " line = %d\n", __LINE__); printf( " file = %s\n", __FILE__); return( 1 ) ; } /* create and catalog a second database */ /* using administrative APIs*/ rc = DbCreate( remoteNodeName, user, pswd, db2Name, db2Alias ) ; if( rc == 0) { /* connect to two databases using Connect Type 2 */ rc = TwoDBconnectType2( henv, dbAlias, db2Alias, user, pswd ) ; if ( rc != 0 ) { DbDrop( remoteNodeName, user, pswd, db2Alias ); return( rc ) ; } /* connect to two databases using Connect Type 1 */ rc = TwoDBconnectType1( henv, dbAlias, db2Alias, user, pswd ) ; if ( rc != 0 ) { DbDrop( remoteNodeName, user, pswd, db2Alias ); return( rc ) ; } /* drop and uncatalog the second database */ /* using administrative APIs */ rc = DbDrop( remoteNodeName, user, pswd, db2Alias ) ; } /* free the environment handle */ sqlrc = SQLFreeHandle( SQL_HANDLE_ENV, henv ) ; ENV_HANDLE_CHECK( henv, sqlrc ) ; return( 0 ) ; } /* end main */ /****************************************************************************** ** TwoDBconnectType1 - connect/disconnect to/from two databases ** using the Connect Type 1 ******************************************************************************/ int TwoDBconnectType1( SQLHANDLE henv, char db1Alias[], char db2Alias[], char user[], char pswd[] ) { SQLRETURN sqlrc = SQL_SUCCESS; int rc = 0; SQLHANDLE a_hdbc[MAX_CONNECTIONS] ; /* array of connection handles */ printf("\nUSE THE CLI FUNCTIONS\n"); printf("-SQLAllocHandle\n-SQLConnect\n"); printf("-SQLDisconnect\n-SQLFreeHandle\n"); printf("TO CONNECT TO/DISCONNECT FROM TWO DATABASES\n"); printf("USING THE CONNECT TYPE 1:\n"); /* allocate the database connection handles */ sqlrc = SQLAllocHandle( SQL_HANDLE_DBC, henv, &a_hdbc[0] ) ; MC_HANDLE_CHECK( SQL_HANDLE_DBC, a_hdbc[0], sqlrc, &henv, a_hdbc, MAX_CONNECTIONS ) ; sqlrc = SQLAllocHandle( SQL_HANDLE_DBC, henv, &a_hdbc[1] ) ; MC_HANDLE_CHECK( SQL_HANDLE_DBC, a_hdbc[1], sqlrc, &henv, a_hdbc, MAX_CONNECTIONS ) ; /* connect to both databases */ printf( "\n Connecting to the database %s ...\n", db1Alias ) ; sqlrc = SQLConnect( a_hdbc[0], (SQLCHAR *)db1Alias, SQL_NTS, (SQLCHAR *)user, SQL_NTS, (SQLCHAR *)pswd, SQL_NTS ) ; MC_HANDLE_CHECK( SQL_HANDLE_DBC, a_hdbc[0], sqlrc, &henv, a_hdbc, MAX_CONNECTIONS ) ; printf( " Connected to the database %s.\n", db1Alias ) ; printf( "\n Connecting to the database %s ...\n", db2Alias ) ; sqlrc = SQLConnect( a_hdbc[1], (SQLCHAR *)db2Alias, SQL_NTS, (SQLCHAR *)user, SQL_NTS, (SQLCHAR *)pswd, SQL_NTS ) ; MC_HANDLE_CHECK( SQL_HANDLE_DBC, a_hdbc[1], sqlrc, &henv, a_hdbc, MAX_CONNECTIONS ) ; printf( " Connected to the database %s.\n", db2Alias ) ; /********* Start using the connections ************************/ /********* Stop using the connections *************************/ /* disconnect from both databases */ printf( "\n Disconnecting from the database %s ...\n", db1Alias ) ; sqlrc = SQLDisconnect( a_hdbc[0] ) ; MC_HANDLE_CHECK( SQL_HANDLE_DBC, a_hdbc[0], sqlrc, &henv, a_hdbc, MAX_CONNECTIONS ) ; printf( " Disconnected from the database %s.\n", db1Alias ) ; printf( "\n Disconnecting from the database %s ...\n", db2Alias ) ; sqlrc = SQLDisconnect( a_hdbc[1] ) ; MC_HANDLE_CHECK( SQL_HANDLE_DBC, a_hdbc[1], sqlrc, &henv, a_hdbc, MAX_CONNECTIONS ) ; printf( " Disconnected from the database %s.\n", db2Alias ) ; /* free the connection handles */ sqlrc = SQLFreeHandle( SQL_HANDLE_DBC, a_hdbc[0] ) ; MC_HANDLE_CHECK( SQL_HANDLE_DBC, a_hdbc[0], sqlrc, &henv, a_hdbc, MAX_CONNECTIONS ) ; sqlrc = SQLFreeHandle( SQL_HANDLE_DBC, a_hdbc[1] ) ; MC_HANDLE_CHECK( SQL_HANDLE_DBC, a_hdbc[1], sqlrc, &henv, a_hdbc, MAX_CONNECTIONS ) ; return( 0 ) ; } /****************************************************************************** ** TwoDBconnectType2 - connect/disconnect to/from two databases ** using the Connect Type 2 ******************************************************************************/ int TwoDBconnectType2( SQLHANDLE henv, char db1Alias[], char db2Alias[], char user[], char pswd[] ) { SQLRETURN sqlrc = SQL_SUCCESS; int rc = 0; SQLHANDLE a_hdbc[MAX_CONNECTIONS] ; /* array of connection handles */ printf("\nUSE THE CLI FUNCTIONS\n"); printf("-SQLAllocHandle\n-SQLSetConnectAttr\n-SQLConnect\n"); printf("-SQLDisconnect\n-SQLFreeHandle\n"); printf("TO CONNECT TO/DISCONNECT FROM TWO DATABASES\n"); printf("USING THE CONNECT TYPE 2:\n"); /* allocate the database connection handles */ sqlrc = SQLAllocHandle( SQL_HANDLE_DBC, henv, &a_hdbc[0] ) ; MC_HANDLE_CHECK( SQL_HANDLE_DBC, a_hdbc[0], sqlrc, &henv, a_hdbc, MAX_CONNECTIONS ) ; sqlrc = SQLAllocHandle( SQL_HANDLE_DBC, henv, &a_hdbc[1] ) ; MC_HANDLE_CHECK( SQL_HANDLE_DBC, a_hdbc[1], sqlrc, &henv, a_hdbc, MAX_CONNECTIONS ) ; /* set CONNECT TYPE 2, SYNCPOINT 1 for the both connections */ sqlrc = SQLSetConnectAttr( a_hdbc[0], SQL_CONNECTTYPE, ( SQLPOINTER ) SQL_COORDINATED_TRANS, SQL_NTS) ; MC_HANDLE_CHECK( SQL_HANDLE_DBC, a_hdbc[0], sqlrc, &henv, a_hdbc, MAX_CONNECTIONS ) ; sqlrc = SQLSetConnectAttr( a_hdbc[0], SQL_SYNC_POINT, ( SQLPOINTER ) SQL_ONEPHASE, SQL_NTS) ; MC_HANDLE_CHECK( SQL_HANDLE_DBC, a_hdbc[0], sqlrc, &henv, a_hdbc, MAX_CONNECTIONS ) ; sqlrc = SQLSetConnectAttr( a_hdbc[1], SQL_CONNECTTYPE, ( SQLPOINTER ) SQL_COORDINATED_TRANS, SQL_NTS) ; MC_HANDLE_CHECK( SQL_HANDLE_DBC, a_hdbc[1], sqlrc, &henv, a_hdbc, MAX_CONNECTIONS ) ; sqlrc = SQLSetConnectAttr( a_hdbc[1], SQL_SYNC_POINT, ( SQLPOINTER ) SQL_ONEPHASE, SQL_NTS) ; MC_HANDLE_CHECK( SQL_HANDLE_DBC, a_hdbc[1], sqlrc, &henv, a_hdbc, MAX_CONNECTIONS ) ; /* connect to both databases */ printf( "\n Connecting to the database %s ...\n", db1Alias ) ; sqlrc = SQLConnect( a_hdbc[0], (SQLCHAR *)db1Alias, SQL_NTS, (SQLCHAR *)user, SQL_NTS, (SQLCHAR *)pswd, SQL_NTS ) ; MC_HANDLE_CHECK( SQL_HANDLE_DBC, a_hdbc[0], sqlrc, &henv, a_hdbc, MAX_CONNECTIONS ) ; printf( " Connected to the database %s.\n", db1Alias ) ; printf( "\n Connecting to the database %s ...\n", db2Alias ) ; sqlrc = SQLConnect( a_hdbc[1], (SQLCHAR *)db2Alias, SQL_NTS, (SQLCHAR *)user, SQL_NTS, (SQLCHAR *)pswd, SQL_NTS ) ; MC_HANDLE_CHECK( SQL_HANDLE_DBC, a_hdbc[1], sqlrc, &henv, a_hdbc, MAX_CONNECTIONS ) ; printf( " Connected to the database %s.\n", db2Alias ) ; /********* Start using the connections ************************/ /********* Stop using the connections *************************/ /* disconnect from both databases */ printf( "\n Disconnecting from the database %s ...\n", db1Alias ) ; sqlrc = SQLDisconnect( a_hdbc[0] ) ; MC_HANDLE_CHECK( SQL_HANDLE_DBC, a_hdbc[0], sqlrc, &henv, a_hdbc, MAX_CONNECTIONS ) ; printf( " Disconnected from the database %s.\n", db1Alias ) ; printf( "\n Disconnecting from the database %s ...\n", db2Alias ) ; sqlrc = SQLDisconnect( a_hdbc[1] ) ; MC_HANDLE_CHECK( SQL_HANDLE_DBC, a_hdbc[1], sqlrc, &henv, a_hdbc, MAX_CONNECTIONS ) ; printf( " Disconnected from the database %s.\n", db2Alias ) ; /* free the connection handles */ sqlrc = SQLFreeHandle( SQL_HANDLE_DBC, a_hdbc[0] ) ; MC_HANDLE_CHECK( SQL_HANDLE_DBC, a_hdbc[0], sqlrc, &henv, a_hdbc, MAX_CONNECTIONS ) ; sqlrc = SQLFreeHandle( SQL_HANDLE_DBC, a_hdbc[1] ) ; MC_HANDLE_CHECK( SQL_HANDLE_DBC, a_hdbc[1], sqlrc, &henv, a_hdbc, MAX_CONNECTIONS ) ; return( 0 ) ; }