Connect C++ with Oracle

Discussion in 'C++' started by Shishir191, Jul 24, 2007.

  1. Shishir191

    Shishir191 New Member

    Joined:
    Jul 24, 2007
    Messages:
    27
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    Delhi
    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:
    create table EMP(
    	empno     NUMBER,
    	ename     VARCHAR2(10),
    	hireDate  Date);

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



    Code:
    #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;
    }
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Nicely coded and commented.
     
  3. seeguna

    seeguna New Member

    Joined:
    Jun 20, 2007
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Technical Consultant
    Location:
    Chennai
  4. Shishir191

    Shishir191 New Member

    Joined:
    Jul 24, 2007
    Messages:
    27
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    Delhi
  5. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Really cool! :)
     
  6. Shishir191

    Shishir191 New Member

    Joined:
    Jul 24, 2007
    Messages:
    27
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    Delhi
  7. kush_2207

    kush_2207 New Member

    Joined:
    Jun 26, 2007
    Messages:
    49
    Likes Received:
    1
    Trophy Points:
    0
  8. Shishir191

    Shishir191 New Member

    Joined:
    Jul 24, 2007
    Messages:
    27
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    Delhi
  9. kush_2207

    kush_2207 New Member

    Joined:
    Jun 26, 2007
    Messages:
    49
    Likes Received:
    1
    Trophy Points:
    0
    Getting compile-time errors like Not able to find dbmanager.h header file....
    Please Help !
    Thanx.
     
  10. Shishir191

    Shishir191 New Member

    Joined:
    Jul 24, 2007
    Messages:
    27
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    Delhi
    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.
     
  11. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Why don't you edit the code to have the correct version.
     
  12. Shishir191

    Shishir191 New Member

    Joined:
    Jul 24, 2007
    Messages:
    27
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    Delhi
    Ok.
     
  13. Shishir191

    Shishir191 New Member

    Joined:
    Jul 24, 2007
    Messages:
    27
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    Delhi
    Hi Shabbir,
    Can i edit the existing code ? Or i have to upload the new code. If it is possible to edit the code then pls tell from where i can do this.
     
  14. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    You don't have any attachment and so all you need to do is edit the post and update the code accordingly.
     
  15. kush_2207

    kush_2207 New Member

    Joined:
    Jun 26, 2007
    Messages:
    49
    Likes Received:
    1
    Trophy Points:
    0
    Hi Shishir,
    I have attached the VC++ 6.0 version connection with Oracle. It is overall the same code which you have listed out in this thread. I have also included oci.h from the Oracle Include directory. Still i am getting problems -> Please check out.
     
    Last edited by a moderator: Aug 15, 2007
  16. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    I have removed the attachment. Please do not attach the complete project / solution and ask for someone to debug it. You can always discuss the issue if you have some problem.
     
  17. Peter_APIIT

    Peter_APIIT New Member

    Joined:
    Apr 11, 2007
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Malaysia
    How about an article explain how to connect C++ to SQL ?

    I using MS Visual Studio 2005.

    I really noob to database as well as C++.

    Thanks for your help. Your help is greatly appreciated by me and others.
     
  18. Peter_APIIT

    Peter_APIIT New Member

    Joined:
    Apr 11, 2007
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Malaysia
    Any guide or tutorial where i can start to learn connect C++ with msssql ?

    Thanks for your help.
     
  19. vkiransrinivas

    vkiransrinivas New Member

    Joined:
    Jun 13, 2007
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for sending
     
  20. Shishir191

    Shishir191 New Member

    Joined:
    Jul 24, 2007
    Messages:
    27
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    Delhi
    Thanks for your good comments.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice