最近在学习Oracle 的c++的编程接口OCCI,自己做了一个简单的包装类,源码贴出来供大家参考。此程序并没有经过严格的测试,只是兴趣所至,大家如果要商用的话,还需进一步完善,代码在vs2005和AIX的xlC中测试通过。
注意:如果需要在vs2005中链接,需要到Oracle网站上下载最新的vs2005的OCCI库文件。
TOCCI.h
#ifndef _OCCIDATABASE_H_
#define _OCCIDATABASE_H_
#include <OCCI.h>
#include <string>
#include <iostream>
using namespace Oracle::OCCI;
using namespace std;
namespace happyever
{
class TOCCIDatabase
{
public:
static TOCCIDatabase* getInstance(string usr, string passwd, string db);
int getConnectCount(){ return _Instance->count; };
Connection* getConnect(){ count++;return _Instance->conn; };
~TOCCIDatabase();
protected:
TOCCIDatabase(){};
TOCCIDatabase(string usr, string passwd, string db);
private:
static TOCCIDatabase* _Instance;
static int count;
Environment *env;
Connection *conn;
};
int TOCCIDatabase::count = 0;
TOCCIDatabase* TOCCIDatabase::_Instance = 0;
TOCCIDatabase::TOCCIDatabase(string usr, string passwd, string db)
{
try
{
env = Environment::createEnvironment (Environment::DEFAULT);
conn = env->createConnection (usr, passwd, db);
}
catch(SQLException ex)
{
cout<<"Exception thrown for getConnect"<<endl;
cout<<"Error number: "<< ex.getErrorCode() << endl;
cout<<ex.getMessage() << endl;
throw ex;
}
};
TOCCIDatabase::~TOCCIDatabase()
{
try
{
env->terminateConnection (conn);
Environment::terminateEnvironment (env);
}
catch(SQLException ex)
{
cout<<"Exception thrown for getConnect"<<endl;
cout<<"Error number: "<< ex.getErrorCode() << endl;
cout<<ex.getMessage() << endl;
throw ex;
}
};
TOCCIDatabase* TOCCIDatabase::getInstance(string usr, string passwd, string db)
{
if(_Instance == 0)
{
_Instance = new TOCCIDatabase(usr,passwd,db);
}
return _Instance;
};
class TOCCIQuery
{
private:
Connection *conn;
Statement *stmt;
bool isAutoCommit;
TOCCIQuery(){};
public :
TOCCIQuery(Connection *connect){ conn = connect; };
void beginTrans();
void commit();
void roolback();
boolean getAutoCommit();
ResultSet* executeQuery(string sql) ;
void executeUpdate(string sql) ;
void close() { if(stmt != NULL) conn->terminateStatement (stmt); };
void close(ResultSet* rs);
};
void TOCCIQuery::close(ResultSet* rs)
{
if(rs != NULL)
stmt->closeResultSet (rs);
if(stmt != NULL)
conn->terminateStatement (stmt);
};
void TOCCIQuery::beginTrans()
{
try
{
isAutoCommit = stmt->getAutoCommit();
stmt->setAutoCommit(false);
}
catch(SQLException ex)
{
cout<<"Exception thrown for beginTrans"<<endl;
cout<<"Error number: "<< ex.getErrorCode() << endl;
cout<<ex.getMessage() << endl;
throw ex;
}
};
void TOCCIQuery::commit()
{
try
{
conn->commit();
stmt->setAutoCommit(isAutoCommit);
}
catch(SQLException ex)
{
cout<<"Exception thrown for commit"<<endl;
cout<<"Error number: "<< ex.getErrorCode() << endl;
cout<<ex.getMessage() << endl;
throw ex;
}
};
void TOCCIQuery::roolback()
{
try
{
conn->rollback();
stmt->setAutoCommit(isAutoCommit);
}
catch(SQLException ex)
{
cout<<"Exception thrown for roolback"<<endl;
cout<<"Error number: "<< ex.getErrorCode() << endl;
cout<<ex.getMessage() << endl;
throw ex;
}
};
boolean TOCCIQuery::getAutoCommit()
{
boolean result = false;
try
{
result = stmt->getAutoCommit();
}
catch(SQLException ex)
{
cout<<"Exception thrown for getAutoCommit"<<endl;
cout<<"Error number: "<< ex.getErrorCode() << endl;
cout<<ex.getMessage() << endl;
throw ex;
}
return result;
};
ResultSet* TOCCIQuery::executeQuery(string sql)
{
ResultSet*rs = NULL;
try
{
stmt = conn->createStatement();
rs = stmt->executeQuery(sql);
}
catch (SQLException ex)
{
cout<<"Exception thrown for executeQuery"<<endl;
cout<<"Error number: "<< ex.getErrorCode() << endl;
cout<<ex.getMessage() << endl;
throw ex;
}
return rs;
};
void TOCCIQuery::executeUpdate(string sql)
{
try
{
stmt = conn->createStatement();
stmt->executeUpdate(sql);
}
catch (SQLException ex)
{
cout<<"Exception thrown for executeUpdate"<<endl;
cout<<"Error number: "<< ex.getErrorCode() << endl;
cout<<ex.getMessage() << endl;
throw ex;
}
};
}
#endif /*_OCCIDATABASE_H_*/
测试程序main.cpp源码如下:
// OCCI.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "TOCCI.h"
int _tmain(int argc, _TCHAR* argv[])
{
using namespace happyever;
TOCCIQuery *query = new
TOCCIQuery(TOCCIDatabase::getInstance("cal","cal","v2b76")->getConnect());
string strSQL = "select count(*) from serv_value_total";
ResultSet* rs = query->executeQuery(strSQL);
while(rs->next())
{
std::cout<<"count = "<<rs->getInt(1)<<std::endl;
}
query->close(rs);
delete(query);
return 1;
}