Thursday 27 July 2017

OCCI(Oracle C++ Call Interface) Example to Query the Data from a Table

The following Example demonestrate how to Query a Table Using OCCI(Oracle Caller C++ Interface). 
I have created an Employees Class with one Method queryEmployees to Query the data. Details are givene below. 

  • Class Declaration Section
  • I have defined a file named Employees.h which contains the Employees Class declaration 



    1. #ifndef _EMPLOYEES_H  
    2. #define _EMPLOYEES_H  
    3. #include <occi.h>  
    4. #include <iostream>  
    5. #include <iomanip>  
    6. using namespace oracle::occi;  
    7. using namespace std;  
    8.   
    9. class Employees {  
    10.     public:  
    11.         Employees();  
    12.         virtual ~Employees();  
    13.         void queryEmployees();  
    14.     private:  
    15.         Environment *env;  
    16.         Connection *con;  
    17.         ResultSet *rs;  
    18.         string user;  
    19.         string passwd;  
    20.         string db;  
    21. };  
    22. #endif



  • Class Definition Sectionbelow. 

    1. #include "Employees.h"  
    2. #include <occi.h>  
    3. #include <stdlib.h>  
    4. using namespace std;  
    5. using namespace oracle::occi;  
    6.   
    7.   
    8. Employees::Employees() {  
    9.         user = "hr";  
    10.         passwd = "**";  
    11.         db = "192.168.*.*:1521/noncdb";  
    12.   
    13.         env = Environment::createEnvironment(Environment::DEFAULT);  
    14.   
    15.         try  
    16.         {  
    17.                 con = env->createConnection(user, passwd, db);  
    18.         }  
    19.         catch (SQLException& ex)  
    20.         {  
    21.                 cout << ex.getMessage();  
    22.                 exit(EXIT_FAILURE);  
    23.         }  
    24. }  
    25.   
    26. Employees::~Employees()         {  
    27.          env->terminateConnection(con);  
    28.          Environment::terminateEnvironment (env);  
    29. }  
    30. void Employees::queryEmployees()  
    31. {  
    32.   
    33.         Statement *stmt = NULL;  
    34.         ResultSet *rs = NULL;  
    35.         string sql = "SELECT employee_id, first_name, last_name FROM employees WHERE rownum<=50 ORDER BY employee_id";  
    36.   
    37.         try  
    38.         {  
    39.                 stmt = con->createStatement(sql);  
    40.         }  
    41.         catch (SQLException& ex)  
    42.         {  
    43.                 cout << ex.getMessage();  
    44.         }  
    45.         if (stmt)  
    46.         {  
    47.                 try  
    48.                 {  
    49.                         stmt->setPrefetchRowCount(32);  
    50.                         rs = stmt->executeQuery();  
    51.                 }  
    52.                 catch (SQLException& ex)  
    53.                 {  
    54.                         cout << ex.getMessage();  
    55.                 }  
    56.                 if (rs)  
    57.                 {  
    58.                         cout << endl << setw(8) << left << "ID"  
    59.                         << setw(22) << left << "FIRST NAME"  
    60.                         << setw(27) << left << "LAST NAME"  
    61.                         << endl;  
    62.                         cout << setw(8) << left << "******"  
    63.                         << setw(22) << left << "********************"  
    64.                         << setw(27) << left << "*************************"  
    65.                         << endl;  
    66.                         while (rs->next()) {  
    67.                                 cout << setw(8) << left << rs->getString(1)  
    68.                                 << setw(22) << left << (rs->isNull(2) ? "n/a" : rs->getString(2))  
    69.                                 << setw(27) << left << rs->getString(3)  
    70.                                 << endl;  
    71.                         }  
    72.                         cout << endl;  
    73.                         stmt->closeResultSet(rs);  
    74.                 }  
    75.                 con->terminateStatement(stmt);  
    76.         }  
    77. }  



  • Define the Main Program that calls the queryEmployees Method.

    1. #include "Employees.h"  
    2. #include <occi.h> 
    3. int main (void) {  
    4.           Employees *pEmployees = new Employees();  
    5.           pEmployees->queryEmployees();  
    6.           delete pEmployees;  
    7.           return 0;  
    8.   }  

    To compile the above program either makefile utility can be used or enter the following command. 


    1. g++ -c Employees.cpp -I/opt/ora12c/product/12.1.0/dbhome_1/rdbms/public/ -L/opt/ora12c/product/12.1.0/dbhome_1/lib/

    2. g++ -c EmployeesMain.cpp -I/opt/ora12c/product/12.1.0/dbhome_1/rdbms/public/ -L/opt/ora12c/product/12.1.0/dbhome_1/lib/

    3. g++  EmployeesMain.o Employees.o -o Employees  -I/opt/ora12c/product/12.1.0/dbhome_1/rdbms/public/ -L/opt/ora12c/product/12.1.0/dbhome_1/lib/ -lclntsh -locci

    It will generate an executable file named Employees.


    1. execute the program by typing the below command.  
    2. ./Employees  
    3.   
    4. -------------  
    5. Output  
    6. -------------  
    7. ID      FIRST NAME            LAST NAME  
    8. ******  ********************  *************************  
    9. 100     Steven                King  
    10. 101     Neena                 Kochhar  
    11. 102     Lex                   De Haan  
    12. 103     Alexander             Hunold  
    13. 104     Bruce                 Ernst  
    14. 105     David                 Austin  
    15. 106     Valli                 Pataballa  
    16. 107     Diana                 Lorentz  
    17. 108     Nancy                 Greenberg  
    18. 109     Daniel                Faviet  
    19. 110     John                  Chen  
    20. 111     Ismael                Sciarra  
    21. 112     Jose Manuel           Urman  
    22. 113     Luis                  Popp  
    23. 114     Den                   Raphaely  
    24. 115     Alexander             Khoo  
    25. 116     Shelli                Baida  
    26. 117     Sigal                 Tobias  
    27. 118     Guy                   Himuro  
    28. 119     Karen                 Colmenares  
    29. 120     Matthew               Weiss  
    30. 121     Adam                  Fripp  
    31. 122     Payam                 Kaufling  
    32. 123     Shanta                Vollman  
    33. 124     Kevin                 Mourgos  
    34. 125     Julia                 Nayer  
    35. 126     Irene                 Mikkilineni  
    36. 127     James                 Landry  
    37. 128     Steven                Markle  
    38. 129     Laura                 Bissot  
    39. 130     Mozhe                 Atkinson  
    40. 131     James                 Marlow  
    41. 132     TJ                    Olson  
    42. 133     Jason                 Mallin  
    43. 134     Michael               Rogers  
    44. 135     Ki                    Gee  
    45. 136     Hazel                 Philtanker  
    46. 137     Renske                Ladwig  
    47. 138     Stephen               Stiles  
    48. 139     John                  Seo  
    49. 140     Joshua                Patel  
    50. 141     Trenna                Rajs  
    51. 142     Curtis                Davies  
    52. 143     Randall               Matos  
    53. 144     Peter                 Vargas  
    54. 145     John                  Russell  
    55. 146     Karen                 Partners  
    56. 147     Alberto               Errazuriz  
    57. 148     Gerald                Cambrault  
    58. 149     Eleni                 Zlotkey