通过OCCI连接oracle(C++)

OCCI介绍
OCCI:Oracle C++调用接口(OCCI),即Oracle的C++API,允许你使用面向对象的特性、本地类、C++语言的方法来访问Oracle数据库。

OCCI优势
基于标准C++和面向对象的设计;
效率较高;
适合开发C/S模式的程序,软件中间层;
OCCI特性
完整支持SQL/PLSQL
为不断增长的用户和请求提供了弹性选项
为使用用户自定义类型,如C中的类,提供了无缝接口
支持所有的Oracle数据类型以及LOB types
可以访问数据库元数据
OCCI头文件及动态链接库
OCCI 头文件

•occi.h
•occiCommon.h
•occiControl.h
•occiData.h
•occiObjects.h
OCCI库

•libocci.so/libocci.a/oraocci9.dll
构建应用程序
步骤1:初始化
OCCI通过创建一个Environment的对象完成初始化工作。
可以通过Environment创建数据库连接,从而进行其它的操作
要创建Environment,应该调用Environment类的静态方法createEnvironment()
示例:
[cpp] view plaincopy
<span style="color:#000000;">//include 1 header file for all OCCI classes/interfaces  
#include <occi.h>  
  
//create Environment  
Environment *env = Environment::createEnvironment();//创建连接对象指针  
//use the Environment instance to create connections,  
//database access  
//terminate Environment by calling static method  
//Environment::terminateEnvironment  
Environment::terminateEnvironment(env);//关闭</span>  
步骤二:连接数据库
连接数据库通过Connection类的对象实例实现
调用Environment类的createConnection()方法可以创建一个Connection对象;
[cpp] view plaincopy
Connection *Environment::createConnection( const string &userName,const string &password, const string &connectString )  
连接数据库成功后,可以用它来访问数据,执行SQL语句;
使用Environment::terminateConnection()断开连接;
示例:
[cpp] view plaincopy
//First need Environment  
Environment *env = Environment::createEnvironment();  
  
Connection *conn=env->createConnection(“scott”,”tiger”,””);  
//3rd parameter is db name/TNS alias  
  
..//database access – use the Connection object  
..  
..  
//logoff and terminate connection  
env->terminateConnection(conn);//断开连接  
步骤三:执行SQL/PLSQL
Statement类用于执行SQL/PLSQL语句,并获取返回结果。
ResultSet 类用于处理SELECT 查询的结果。
对于所有类型的数据的绑定或者获取,OCCI都提供了统一的方法
  - setXXX  方法用于Statement

  - getXXX 方法用于Statement & ResultSet

OCCI会自动处理类型之间的转换。
使用方法:
使用Connection::createStatement()创建Statement对象
指定 SQL 命令(DDL/DML/query)作为参数
   Connection::createStatement(string &sql);

   Statement::setSQL(string &sql);

  Statement::execute(string &sql); - can be used for any SQL, returnsstatus

   Statement::executeUpdate(string &sql); - returns Insert/Update/Delete count

   Statement::executeQuery(string &sql); - returns ResultSet(结果集)

使用 setXXX 方法传递要绑定用于输入的值
使用合适的execute方法执行SQL
对于SELECT 查询, 使用ResultSet 对象处理返回结果
简单的DML Insert示例:
[cpp] view plaincopy
//createStatement() on Connection class gives a Statement  
//instance  
Statement *stmt = conn->createStatement(“ insert into Dept(Deptno,Dname, Loc) values (1, ‘ACCOUNTS’, ‘ZONE1’ ”);  
//executeUpdate for all INSERT/UPDATE/DELETE  
stmt->executeUpdate();  
conn->terminateStatement(stmt);  
使用绑定参数的DML示例:
[cpp] view plaincopy
Statement *stmt = conn->createStatement(“ insert into  
Emp(EmpNo,Ename) values(:1, :2) ”);  
//1 and 2 are bind placeholders  
int empno = 2;  
string empname = “JOHN W”;  
//first parameter is bind position, second is value  
stmt->setInt(1, empno);   
stmt->setString(2, empname);  
stmt->executeUpdate();  
[cpp] view plaincopy
  
执行SELECT查询并处理结果:
[cpp] view plaincopy
Statement *stmt = conn->createStatement(“ select Empno,   
Ename, Sal from Emp where Hiredate >= :1”);  
  
//automatically converted to Date  
stmt->setString(1, “01-JAN-1987”);  
  
//executeQuery returns a ResultSet  
ResultSet *rs = stmt->executeQuery();   
  
//ResultSet::next fetches rows and returns FALSE   
//when no more rows  
while (rs->next() == true)  
{  
   //get values using the getXXX methods of ResultSet  
   empno = rs->getInt(1);  
   empname = rs->getString(2);  
   empsalary = rs->getFloat(3);  
}  
stmt->closeResultSet(rs);//to free resources  
执行PL/SQL:
要执行PL/SQL,应该在创建Statement的时候指定PL/SQL块
使用setXXX将所有的输入参数(IN andIN/OUT)传给PLSQL函数或者过程
使用Statement::registerOutParam来指定OUT参数,参数的大小通过Statement::setMaxParamSize指定
使用Statement::execute()执行PL/SQL
使用getXXX方法来获取函数执行的结果及OUT/IN OUT参数
示例:Calling PL/SQL function/procedure
[cpp] view plaincopy
//PLSQL function : functionCalculateBonus(EmpNo INNumber,  
//                           EmpStatus IN OUT VARCHAR2,  
//                           Bonus OUT Number)RETURN VARCHAR2  
//call function usinganonymous block  
Statement *stmt = conn->createStatement(“ begin :1 := CalculateBonus(  
            :2, :3, :4); end;”);  
//bind position 1 is thefunction’s return value  
stmt->setInt(2, 100); //IN parameter  
stmt->setString(3, “Active”); //IN OUT parameter  
//call registerOutParam for each OUT parameter  
stmt->registerOutParam(1, OCCISTRING, 1000);//function’sreturn value  
stmt->setMaxParamSize(1, 100);//setMaxParamSize for STRING types  
stmt->registerOutParam(4, OCCIFLOAT);  
stmt->execute();  
//use getXXX methods of Statement to get OUTparameters, return value  
string msg = stmt->getString(1);//function return value  
string newstatus = stmt->getString(3);//IN OUT parameter  
float bonus = stmt->getFloat(4); //OUT parameter  
步骤四:错误处理
OCCI使用C++异常机制来返回所有的错误信息
应用程序中应该使用try-catch结构来处理异常
抛出的异常属于SQLException类型,该类型继承自标准C++中的exception类
可以使用getErrorCode和getMessage方法来获取Oracle的错误信息
示例:
分别处理Oracle和C++ STL错误
[cpp] view plaincopy
try  
{  
   ResultSet *rs = stmt->executeQuery();  
   while (rs->next())  
   ……….  
}  
catch (SQLException &oraex) //Oracle/OCCI errors  
{  
  int errno = oraex->getErrorCode();//returns the ORA number  
  string errmsg = oraex->getMessage();  
  //more application error handling  
}  
catch (exception &ex) //any other C++/STL error  
{  
  cout << “Error “ << ex.what() << endl;  
}  
原文地址:https://www.cnblogs.com/klxll/p/3425976.html