Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/articles/cpp-tutorials/)
-   -   Connect C++ with Oracle (http://www.go4expert.com/articles/connect-cpp-oracle-t5442/)

Shishir191 24Jul2007 16:53

Connect C++ with Oracle
 
Oracle C++ Call Interface (OCCI) is an Application Programming Interface (API) that provides C++ applications access to data in an Oracle database. This API is a significant improvement to the Oracle Call Interface (OCI) API as far as ease of use is concerned. Engineers who have written JDBC (Java Database Connectivity) code will find the OCCI API to be quite similar to that of JDBC

1) The table that is used in the example code is:



Code: SQL

CREATE TABLE EMP(
    empno     NUMBER,
    ename     VARCHAR2(10),
    hireDate  Date);

2) Database Query : Select the contents of the EMP table



Code: Cpp

#include <DbManager.h>
#include <iostream>

using namespace std;

using namespace oracle::occi;

const string sqlString("select empno, ename, hiredate from emp");

const string dateFormat("DD-MON-YYYY HH24:MI:SS");

int main(int argc, char **argv)

{
    if (argc != 2)
    {
        cerr << "\nUsage: " << argv[0] << " <db-user-name>\n" << endl;
        exit(1);
    }
   
    // Initialize OracleServices
   
    DbManager* dbm = NULL;
   
    OracleServices* oras = NULL;
   
    Statement *stmt = NULL;
   
    ResultSet *resultSet = NULL;
   
    try
    {
       
        // Obtain OracleServices object with the default args.
       
        dbm = new DbManager(userName);
       
        oras = dbm->getOracleServices();
       
        // Obtain a cached connection
       
        Connection * conn = oras->connection();
       
        // Create a statement
       
        stmt = conn->createStatement(sqlString);
       
        int empno;
       
        string ename;
       
        Date hireDate;
       
        string dateAsString;
       
        // Execute query to get a resultset
       
        resultSet = stmt->executeQuery();
       
        while (resultSet->next())
        {
           
            empno = resultSet->getInt(1)// get the first column returned by the query;
           
            ename = resultSet->getString(2)// get the second column returned by the query
           
            hireDate = resultSet->getDate(3)// get the third column returned by the query
           
            dateAsString="";
           
            //You cannot check for null until the data has been read
           
            if (resultSet->isNull(1))
            {
                cout << "Employee num is null... " << endl;
            }
            if (resultSet->isNull(2))
            {
                cout << "Employee name is null..." << endl;
            }
            if (resultSet->isNull(3))
            {
                cout << "Hire date is null..." << endl;
            }
            else
            {
                dateAsString=hireDate.toText(dateFormat);
            }
            cout << empno << "\t" << ename << "\t" << dateAsString << endl;
           
        }
       
        // Close ResultSet and Statement
       
        stmt->closeResultSet(resultSet);
       
        conn->terminateStatement(stmt);
       
        // Close Connection and OCCI Environment
       
        delete dbm;
       
    }
    catch (SQLException& ex)
    {
        if (dbm != NULL)
        {
            dbm->rollbackActions(ex, stmt, resultSet); // free resources and rollback transaction
        }
    }
    catch (ExoException& ex1)
    {
        cerr << "\nCaught ExoException:\n" << ex1.getExceptionText() << endl;
        exit(2);
    }
   
    return 0;
}


shabbir 24Jul2007 18:23

Re: Connect C++ with Oracle
 
Nicely coded and commented.

seeguna 26Jul2007 08:01

Re: Connect C++ with Oracle
 
Good one

Shishir191 26Jul2007 13:17

Re: Connect C++ with Oracle
 
Thanks

pradeep 27Jul2007 14:21

Re: Connect C++ with Oracle
 
Really cool! :-)

Shishir191 29Jul2007 21:02

Re: Connect C++ with Oracle
 
Thanks.

kush_2207 30Jul2007 19:07

Re: Connect C++ with Oracle
 
gr8 post !

Shishir191 30Jul2007 19:45

Re: Connect C++ with Oracle
 
Thanks.

kush_2207 3Aug2007 03:03

Re: Connect C++ with Oracle
 
Getting compile-time errors like Not able to find dbmanager.h header file....
Please Help !
Thanx.

Shishir191 13Aug2007 23:12

Re: Connect C++ with Oracle
 
Quote:

Originally Posted by kush_2207
Getting compile-time errors like Not able to find dbmanager.h header file....
Please Help !
Thanx.

Hi Kush,

Sorry for the delay. Sorry, there is no need to include "dbmanager.h" by mistake i have included it.

You have to include "occi.h" like #include<occi.h> in your program.

Note one thing more this code will not work for every compiler. Either you have to use visual C++7.
Or you can use gcc comipler version 3.4.3. If you are using gcc comipler in linux then you have to download occi libraries from Oracle web site.

And you have to copy these files into your $ORACLE_HOME/lib.
If you have installed oracle client in your machine the it contains one "rdms" folder which contain one "demo" folder . From that folder you can use "demo_xe.mk" to compile your file.

If you have any further query then you can ask me.


All times are GMT +5.5. The time now is 23:11.