数据库

位置:IT落伍者 >> 数据库 >> 浏览文章

oracleOCCI的一个简单的包装类的实现


发布日期:2024年05月03日
 
oracleOCCI的一个简单的包装类的实现

最近在学习oracle 的c++的编程接口OCCI自己做了一个简单的包装类源码贴出来供大家参考此程序并没有经过严格的测试只是兴趣所至大家如果要商用的话还需进一步完善代码在vs和AIX的xlC中测试通过

注意如果需要在vs中链接需要到oracle网站上下载最新的vs的occi库文件


以下是引用片段

TOccih

#ifndef_OCCIDATABASE_H_

#define_OCCIDATABASE_H_

#include

#include

#include

usingnamespaceoracle::occi;

usingnamespacestd;

namespacehappyever

{

classTOcciDatabase

{

public:

staticTOcciDatabase*getInstance(stringusrstringpasswdstringdb);

intgetConnectCount(){return_Instance>count;};

Connection*getConnect(){count++;return_Instance>conn;};

~TOcciDatabase();

protected:

TOcciDatabase(){};

TOcciDatabase(stringusrstringpasswdstringdb);

private:

staticTOcciDatabase*_Instance;

staticintcount;

Environment*env;

Connection*conn;

};

intTOcciDatabase::count=;

TOcciDatabase*TOcciDatabase::_Instance=;

TOcciDatabase::TOcciDatabase(stringusrstringpasswdstringdb)

{

try

{

env=Environment::createEnvironment(Environment::DEFAULT);

conn=env>createConnection(usrpasswddb);

}

catch(SQLExceptionex)

{

cout<<ExceptionthrownforgetConnect<

cout<<Errornumber:<<exgetErrorCode()<<endl;

cout<

throwex;

}

};

TOcciDatabase::~TOcciDatabase()

{

try

{

env>terminateConnection(conn);

Environment::terminateEnvironment(env);

}

catch(SQLExceptionex)

{

cout<<ExceptionthrownforgetConnect<

cout<<Errornumber:<<exgetErrorCode()<<endl;

cout<

throwex;

}

};

TOcciDatabase*TOcciDatabase::getInstance(stringusrstringpasswdstringdb)

{

if(_Instance==)

{

_Instance=newTOcciDatabase(usrpasswddb);

}

return_Instance;

};

classTOcciQuery

{

private:

Connection*conn;

Statement*stmt;

boolisAutoCommit;

TOcciQuery(){};

public:

TOcciQuery(Connection*connect){conn=connect;};

voidbeginTrans();

voidcommit();

voidroolback();

booleangetAutoCommit();

ResultSet*executeQuery(stringsql);

voidexecuteUpdate(stringsql);

voidclose(){if(stmt!=NULL)conn>terminateStatement(stmt);};

voidclose(ResultSet*rs);

};

voidTOcciQuery::close(ResultSet*rs)

{

if(rs!=NULL)

stmt>closeResultSet(rs);

if(stmt!=NULL)

conn>terminateStatement(stmt);

};

voidTOcciQuery::beginTrans()

{

try

{

isAutoCommit=stmt>getAutoCommit();

stmt>setAutoCommit(false);

}

catch(SQLExceptionex)

{

cout<<ExceptionthrownforbeginTrans<

cout<<Errornumber:<<exgetErrorCode()<<endl;

cout<

throwex;

}

};

voidTOcciQuery::commit()

{

try

{

conn>commit();

stmt>setAutoCommit(isAutoCommit);

}

catch(SQLExceptionex)

{

cout<<Exceptionthrownforcommit<

cout<<Errornumber:<<exgetErrorCode()<<endl;

cout<

throwex;

}

};

voidTOcciQuery::roolback()

{

try

{

conn>rollback();

stmt>setAutoCommit(isAutoCommit);

}

catch(SQLExceptionex)

{

cout<<Exceptionthrownforroolback<

cout<<Errornumber:<<exgetErrorCode()<<endl;

cout<

throwex;

}

};

booleanTOcciQuery::getAutoCommit()

{

booleanresult=false;

try

{

result=stmt>getAutoCommit();

}

catch(SQLExceptionex)

{

cout<<ExceptionthrownforgetAutoCommit<

cout<<Errornumber:<<exgetErrorCode()<<endl;

cout<

throwex;

}

returnresult;

};

ResultSet*TOcciQuery::executeQuery(stringsql)

{

ResultSet*rs=NULL;

try

{

stmt=conn>createStatement();

rs=stmt>executeQuery(sql);

}

catch(SQLExceptionex)

{

cout<<ExceptionthrownforexecuteQuery<

cout<<Errornumber:<<exgetErrorCode()<<endl;

cout<

throwex;

}

returnrs;

};

voidTOcciQuery::executeUpdate(stringsql)

{

try

{

stmt=conn>createStatement();

stmt>executeUpdate(sql);

}

catch(SQLExceptionex)

{

cout<<ExceptionthrownforexecuteUpdate<

cout<<Errornumber:<<exgetErrorCode()<<endl;

cout<

throwex;

}

};

}

#endif/*_OCCIDATABASE_H_*/

测试程序maincpp源码如下

//occicpp:定义控制台应用程序的入口点

//

#includestdafxh

#includeTOccih

int_tmain(intargc_TCHAR*argv[])

{

usingnamespacehappyever;

TOcciQuery*query=new

TOcciQuery(TOcciDatabase::getInstance(calcalvb)>getConnect());

stringstrSQL=selectcount(*)fromserv_value_total;

ResultSet*rs=query>executeQuery(strSQL);

while(rs>next())

{

std::cout<<count=<getInt()<

}

query>close(rs);

delete(query);

return;

}

               

上一篇:Oracle聚集函数排序

下一篇:利用作业备份和恢复数据库