通過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 plain  copy
 
  1. <span style="color:#000000;">//include 1 header file for all OCCI classes/interfaces  
  2. #include <occi.h>  
  3.   
  4. //create Environment  
  5. Environment *env = Environment::createEnvironment();//創建連接對象指針  
  6. //use the Environment instance to create connections,  
  7. //database access  
  8. …  
  9.   
  10. //terminate Environment by calling static method  
  11. //Environment::terminateEnvironment  
  12. Environment::terminateEnvironment(env);//關閉</span>  
步驟二:連接數據庫
連接數據庫通過Connection類的對象實例實現
調用Environment類的 createConnection()方法可以創建一個Connection對象;

 

[cpp]  view plain  copy
 
  1. Connection *Environment::createConnection( const string &userName,const string &password, const string &connectString )  

 

連接數據庫成功后,可以用它來訪問數據,執行SQL語句;
使用 Environment::terminateConnection()斷開連接;
示例:
[cpp]  view plain  copy
 
  1. //First need Environment  
  2. Environment *env = Environment::createEnvironment();  
  3.   
  4. Connection *conn=env->createConnection(“scott”,”tiger”,””);  
  5. //3rd parameter is db name/TNS alias  
  6.   
  7. ..//database access – use the Connection object  
  8. ..  
  9. ..  
  10. //logoff and terminate connection  
  11. 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 plain  copy
 
  1. //createStatement() on Connection class gives a Statement  
  2. //instance  
  3. Statement *stmt = conn->createStatement(“ insert into Dept(Deptno,Dname, Loc) values (1, ‘ACCOUNTS’, ‘ZONE1’ ”);  
  4. //executeUpdate for all INSERT/UPDATE/DELETE  
  5. stmt->executeUpdate();  
  6. conn->terminateStatement(stmt);  
使用綁定參數的DML示例:
[cpp]  view plain  copy
 
  1. Statement *stmt = conn->createStatement(“ insert into  
  2. Emp(EmpNo,Ename) values(:1, :2) ”);  
  3. //1 and 2 are bind placeholders  
  4. int empno = 2;  
  5. string empname = “JOHN W”;  
  6. //first parameter is bind position, second is value  
  7. stmt->setInt(1, empno);   
  8. stmt->setString(2, empname);  
  9. stmt->executeUpdate();  
[cpp]  view plain  copy
 
  1.   
執行SELECT查詢並處理結果:
[cpp]  view plain  copy
 
  1. Statement *stmt = conn->createStatement(“ select Empno,   
  2. Ename, Sal from Emp where Hiredate >= :1”);  
  3.   
  4. //automatically converted to Date  
  5. stmt->setString(1, “01-JAN-1987”);  
  6.   
  7. //executeQuery returns a ResultSet  
  8. ResultSet *rs = stmt->executeQuery();   
  9.   
  10. //ResultSet::next fetches rows and returns FALSE   
  11. //when no more rows  
  12. while (rs->next() == true)  
  13. {  
  14.    //get values using the getXXX methods of ResultSet  
  15.    empno = rs->getInt(1);  
  16.    empname = rs->getString(2);  
  17.    empsalary = rs->getFloat(3);  
  18. }  
  19. 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 plain  copy
 
  1. //PLSQL function : functionCalculateBonus(EmpNo INNumber,  
  2. //                           EmpStatus IN OUT VARCHAR2,  
  3. //                           Bonus OUT Number)RETURN VARCHAR2  
  4. //call function usinganonymous block  
  5. Statement *stmt = conn->createStatement(“ begin :1 := CalculateBonus(  
  6.             :2, :3, :4); end;”);  
  7. //bind position 1 is thefunction’s return value  
  8. stmt->setInt(2, 100); //IN parameter  
  9. stmt->setString(3, “Active”); //IN OUT parameter  
  10. //call registerOutParam for each OUT parameter  
  11. stmt->registerOutParam(1, OCCISTRING, 1000);//function’sreturn value  
  12. stmt->setMaxParamSize(1, 100);//setMaxParamSize for STRING types  
  13. stmt->registerOutParam(4, OCCIFLOAT);  
  14. stmt->execute();  
  15. //use getXXX methods of Statement to get OUTparameters, return value  
  16. string msg = stmt->getString(1);//function return value  
  17. string newstatus = stmt->getString(3);//IN OUT parameter  
  18. float bonus = stmt->getFloat(4); //OUT parameter  

 

步驟四:錯誤處理
OCCI使用C++異常機制來返回所有的錯誤信息
應用程序中應該使用try-catch結構來處理異常
拋出的異常屬於SQLException類型,該類型繼承自標准C++中的exception類
可以使用getErrorCode和getMessage方法來獲取Oracle的錯誤信息

示例:

分別處理Oracle和C++ STL錯誤

 

 

[cpp]  view plain  copy
 
  1. try  
  2. {  
  3.    ResultSet *rs = stmt->executeQuery();  
  4.    while (rs->next())  
  5.    ……….  
  6. }  
  7. catch (SQLException &oraex) //Oracle/OCCI errors  
  8. {  
  9.   int errno = oraex->getErrorCode();//returns the ORA number  
  10.   string errmsg = oraex->getMessage();  
  11.   //more application error handling  
  12. }  
  13. catch (exception &ex) //any other C++/STL error  
  14. {  
  15.   cout << “Error “ << ex.what() << endl;  
  16. }  

可以使用Oracle為C++提供的編程開發接口,也就是 OCCI。這是一個博文,可供參考
http://blog.csdn.net/xiaobai1593/article/details/6671722 注意下載sdk時注意和你的vs版本的匹配,在oracle網站搜索“OCCI”,下載對應的版本的 basic 壓縮包 和sdk就可以了。

 

 

http://www.oracle.com/technetwork/database/occidownloads-083553.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM