#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;
}