#include #include #include #include using namespace std; MYSQL dbConnection; string dbHost = "10.34.77.64"; string dbUser = "root"; string dbPass = ""; string schema = "HSSPROD"; int port = 3306; const char *unix_socket = NULL; MYSQL* l_dbConn = NULL; class SQLException : public std::exception { public: char error[250]; unsigned int errNumber; char errString[201]; virtual ~SQLException() throw(){} SQLException(int errNumber_,char *errString_):errNumber(errNumber_) { if(strlen(errString_) <= 200) strcpy(errString,errString_); else { strncpy(errString,errString_,200); errString[200] = '\0'; } sprintf(error,"%d",errNumber); strcat(error,":"); if(strlen(errString_) <= 200) strcat(error,errString); else { strncpy(errString,errString_,200); errString[200] = '\0'; } } virtual const char * what() const throw() { return error;} SQLException() { } }; int querryDataBase() throw (SQLException &, exception &);; int main() { unsigned long client_flag = CLIENT_MULTI_STATEMENTS | CLIENT_INTERACTIVE; my_bool reconnectFlag = 1; unsigned int readTimeOut = 5; if (mysql_library_init(0, NULL, NULL)) { printf("[%s::%d] -- > MySQL Library not initialized \n" , __FILE__,__LINE__); exit(0); } dbConnection = *(mysql_init(&dbConnection)); mysql_options(&dbConnection, MYSQL_OPT_RECONNECT, &reconnectFlag); mysql_options(&dbConnection,MYSQL_OPT_READ_TIMEOUT, &readTimeOut); l_dbConn = mysql_real_connect(&dbConnection,dbHost.c_str(),dbUser.c_str(),dbPass.c_str(),schema.c_str(),port,unix_socket,client_flag); if(l_dbConn == NULL) { printf(" Could not establish connection with MYSQL Server \n" ); exit(0); } else printf(" connection established with MYSQL Server \n" ); while(true) { try { querryDataBase(); } catch(...) { printf(" Exception while Executing querry \n" ); } } } #define GET_PRIVATE_IDENTITY_DATA "SELECT subscription_id from PRIVATE_IDENTITY where private_identity =? " string querryString = "SELECT subscription_id from PRIVATE_IDENTITY where private_identity = 'test_1_LIR001_impi@rancoretech.com' " ; int querryDataBase() throw (SQLException&, std::exception&) { MYSQL_STMT *l_stmt = NULL; MYSQL_BIND l_outputBind[1]; unsigned long l_length; my_bool l_isNull; my_bool l_error; int l_countRow = 0; long counter = 0; try { /* Use any MySQL API functions here */ l_stmt = mysql_stmt_init(&dbConnection); if (!l_stmt) { printf("\n [ %s::%d ]-->Exception mysql_stmt_init(),MySQL client ran out of memory\n",__FILE__,__LINE__); throw SQLException(2008,": mysql_stmt_init(),MySQL client ran out of memory"); } { string l_query = "select count(1) from IMS_SUBSCRIPTION"; if (mysql_stmt_prepare(l_stmt,l_query.c_str(),strlen(l_query.c_str())) != 0) { printf("\n [ %s::%d ]-->Exception mysql_stmt_prepare(), SELECT failed\n",__FILE__,__LINE__); printf("%d: %s \n",mysql_stmt_errno(l_stmt),mysql_stmt_error(l_stmt)); throw SQLException(mysql_stmt_errno(l_stmt),(char*)mysql_stmt_error(l_stmt)); } if (mysql_stmt_execute(l_stmt)) { printf("\n [ %s::%d ]-->Exception mysql_stmt_execute() failed\n",__FILE__,__LINE__); printf("%d: %s \n",mysql_stmt_errno(l_stmt),mysql_stmt_error(l_stmt)); throw SQLException(mysql_stmt_errno(l_stmt),(char*)mysql_stmt_error(l_stmt)); } memset(l_outputBind,0,sizeof(l_outputBind)); l_outputBind[0].buffer_type = MYSQL_TYPE_LONG; l_outputBind[0].buffer = (char*)&counter ; l_outputBind[0].is_null = &l_isNull; l_outputBind[0].length = &l_length; l_outputBind[0].error = &l_error; if (mysql_stmt_bind_result(l_stmt,l_outputBind)) { printf("\n [ %s::%d ]-->Exception mysql_stmt_bind_result() failed\n",__FILE__,__LINE__); printf("%d: %s \n",mysql_stmt_errno(l_stmt),mysql_stmt_error(l_stmt)); throw SQLException(mysql_stmt_errno(l_stmt),(char*)mysql_stmt_error(l_stmt)); } if (mysql_stmt_store_result(l_stmt)) { printf("\n [ %s::%d ]-->Exception mysql_stmt_store_result() failed\n",__FILE__,__LINE__); printf("%d: %s \n",mysql_stmt_errno(l_stmt),mysql_stmt_error(l_stmt)); throw SQLException(mysql_stmt_errno(l_stmt),(char*)mysql_stmt_error(l_stmt)); } if (!mysql_stmt_fetch(l_stmt)) { if (l_isNull) { printf("\nNULL value of Count of subscription_id fetched\n"); } else { // INFO logs ******************** printf("\n value of Count of subscription_id is %d \n " ,counter); } l_countRow++; } } if (mysql_stmt_free_result(l_stmt)) { printf("\n [ %s::%d ]-->Exception mysql_stmt_free_result() failed\n",__FILE__,__LINE__); printf("%d: %s \n",mysql_stmt_errno(l_stmt),mysql_stmt_error(l_stmt)); throw SQLException(mysql_stmt_errno(l_stmt),(char*)mysql_stmt_error(l_stmt)); } if (mysql_stmt_close(l_stmt)) { printf("\n [ %s::%d ]-->Exception mysql_stmt_close() failed\n",__FILE__,__LINE__); printf("%d: %s \n",mysql_stmt_errno(l_stmt),mysql_stmt_error(l_stmt)); throw SQLException(mysql_stmt_errno(l_stmt),(char*)mysql_stmt_error(l_stmt)); } l_stmt = NULL; if (l_countRow == 0) { printf("\n [ %s::%d ]-->Exception No data fetched\n",__FILE__,__LINE__); // Note: 10000 is User defined l_error Number throw SQLException(10000,": No data fetched"); counter =-1; } } catch (SQLException &ex) { if (l_stmt != NULL) { if (mysql_stmt_close(l_stmt)) { printf("\n [ %s::%d ]-->Exception mysql_stmt_close() failed\n",__FILE__,__LINE__); printf("%d: %s \n",mysql_stmt_errno(l_stmt),mysql_stmt_error(l_stmt)); } } printf("\n [ %s::%d ]-->SQL Exception ------- %d: %s\n",__FILE__,__LINE__,ex.errNumber,ex.errString); throw ex; } catch (exception & e) { if (l_stmt != NULL) { if (mysql_stmt_close(l_stmt)) { printf("\n [ %s::%d ]-->Exception mysql_stmt_close() failed\n",__FILE__,__LINE__); printf("%d: %s \n",mysql_stmt_errno(l_stmt),mysql_stmt_error(l_stmt)); } } printf("\n [ %s::%d ]-->Standard Exception ---------- %s\n",__FILE__,__LINE__,e.what()); throw e; } }