follows a very basic tip of how to use the Occci library that comes with Oracle to create C ++ programs accessing your database.
As you can see, use the Occci.h is very simple and reminds you very much the use Java.sql and the JDBC drives ..
to compile the example in Linux, use the following command. .
c++ -L /oracle/9.2.0/lib/ -L /oracle/9.2.0/rdbms/lib/ -I /oracle/9.2.0/rdbms/public/ -I /oracle/9.2.0/rdbms/demo/ -lclntsh `cat /oracle/9.2.0/lib/sysliblist` -ldl -lm -locci -o OraUsers OraUsers.cpp
./OraUsers
I will soon post another tip of how to create internal functions in the bank using C / C ++.
I hope you like the tip ..: D
#include <string>
#include <iostream.h>
#include <occi.h> //Include das bibliotecas do Oracle
using namespace oracle::occi; //Utiliza o namespace das libs do Oracle occi.
using namespace std;
int main(int argc, char *argv[]) {
Environment* env = Environment::createEnvironment(); //Cria o ambiente de acesso ao Oracle
Connection* conn = env->createConnection( "system", "senha" ); //Conecta ao Oracle utilizando usuario, senha
cout << "\n------------------------------------------------------------------------------\n" << endl;
cout << "\t\t\tUsuários Conectados\n" << endl;
Statement* stmt = conn->createStatement(); //Cria um objeto de declaração e o associa à conecção do Oracle
stmt->setSQL("select sid,serial#,username,osuser,server,status from v$session"); // Cria o sql dentro do objeto de declaração
ResultSet *rs = stmt->executeQuery(); // Cria um objeto ResultSet e executa a query do objeto de declaração
while (rs->next()) //Enquanto houver registro...
{
int sid = rs->getInt(1); //Pega a primeira coluna do resultado e associa a váriavel.
string serial = rs->getString(2); //Pega a segunda coluna do resultado ...
string username = rs->getString(3);
string osusuario = rs->getString(4);
string server = rs->getString(5);
string status = rs->getString(6);
if (argc>1 && strcpy(argv[1],"-noora")) { // Imprime resultado.
if(osusuario!="oracle") cout << "SID: "<< sid << "|" << "Usuário:" << username << "|" << "Usuário SO: "<< osusuario <<"|"<<"Servidor: "<< server << "|"<< "Status: "<< status << endl;
} else {
cout << "SID: "<< sid << "|"<< "Usuário:" << username << "|" << "Usuário SO: "<< osusuario <<"|"<<"Servidor: "<< server << "|"<< "Status: "<< status << endl;
}
}
conn->terminateStatement(stmt); //Fecha o objeto de declaração.
env->terminateConnection(conn); // Fecha a conexão ao banco.
Environment::terminateEnvironment(env); //Fecha o objeto de ambiente.
cout << "\n------------------------------------------------------------------------------\n" << endl;
}