OCIPlus

         Introduction
         Download
         Compiling
         Using the library
               Connecting to the database
               Using the statement to insert/update record
               Using the statement to select record
               Using the resultset to browse the records
               Using the statement to execute stored procedures
         Questions/Comments

Introduction

OCIPlus is a simple C++ wrapper library for OCI API that is used to access oracle database. The main advantage of this library is the simplicity in understanding and coding, as it very similar to JDBC.

These simple classes could be compiled using any standard C++ compiler and linked on any platform. It has support for oracle cursors too, so you can call stored procedures and functions. The limitation is currently is does not support transaction, and CLOB or BLOB data type. 


Download

You can download OCIPlus source code files from below links:

Compiling

Use make command to compile the library and create a shared library that can be used by many applications. The makefile included in the release does this for you. It creates shared library which can be distributed along with the header file or used directly in the application.  

To compile using gnu gcc compiler use Makefile.gcc as shown below:

   
    make -f Makefile.gcc

To compile using sun compiler use Makefile.sun as shown below:

   
    make -f Makefile.sun

The output should be a shared library named libocintf.so and demo programs demo1, demo2 and demo3. These demo programs use libocintf.so for OCI functionality.

Using the library
class OCIConnection 
This class provides interface to connect to oracle server using the OCI (oracle callable interface) API.

class OCIStatement
This class provides interface to an sql statement to execute sql queries on oracle server.

class OCIResultSet
This class provides interface to an sql resultset, which can be used to fetch data by executing sql queries on oracle server.

Connecting to the database
Connecting to the database is done using the OCIConnection class. Use method login and pass parameters username, password and oracle TNS name. Once connection is made, multiple statements could be opened in the context of this connection.

  
    OCIConnection conn;
    conn.login("SCOTT", "TIGER", "ORCL");



This method returns success code OCI_SUCCESS (defined in oci.h) on successful login.

Using the statement to insert/update record
After successful login, create a statement pointer object using the OCIConnection object. As this sql statement will not return any resultset, use executeUpdate to execute this statement and return the number of rows effected.

   
    char *sql="insert into emp(eno,ename) values(1,'Joe')";
    OCIStatement *stmt =  conn.createStatement(sql);
    int nrows = stmt->executeUpdate();
    cout << "Updated " << nrows << " records." << endl;


Using the statement to select record

After successful login, create a statement pointer object using the OCIConnection object. As this sql statement will return resultset, use executeSelect to execute this statement and return a resultset.

   
    char *sql="select emp.eno, emp.ename, emp.datebirth from emp";
    OCIStatement *stmt =  conn.createStatement(sql);
    OCIResultSet *rs = stmt->executeSelect();



Using the resultset to browse the records
ResultSet object is not updatable and has a cursor that moves forward only.Thus, it is possible to iterate through it only once and only from the first row to the last row.

   
    while(rs->fetchNext()) {
      cout << rs.getInt(1) << " ";
      cout << rs.getString(2) << " ";
      OCIDateAndTime dt =  rs->getDateTime(3);
      cout << dt.m_year << "/" << dt.m_month << "/" << dt.m_day << endl;
    }




Using the statement to execute stored procedures
OCIStatement object can be used to execute stored procedures too. After creating the statement, bind variables could be binded using the bind method. For returnable cursors, OCIResultSet object could be binded using the bindResultSet method. Finally, the PL/SQL block is executed using executePLSQL method of the statement object.

    
    char *sql="BEGIN SCOTT.GET_EMP_SP(1,:salary,:mycursor); END;";
    OCIStatement *stmt =  conn.createStatement(sql);

    stmt->bind((char*)":salary",salary);    
    OCIResultSet *rs = stmt->bindResultSet((char*)":mycursor");

    stmt->executePLSQL();


    cout << "Salary: " << endl;

    while(rs->fetchNext()) {
      cout << rs.getInt(1) << " " rs.getString(2);
      cout << " " << rs.getFloat(3) << endl;
    }

         


Freeing up resources
OCIConnection, OCIStatement, and OCIResultSet objects should be closed and deleted after use. 


        rs->close();
        delete rs3;

        stmt->close();
        delete stmt;
        
        conn.logoff();  


Questions/Comments

Email me at sansari13@yahoo.com