Thursday, July 23, 2020

How to use occi to connect C++ with oracle 11gR2.

 ( I assume you have already installed oracle 11g R2 server in on VM and in second VM oracle instant client 11gr2 and sever's  sid is "orcl" and in client you are using codeblocks  17.12 for C++ editor. You must be able to issue sqlplus command in instant client side successfully )

    #include <iostream>
    #include <occi.h>
    #include <iomanip>
    using namespace oracle::occi;
    using namespace std;
    int main()
    {
        const string user="rahul", passwd = "rahul", connstring = "orcl";
        Environment *env =  Environment::createEnvironment();
        Connection *conn = env->createConnection(user, passwd, connstring);
        string strstmt = "select *from emp";
        Statement *stmt = conn->createStatement(strstmt);

        ResultSet *rs = stmt->executeQuery();

while(rs->next())
    {
        cout << setw(4) << rs->getInt(1) << "\t" << setw(6) << rs->getString(2) << "\t"
         << setw(9) << rs->getString(3) << "\t" << setw(4) << rs->getInt(4) << "\t"
         << setw(9) << rs->getString(5) << "\t" << setw(4) << rs->getInt(6) << "\t"
         << setw(4) << rs->getInt(7) << "\t" << setw(5) << rs->getInt(8)<< endl;

    }
        stmt->closeResultSet(rs);
        conn->terminateStatement(stmt);
        env->terminateConnection(conn);
        Environment::terminateEnvironment(env);
        return 0;
    }


now to compile do following :-
-------------------------------------
include clntsh, occi, nnz11 in  :- projects->build options->linkersettings->link libraries->clntsh , occi, nnz11


projects->build options->linker settings->search directories ->compiler-> {path to include dir for oracle} as I given -> /opt/oracle/instantclient_11_2/sdk/include

then :-
then in linker -> /opt/oracle/instantclient_11_2  this is  for library



now build your project  and output should be :-


No comments:

Post a Comment