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
Class Definition Sectionbelow.
Define the Main Program that calls the queryEmployees Method.
To compile the above program either makefile utility can be used or enter the following command.
It will generate an executable file named Employees.
I have created an Employees Class with one Method queryEmployees to Query the data. Details are givene below.
I have defined a file named Employees.h which contains the Employees Class declaration
- #ifndef _EMPLOYEES_H
- #define _EMPLOYEES_H
- #include <occi.h>
- #include <iostream>
- #include <iomanip>
- using namespace oracle::occi;
- using namespace std;
- class Employees {
- public:
- Employees();
- virtual ~Employees();
- void queryEmployees();
- private:
- Environment *env;
- Connection *con;
- ResultSet *rs;
- string user;
- string passwd;
- string db;
- };
- #endif
- #include "Employees.h"
- #include <occi.h>
- #include <stdlib.h>
- using namespace std;
- using namespace oracle::occi;
- Employees::Employees() {
- user = "hr";
- passwd = "**";
- db = "192.168.*.*:1521/noncdb";
- env = Environment::createEnvironment(Environment::DEFAULT);
- try
- {
- con = env->createConnection(user, passwd, db);
- }
- catch (SQLException& ex)
- {
- cout << ex.getMessage();
- exit(EXIT_FAILURE);
- }
- }
- Employees::~Employees() {
- env->terminateConnection(con);
- Environment::terminateEnvironment (env);
- }
- void Employees::queryEmployees()
- {
- Statement *stmt = NULL;
- ResultSet *rs = NULL;
- string sql = "SELECT employee_id, first_name, last_name FROM employees WHERE rownum<=50 ORDER BY employee_id";
- try
- {
- stmt = con->createStatement(sql);
- }
- catch (SQLException& ex)
- {
- cout << ex.getMessage();
- }
- if (stmt)
- {
- try
- {
- stmt->setPrefetchRowCount(32);
- rs = stmt->executeQuery();
- }
- catch (SQLException& ex)
- {
- cout << ex.getMessage();
- }
- if (rs)
- {
- cout << endl << setw(8) << left << "ID"
- << setw(22) << left << "FIRST NAME"
- << setw(27) << left << "LAST NAME"
- << endl;
- cout << setw(8) << left << "******"
- << setw(22) << left << "********************"
- << setw(27) << left << "*************************"
- << endl;
- while (rs->next()) {
- cout << setw(8) << left << rs->getString(1)
- << setw(22) << left << (rs->isNull(2) ? "n/a" : rs->getString(2))
- << setw(27) << left << rs->getString(3)
- << endl;
- }
- cout << endl;
- stmt->closeResultSet(rs);
- }
- con->terminateStatement(stmt);
- }
- }
- #include "Employees.h"
- #include <occi.h>
- int main (void) {
- Employees *pEmployees = new Employees();
- pEmployees->queryEmployees();
- delete pEmployees;
- return 0;
- }
To compile the above program either makefile utility can be used or enter the following command.
- 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/
- 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/
- 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.
- execute the program by typing the below command.
- ./Employees
- -------------
- Output
- -------------
- ID FIRST NAME LAST NAME
- ****** ******************** *************************
- 100 Steven King
- 101 Neena Kochhar
- 102 Lex De Haan
- 103 Alexander Hunold
- 104 Bruce Ernst
- 105 David Austin
- 106 Valli Pataballa
- 107 Diana Lorentz
- 108 Nancy Greenberg
- 109 Daniel Faviet
- 110 John Chen
- 111 Ismael Sciarra
- 112 Jose Manuel Urman
- 113 Luis Popp
- 114 Den Raphaely
- 115 Alexander Khoo
- 116 Shelli Baida
- 117 Sigal Tobias
- 118 Guy Himuro
- 119 Karen Colmenares
- 120 Matthew Weiss
- 121 Adam Fripp
- 122 Payam Kaufling
- 123 Shanta Vollman
- 124 Kevin Mourgos
- 125 Julia Nayer
- 126 Irene Mikkilineni
- 127 James Landry
- 128 Steven Markle
- 129 Laura Bissot
- 130 Mozhe Atkinson
- 131 James Marlow
- 132 TJ Olson
- 133 Jason Mallin
- 134 Michael Rogers
- 135 Ki Gee
- 136 Hazel Philtanker
- 137 Renske Ladwig
- 138 Stephen Stiles
- 139 John Seo
- 140 Joshua Patel
- 141 Trenna Rajs
- 142 Curtis Davies
- 143 Randall Matos
- 144 Peter Vargas
- 145 John Russell
- 146 Karen Partners
- 147 Alberto Errazuriz
- 148 Gerald Cambrault
- 149 Eleni Zlotkey
No comments:
Post a Comment