Page 1 of 1

How to connect C++ with Oracle. Rate Topic: ***** 2 Votes

#1 Shishir191  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 24-July 07

Posted 24 July 2007 - 05:05 AM

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.

The table that is used in the example code is:

create table EMP

(empno NUMBER,

ename VARCHAR2(10),

hireDate Date);

Database Query

This program expain how to select the contents of the EMP table.



#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);
  }

  string userName = argv[1];

  // Initialize OracleServices

  DbManager* dbm = NULL;

  OracleServices* oras = NULL;

  Statement *stmt = NULL;

  ResultSet *resultSet = NULL;

  try

  {

	// Obtain OracleServices object with the default args.

	// The default args creates OracleServices with an environment of

	// Environment::OBJECT|Environment::THREADED_MUTEXED

	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

	}

  }

  
  return 0;

}





Is This A Good Question/Topic? 1
  • +

Replies To: How to connect C++ with Oracle.

#2 rbird  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-September 08

Posted 25 September 2008 - 12:50 PM

View PostShishir191, on 24 Jul, 2007 - 05:05 AM, said:

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.

The table that is used in the example code is:

create table EMP

(empno NUMBER,

ename VARCHAR2(10),

hireDate Date);

Database Query

This program expain how to select the contents of the EMP table.



#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);
  }

  string userName = argv[1];

  // Initialize OracleServices

  DbManager* dbm = NULL;

  OracleServices* oras = NULL;

  Statement *stmt = NULL;

  ResultSet *resultSet = NULL;

  try

  {

	// Obtain OracleServices object with the default args.

	// The default args creates OracleServices with an environment of

	// Environment::OBJECT|Environment::THREADED_MUTEXED

	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

	}

  }

  
  return 0;

}



this was easy to follow. however, I can not get it to compile. it can not find #include <DbManager.h>. what type of project should I create this as? Also, will it work in the express edition? thank you
Bob
Was This Post Helpful? 0
  • +
  • -

#3 ianweg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-November 09

Posted 07 June 2010 - 12:51 AM

Where do you get dbmanager.h from? It is not explained in the tutorial.
Was This Post Helpful? 0
  • +
  • -

#4 Btu  Icon User is offline

  • D.I.C Regular

Reputation: 36
  • View blog
  • Posts: 250
  • Joined: 16-May 11

Posted 08 July 2011 - 11:19 AM

View Postianweg, on 07 June 2010 - 12:51 AM, said:

Where do you get dbmanager.h from? It is not explained in the tutorial.

it appears this was copied directly from another forum from the same person
this tutorial

This post has been edited by Btu: 08 July 2011 - 11:22 AM

Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2871
  • View blog
  • Posts: 11,026
  • Joined: 15-July 08

Posted 08 July 2011 - 11:37 AM

Same person on the same date. Seems ok to me...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1