Oracle C++ OCI Database Example

7 篇文章 0 订阅

转自:http://www.tidytutorials.com/2009/08/oracle-c-occi-database-example.html


Aim
The aim of this C++ tutorial is to create a simple client that uses Oracle C++ OCI (OCCI Oracle C++ Call Interface) to connect to an Oracle database. We will be using the windows command line C++ compiler cl.exe that comes with Visual Studio C++ for this example. This demonstrates connectiong to the Oracle database and manipulating data. Leave any questions or problems as comments and I will endeavour to answer them.

If you would like to see a database pool implemented for Oracle OCCI the please visit Oracle C++ OCI Database Connection Pool.

Assumptions
This article assumes that you have an Oracle database installed and running and you have downloaded and installed/unzipped the Oracle Client which contains the header files and libs required. For purposes of this example the the project directory is OracleOCCIExample. Also you have VC++ installed and configured. See Problems and Solutions Installing VC++ Express Edition, to install and run vcvars32.bat.

Versions used in this example

Sofware/ComponentImage
Windows XP SP2N/A
Oracle Client10201_client_win32.zip
Visual Studio Express Editions 2008 VC++N/A
Links to these files can be found here

Oracle client is installed (expanded) in the "D:\oracle" directory for this example. Please remember to substitute your own directories for these.

NOTE: OCCI uses tnsnames.ora to connect to the database. So make sure you have this file configured with the database details you wish to connect to.


Write and compile the Client

  1. Write the client and save it as oracleexample.cpp in your working directory. 
    #include <iostream>
    #include <occi.h>
    
    using namespace std;
    
    int main(){
    
        oracle::occi::Environment* environment;
        oracle::occi::Connection *con;
        oracle::occi::Statement* stmt;
        oracle::occi::ResultSet* res;
    
        try{
        
            environment = oracle::occi::Environment::createEnvironment(oracle::occi::Environment::DEFAULT);
            con = environment->createConnection("gldbuser", "gldbuser", "MYDATABSE");
    
            stmt = con->createStatement("select * from example");
            res = stmt->executeQuery();
            
            while (res->next())
                std::cout<<res->getInt(1)<<"  "<<res->getString(2)<<std::endl;
            
            stmt->closeResultSet(res);
            con->terminateStatement(stmt);
            environment->terminateConnection(con);
    
        }catch(oracle::occi::SQLException &e){
            std::cout<<e.what();
        }
    
     return 0;
    }
    Hide line numbers 

  2. Open a prompt to the working directory and compile the code using the windows cl.exe compiler. Make sure to point '-I' (capital i) option to the include files and to include mysqlcppconn.lib 

    ..workspace\OracleOCCIExample>cl -o oracletest.exe oracleexample.cpp -I D:\oracle\product\10.2.0\client_1\oci\include D:\oracle\product\10.2.0\client_1\oci\lib\msvc\oraocci10.lib
    Substitue your local directories.

  3. This should now create the ocalceexample.exe file. However if you run it now you might get some errors saying that some DLLs are missing.

  4. Add these paths to your existing path so that the executable can find the required libraries. Again, don't forget to replace the directories with the ones on your local machine. Basically you need to give the 'bin' diretory of your MySQL database installation and the 'lib' directory in your MySQL Connector C++ directory.


    ..\worksapce\OracleOCCIExample>set path=%path%;D:\oracle\product\10.2.0\client_1\oci\lib\msvc\vc7

    ..\worksapce\OracleOCCIExample>set path=%path%;D:\oracle\product\10.2.0\client_1\BIN

  5. Now run the oracleexample.exe and you should see the result. The most likely errors are errors stemming from problems with the tnsnames.ora and maybe DLL entry points when you run the program. To fix the DLL entry points you can set your classpath only to the paths specified above, to make sure you are not picking up any other dll.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值