C++通過occi執行select語句、儲存過程、函式,取cursor值示例
#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;
}
相關文章
- 在.NET中用儲存過程執行SQL語句儲存過程SQL
- 通過儲存過程執行通過DBLINK的查詢語句失敗-單個語句成功--ORA-00604儲存過程
- 儲存過程 函式儲存過程函式
- PostgreSQL獲取建表語句儲存過程SQL儲存過程
- 通過SQL語句提取儲存過程中的內容SQL儲存過程
- 儲存過程與儲存函式儲存過程儲存函式
- 儲存過程結果進行查詢 select 存過過程儲存過程
- 15個常用sql語句 分支 迴圈 子查詢 儲存過程 事務 常用函式 sql語句執行過程(轉載備用)SQL儲存過程函式
- 儲存過程與函式儲存過程函式
- 儲存過程中使用cursor儲存過程
- 深入解讀MySQL InnoDB儲存引擎Update語句執行過程MySql儲存引擎
- MySQL儲存過程語句及呼叫MySql儲存過程
- SQL儲存過程示例SQL儲存過程
- ORACLE 儲存過程示例Oracle儲存過程
- MySQL 儲存過程和函式MySql儲存過程函式
- MySQL儲存過程 (即函式)MySql儲存過程函式
- MySQL儲存過程和函式MySql儲存過程函式
- mySql 儲存過程與函式MySql儲存過程函式
- SQL server儲存過程函式SQLServer儲存過程函式
- Entity Framework Code First執行SQL語句、檢視及儲存過程FrameworkSQL儲存過程
- Mysql 的儲存過程和儲存函式MySql儲存過程儲存函式
- oracle 儲存過程以及plsql語句塊Oracle儲存過程SQL
- Oracle 儲存過程中的DDL語句Oracle儲存過程
- 通過v$access檢視正在執行的儲存過程procedure儲存過程
- mysql執行sql語句過程MySql
- MyBatis 示例之儲存過程MyBatis儲存過程
- select 語句的解析過程
- 刪除資料庫中所有儲存過程和函式的sql語句資料庫儲存過程函式SQL
- 儲存過程vs.函式QM儲存過程函式
- mysql儲存過程基本函式(轉)MySql儲存過程函式
- MyBatis 呼叫Oracle儲存過程,傳參、返回遊標的值獲取--示例MyBatisOracle儲存過程
- 儲存過程中巢狀儲存過程的變數執行方式儲存過程巢狀變數
- 遞迴儲存過程中使用cursor遞迴儲存過程
- C++建構函式解構函式的執行過程C++函式
- mysql儲存函過程和儲存函式都屬於儲存程式MySql儲存函式
- EF中使用SQL語句或儲存過程SQL儲存過程
- mysql和orcale的儲存過程和儲存函式MySql儲存過程儲存函式
- oracle自動生成編譯所有函式、儲存過程、觸發器的語句Oracle編譯函式儲存過程觸發器