C++用OTL访问Oracle数据库的例子

     在Windows平台,数据库的访问手段比较丰富,如ADO或者ODBC等,然而在UNIX/Linux平台上访问数据就不是那么容易了。 如果我们使用Java作为开发语言,那么JDBC可以提供数据访问的途径,但是如果用C/C++就没这么简单了,你必须使用最原始的C/C++调用接口来访问数据库。
       目前大型的数据库,如Oracel或者DB2都提供了C/C++的调用接口,但是作为开发人员使用这些接口是一件很头痛的事情,你必须要熟记每一个API,然而OTL给我带来了一个新的数据库访问方式。
      本文将通过一个例子来展示OTL是如何使用的。希望借此使得那些和我一样的OTL入门者少走一些弯路。

开发环境:Windows 2003 + Oracle 9i
开发工具:Dev C++  V 4.9.9.0

Oracle安装路径:C:/Oracle/

我们仅仅展示数据库的连接和释放。

 

首先,我们建立一个Dev C++项目,名为“OTL”



在弹出的对话框中选择“Empty Project”


接下来选择一个位置来保存工程文件

 

之后,我们将向工程中添加我们的源代码文件,源代码文件如下:

#include <iostream>
using namespace std;
#include <stdio.h>

#define OTL_ORA9I // Compile OTL 4.0/OCI9i
#define OTL_ORA_TIMESTAMP // enable Oracle 9i TIMESTAMPs [with [local] time zone]
#include "otlv4.h" // include the OTL 4.0 header file

otl_connect db; // connect object


int
main()
{
    otl_connect::otl_initialize(); // initialize OCI environment 
    try
    {
        db.rlogon("scott/tiger@ORACLE9I "); // connect to Oracle 
        cout<<"Connect to Database"<<endl;
    }
    catch(otl_exception& p)
    { 
        // intercept OTL exceptions 
        cerr<<p.msg<<endl; // print out error message 
        cerr<<p.stm_text<<endl; // print out SQL that caused the error 
        cerr<<p.sqlstate<<endl; // print out SQLSTATE message 
        cerr<<p.var_info<<endl; // print out the variable that caused the error 
    }
 

    db.logoff(); // disconnect from Oracle

    return 0;
}

我们还需要将otlv4.h这个头文件添加到我们的工程中。

此时,我们需要设置一下头文件的路径和库文件的路径




此时,我们就可以编译并执行该程序了!

作者:http://allanyan.cnblogs.com/articles/105159.html

OTL介绍: OTLOracle, Odbc and DB2-CLI Template Library 的缩写,是一个C++操控关系数据库的模板库,最新版本4.0.104,参见http://otl.sourceforge.net/ 优点:a. 跨平台 b. 运行效率高,与C语言直接调用API相当 c. 开发效率高,起码比ADO.net使用起来更简单,更简洁 d. 部署容易,不需要ADO组件,不需要.net framework 等 缺点: a. 只有C++才可以使用她 b. 说明以及范例不足(已附带了686个实例) This document describes the Oracle, ODBC and DB2-CLI Template Library, Version 4.0 (OTL 4.0). OTL 4.0 is a C++ library based on C++ templates. OTL 4.0 was designed as a combination of a C++ template framework and OTL-adapters. The framework is a generic implementation of the concept of OTL streams. The OTL-adapters are thin wrappers around the database APIs and are used as class type parameters to be passed into the template framework. OTL 4.0 covers the functionality of a full featured C++ database access library with just a handful of classes: otl_stream, otl_connect, otl_exception, otl_long_string, and several template PL/SQL (Oracle) table container classes, generated from the template framework and the OTL-adapters. The OTL code gets expanded into direct database API function calls, so it provides very decent performance (only 10-15% overhead, compared with the database APIs themselves) and reliability in multi-processor environments as well as traditional batch programs. OTL 4.0, being a template library, is highly portable since it is self-sufficient and compact enough. OTL 4.0 is ANSI C++ compliant (ANSI C++ typecasts, clean templatized code, etc.), tightly integrated with the Standard Template Library (STL) via STL-compliant stream iterators, and natively supports the STL std::string's in otl_stream's. OTL integrates with ACE, and supports ACE_TStrings. OTL 4.0 supports all versions of Oracle starting with 7.3 (natively via the corresponding version of the OCI), DB2 UDB LUW / zOS (natively via DB2 CLI), MS SQL Server 2005/2008 (natively via SNAC), Informix 11 (natively via Informix CLI), TimesTen 7 and higher (natively TimesTen CLI), SAP-MAX/DB (natively via SAP/DB CLI), ODBC 3.x as well as ODBC 2.5 (for legacy applications) compliant data sources in MS Windows, Linux/Unix/Mac OS X (via unixodbc and iODBC driver managers): Sybase, MySQL, PostgreSQL, EnterpriseDB, SQLite, MS ACCESS, Firebird, etc. The list of supported database back ends is growing. In the last few years, transition from the 32-bit platforms to the 64-bit platforms has occurred: OTL's source code is portable, and it support both 32-bit and 64-bit C++ compilers. Also, OTL supports UTF-8 and UTF-16 for Oracle, and UTF-16 for the rest of the database types, when the underlying database API / ODBC driver supports it.
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值