C++通過occi執行select語句、儲存過程、函式,取cursor值示例

冷月宮主發表於2014-03-21

#include <stdio.h>

#include <iostream>
#include <String>
using namespace std;

#include <occi.h>
#include <occiControl.h>
using namespace oracle::occi;


/*
void selectDemo()
{
Environment      *pEnv   =    NULL; //定義執行環境物件
Connection       *pConn =    NULL; //定義連結物件
Statement        *pStmt =    NULL; //定義執行語句物件
ResultSet        *pRs    =    NULL; //定義結果集物件

const string szUserName   = "demo"; //設定使用者名稱
const string szUserPwd    = "demo"; //設定密碼
const string szConnection = "icss"; //設定要連結的資料庫,"icss"是資料庫名

try
{
   pEnv = Environment::createEnvironment(); //建立了一個執行環境
   pConn = pEnv->createConnection(szUserName, szUserPwd, szConnection);
                                                  //建立了一個連結,有三個引數。
   if ( NULL == pConn) //如果連結建立失敗,執行以下程式碼
   {
    cout << "Connection Failure" << endl;
    return;
   }

   pStmt = pConn->createStatement(); //通過connection物件建立一個statement物件
   string strSql = "select * from test1";
   pStmt -> setSQL(strSql); //設定語句執行物件的屬性
   pRs = pStmt->executeQuery(); //執行呼叫,返回結果集物件
   while ( pRs -> next())        //對結果集遍歷,輸出結果集裡的資料
   {
    cout << pRs->getInt(1) << " " << pRs->getString(2) << endl;
   }    //getInt()和getString()方法的使用取決於資料集裡資料的型別。1和2指第一、第二個欄位
}
catch ( SQLException ex)
{
   cout << ex.getMessage() << endl;
}

pStmt -> closeResultSet(pRs);     //statement物件呼叫方法關閉資料集
pConn -> terminateStatement(pStmt); //connection物件呼叫terminateStatement()方法關閉statement物件
pEnv -> terminateConnection(pConn); //environment物件呼叫方法關閉connection物件
Environment::terminateEnvironment(pEnv); //關閉環境物件。

//return 0;
}

void procedureDemo()
{
Environment      *pEv   =   NULL;
Connection       *pConn =   NULL;
Statement        *pStmt =   NULL;

const   string username = "demo";
const   string password = "demo";
const   string database = "icss";

try
{
   pEv   =   Environment::createEnvironment(); 
   pConn =   pEv->createConnection(username, password, database);

   if ( NULL == pConn)
   {
    cout << "Connection Failure!" << endl;
    return;
   }

   pStmt = pConn->createStatement("BEGIN sp_selectspecial(:1, :2, :3); end;"); 
   //執行儲存過程與執行SQL語句的不同:執行sql語句時,createstatement的引數是select引數。而執行儲存過程時
   //createstatement的引數是“BEGIN SPNAME(PARAM1, PARAM2 …); end;”。
                                                                        
     
   pStmt -> setInt(1, 43);
   pStmt -> registerOutParam(2, OCCISTRING, 30, "");
   pStmt -> registerOutParam(3, OCCISTRING, 30, "");
   //pStmt -> registerOutParam(4, OCCINUMBER, 10, "");
   //pStmt -> registerOutParam(5, );

   int updateCount = pStmt->execute();

   cout << pStmt->getString(2) << " " << pStmt->getString(3) << endl;
  
   pConn->terminateStatement(pStmt);
   pEv->terminateConnection(pConn);
   Environment::terminateEnvironment(pEv);
}
catch ( SQLException ex)
{
        cout << ex.getMessage() << endl;
}
}


void functionDemo()
{
Environment *pEvn =   NULL;
Connection   *pConn =   NULL;
Statement    *pStmt =   NULL;

const     string username = "demo";
const     string password = "demo";
const     string database = "icss";

try
{
   pEvn = Environment::createEnvironment();
   pConn = pEvn->createConnection(username, password, database);
   if ( NULL == pConn)
   {
    cout << "Connection Failure!" << endl;
    return;
   }
     pStmt = pConn->createStatement(" BEGIN :1:=fun_GetSalary(:2); END; ");
  
   pStmt->registerOutParam(1, OCCIINT,30,"");

   //try
   //{
    pStmt->setInt(2,2);
   //}
   //catch ( SQLException ex)
   //{
   // cout << ex.getMessage() << endl;
   //}

   pStmt->executeUpdate();
   cout << pStmt->getInt(1) << endl;
  
}
catch ( SQLException ex)
{
   cout << ex.getMessage() << endl;
}

}
*/

void cursorDemo()
{
Environment      *pEv   =   NULL;
Connection       *pConn =   NULL;
Statement        *pStmt =   NULL;
ResultSet        *pRs   =   NULL;

const   string username = "demo";
const   string password = "demo";
const   string database = "icss";

try
{
   pEv   =   Environment::createEnvironment(); 
   pConn =   pEv->createConnection(username, password, database);

   if ( NULL == pConn)
   {
    cout << "Connection Failure!" << endl;
    return;
   }

   pStmt = pConn->createStatement("BEGIN sp_GetSpecialByID(:1, :2, :3, :4, :5); end;"); 
   //執行儲存過程與執行SQL語句的不同:執行sql語句時,createstatement的引數是select引數。而執行儲存過程時
   //createstatement的引數是“BEGIN SPNAME(PARAM1, PARAM2 …); end;”。
                                                                        
     
   pStmt -> setInt(1, 12);
   pStmt -> setString(2, "");
   pStmt -> setString(3, "");
   pStmt -> setInt(4, -1);
   pStmt -> registerOutParam(5, OCCICURSOR);

   int updateCount = pStmt->execute();

   pRs = pStmt->getCursor(5);
        pRs->next();
     cout << pRs->getString(2) << endl;
  
   pStmt->closeResultSet(pRs);
   pConn->terminateStatement(pStmt);
   pEv->terminateConnection(pConn);
   Environment::terminateEnvironment(pEv);
}
catch ( SQLException ex)
{
        cout << ex.getMessage() << endl;
}
}

int main(int argc, char* argv[])
{
//procedureDemo();
//selectDemo();
//functionDemo();
cursorDemo();
return 0;
}

相關文章