C ++ / Oracle / Linux

Coloque aqui tutoriais (por enquanto, sobre qualquer assunto relacionado a Oracle) e apostilas.
Post Reply
kim_kaphwan
Rank: Estagiário Pleno
Rank: Estagiário Pleno
Posts: 6
Joined: Fri, 04 Jun 2004 8:54 am
Location: sp

Ola,
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. .

Select all

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
... Changing / / 9.2.0/ by your Oracle_Home. After compiled is just run:

Select all

./OraUsers  
with the --Noor, it suppresses the Oracle users of the system.

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

Select all

 
#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; 
} 
bortho
Rank: Estagiário Júnior
Rank: Estagiário Júnior
Posts: 2
Joined: Tue, 23 Oct 2007 10:02 am
Location: SP

Hello,

Resurrected the post, I am having my first contact with Linux programming and I can not compile the code as presented above. Could anyone give me a hand?

Thanks
bortho
Rank: Estagiário Júnior
Rank: Estagiário Júnior
Posts: 2
Joined: Tue, 23 Oct 2007 10:02 am
Location: SP

Problem solved, the makefile was with wrong parameters
Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 10 guests