/***************************************************************************************** File Example1.cpp Description This file contains a simple example of a C++ program accessing a database using the ODBC functions. Public Methods Private Methods *****************************************************************************************/ // Header Files #include #include //Main include for ODBC core functions #include // Used for applications using Microsoft SQL Extensions #include /***************************************************************************************** Class Definition OBBC_Class.cpp Description This class contains the handles necessary to create an ODBC connection to a database. *****************************************************************************************/ // Define The ODBC_Class Class class ODBC_Class { // Attributes public: SQLHANDLE EnvirHandle; SQLHANDLE ConnHandle; SQLHANDLE StmtHandle; // for use with second SQL statement SQLHANDLE StmtHandle2; // for use with second SQL statement SQLRETURN return_code; // Methods public: ODBC_Class(); // This is the Constructor ~ODBC_Class(); // This is the Destructor SQLRETURN ShowColNames(); SQLRETURN StaticDisplayUniversity(); SQLRETURN StaticDisplayDepartment(); SQLRETURN DynamicDisplayResults(); SQLRETURN DynamicDisplayResults2(); void DriverInfo(); }; /***************************************************************************************** Function ODBC_Class::ODBC_Class Description This is the ODBC_Class constructor. *****************************************************************************************/ ODBC_Class::ODBC_Class() { // Initialize return code variable return_code = SQL_SUCCESS; // Allocate environment handle to store information about the environment return_code = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &EnvirHandle); // Set the ODBC version to 3.x if (return_code == SQL_SUCCESS) return_code = SQLSetEnvAttr(EnvirHandle, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, SQL_IS_UINTEGER); // Allocate connection handle to store information about the connection if (return_code == SQL_SUCCESS) return_code = SQLAllocHandle(SQL_HANDLE_DBC, EnvirHandle, &ConnHandle); } /***************************************************************************************** Function ODBC_Class::~ODBC_Class Description This is the ODBC_Class destructor. *****************************************************************************************/ ODBC_Class::~ODBC_Class() { // Free connection handle if (ConnHandle != NULL) SQLFreeHandle(SQL_HANDLE_DBC, ConnHandle); // Free environment handle if (EnvirHandle != NULL) SQLFreeHandle(SQL_HANDLE_ENV, EnvirHandle); } /***************************************************************************************** Function ODBC_Class::DriverInfo Description This will display the names of the columns in the result set. . *****************************************************************************************/ void ODBC_Class::DriverInfo(void) { SQLCHAR InfoBuffer[255]; SQLSMALLINT InfoSize; SQLUINTEGER Conformance; cout << "Driver Information \n" ; return_code = SQLGetInfo (ConnHandle, SQL_DRIVER_NAME, (SQLPOINTER) &InfoBuffer, sizeof(InfoBuffer), &InfoSize); cout << "SQL_DRIVER_NAME= " << InfoBuffer <FieldValue, sizeof(nextone->FieldValue[0]), NULL); nextone-> next = NULL; last -> next = nextone; last = nextone; }; // end for // Display A Header cout << "Cursor Controlled Display :" << endl << endl; // While There Are Records In The Result Data Set Generated, // Retrieve And Display Them cout << "Run through data from first to last:" << endl << endl; while (fetch_return_code != SQL_NO_DATA_FOUND) { fetch_return_code = SQLFetchScroll(StmtHandle,SQL_FETCH_NEXT,1); // fetch_return_code = SQLExtendedFetch(StmtHandle,SQL_FETCH_NEXT,1, // &NumberRowsFetched,&RowStatus[0]); cout << "Number of rows fetched = " << NumberRowsFetched << endl; for (int i = 0; i < (int)NumberRowsFetched; i++) { // if (RowStatus[i] == SQL_ROW_SUCCESS || // RowStatus[i] == SQL_ROW_SUCCESS_WITH_INFO) // { nextone = first; for (int j = 0; j < (int) NumCols; j++) { cout << nextone -> FieldValue[i] ; nextone = nextone -> next; }//end for (int j cout << endl; } // end for (int i // } // end if (RowStatus } //end while (fetch_return // Return The ODBC API Return Code To The Calling Function return(return_code); } //ODBC_Class::DynamicDisplayResults /***************************************************************************************** Function main Description This is the main function of the example1 program. *****************************************************************************************/ int main() { // Declare The Local Memory Variables SQLRETURN return_code = SQL_SUCCESS; SQLCHAR DBName[10] = "oracle"; SQLCHAR DBUser[10] = "thinke"; SQLCHAR DBPswd[10] = "illustra"; SQLCHAR SQLStmt[255]; // Holds SQL statement SQLCHAR SQLStmt2[255]; // Holds another SQL statement SQLCHAR Relation[20] = "university"; SQLCHAR City[20] = "Los Angeles"; SQLINTEGER Terminator; SQLINTEGER NativeError; // used for SQLGetDiagRec SQLCHAR SQLState[6]; // used for SQLGetDiagRec SQLCHAR ErrorMsg[255]; // used for SQLGetDiagRec SQLSMALLINT ErrorMsgLength; // used for SQLGetDiagRec Terminator = SQL_NTS; // Create instance of ODBC_Class ODBC_Class Example; // Connect to oracle database if (Example.ConnHandle != NULL) { return_code = SQLConnect(Example.ConnHandle, DBName, SQL_NTS, DBUser, SQL_NTS, DBPswd, SQL_NTS); cout << "Connect return " << return_code << endl; // Print out information about the driver Example.DriverInfo(); // Allocate An SQL Statement Handle return_code = SQLAllocHandle(SQL_HANDLE_STMT, Example.ConnHandle, &Example.StmtHandle); if (return_code == SQL_SUCCESS) { //Static SQL query cout << "Build and execute a static SQL query \n"; // Define a static SQL select query strcpy((char *) SQLStmt, "SELECT * FROM "); strcat((char *) SQLStmt, "university "); // Submit query to database for execution. return_code = SQLExecDirect(Example.StmtHandle, SQLStmt, SQL_NTS); // Display Information About The Columns In The Result // Data Set Produced By The SQL Query if (return_code == SQL_SUCCESS) Example.ShowColNames(); // Display the university and city if (return_code == SQL_SUCCESS) Example.StaticDisplayUniversity(); //Build SQL query at run time cout << "Build an SQL query at run time \n"; //Define an SQL select query using a relation name stored in variable strcpy((char *) SQLStmt, "SELECT * FROM "); strcat((char *) SQLStmt, (char *)Relation); // Note, do not put a semicolon at the end of the query //Display the query that was built cout << "This is the query that was built " << SQLStmt; return_code = SQLExecDirect(Example.StmtHandle, SQLStmt, SQL_NTS); //Display Information About The Columns In The Result // Data Set Produced By The SQL Query if (return_code == SQL_SUCCESS) Example.ShowColNames(); //Display the university and city if (return_code == SQL_SUCCESS) Example.StaticDisplayUniversity(); //Build a parameterized SQL Query7 cout << "Build a parameterized SQL query at run time\n"; //Define a parameterized SQL query. strcpy((char *) SQLStmt, "SELECT * FROM University where City = ?"); //Prepare SQL statement -- must occur prior to binding return_code = SQLPrepare(Example.StmtHandle, SQLStmt, SQL_NTS); //Bind parameter to query return_code = SQLBindParameter(Example.StmtHandle, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 20, 0, City, 20, &Terminator); // Submit query to database for execution. // Note since query was prepared, you use SQLExecute // not SQLExecDirect return_code = SQLExecute(Example.StmtHandle); // Display the university, city, state, size if (return_code == SQL_SUCCESS) Example.StaticDisplayUniversity(); // Dynamic display cout <<"Dynamic Display \n"; if (return_code == SQL_SUCCESS) Example.DynamicDisplayResults(); // Define a static SQL select query strcpy((char *) SQLStmt, "SELECT * FROM "); strcat((char *) SQLStmt, "university "); // Enable ODBC cursor library - not supported in current driver // return_code = SQLSetConnectAttr(Example.ConnHandle, // SQL_ODBC_CURSORS, (SQLPOINTER) SQL_CUR_USE_ODBC,NULL); //Set scroll options to scrollable cursor -- not supported in current driver // return_code = SQLSetStmtAttr(Example.StmtHandle, SQL_ATTR_CURSOR_TYPE, // (SQLPOINTER) SQL_CURSOR_STATIC, NULL); // Submit query to database for execution. return_code = SQLExecDirect(Example.StmtHandle, SQLStmt, SQL_NTS); // Following provides diagnotic for function SQLGetDiagRec(SQL_HANDLE_STMT, Example.StmtHandle, 1, SQLState, &NativeError, ErrorMsg, 255, &ErrorMsgLength); cout << "mainSQLState " << SQLState << endl; cout << ErrorMsg << endl; // Display the university and city if (return_code == SQL_SUCCESS) Example.DynamicDisplayResults2(); // Allocate a second SQL Statement Handle return_code = SQLAllocHandle(SQL_HANDLE_STMT, Example.ConnHandle, &Example.StmtHandle2); if (return_code == SQL_SUCCESS) { cout << "Build and execute a second SQL query \n"; // Define a query for a different relation strcpy((char *) SQLStmt2, "SELECT * FROM department"); // Use different handle to submit query new relation. return_code = SQLExecDirect(Example.StmtHandle2, SQLStmt2, SQL_NTS); //Display the results Example.StaticDisplayDepartment(); // Free second statement handle if (Example.StmtHandle2 != NULL) SQLFreeHandle(SQL_HANDLE_STMT, Example.StmtHandle2); } // end second if (return_code == SQL_SUCCESS) // Free The SQL Statement Handle if (Example.StmtHandle != NULL) SQLFreeHandle(SQL_HANDLE_STMT, Example.StmtHandle); } //end if (return_code == SQL_SUCCESS) // Disconnect from oracle database return_code = SQLDisconnect(Example.ConnHandle); }// end if (Example.ConnHandle != NULL) // Return To The Operating System return(return_code); }