首页 >> 民生历史

OracleOCCI的一个简单的包装类的节能

民生历史  2020-11-08 08:43 字号: 大 中 小

注意:如果需要在vs2005中链接,需要到Oracle站上下载最新的vs2005的occi库文件。

TOcci.h

#ifndef _OCCIDA尽量少使用一些JS和FLASH特效。站首页最好不要使用FLASH引导页。关键词TABASE_H_

#define _OCCIDATABASE_H_

#include

#include

#include

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"

cout"Error number: " tErrorCode() endl;

cout

throw ex;

}

};

TOcciDatabase::~TOcciDatabase()

{

try

{

env-terminateConnection (conn);

Environment::terminateEnvironment (env);

}

catch(SQLException ex)

{

cout"Exception thrown for getConnect"

cout"Error number: " tErrorCode() endl;

cout

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"

cout"Error number: " tErrorCode() endl;

cout

throw ex;

}

};

void TOcciQuery::commit()

{

try

{

conn-commit();

stmt-setAutoCommit(isAutoCommit);

}

catch(SQLException ex)

{

cout"Exception thrown for commit"

cout"Error number: " tErrorCode() endl;

cout

throw ex;

}

};

void TOcciQuery::roolback()

{

try

{

conn-rollback();

stmt-setAutoCommit(isAutoCommit);

}

catch(SQLException ex)

{

cout"Exception thrown for roolback"

cout"Error number: " tErrorCode() endl;

cout

throw ex;

}

};

boolean TOcciQuery::getAutoCommit()

{

boolean result = false;

try

{

result = stmt-getAutoCommit();

}

catch(SQLException ex)

{

cout"Exception thrown for getAutoCommit"

cout"Error number: " tErrorCode() endl;

cout

throw ex;

}

return result;

};

ResultSet* TOcciQuery::executeQuery(string sql)

{

ResultSet*rs = NULL;

try

{

stmt = conn-createSta让他提供要去的人的和姓名给您tement();

rs = stmt-executeQuery(sql);

}

catch (SQLException ex)

{

cout"Exception thrown for executeQuery"

cout"Error number: " tErrorCode() endl;

cout

throw ex;

}

return rs;

};

void TOcciQuery::executeUpdate(string sql)

{

try

{

stmt = conn-createStatement();

stmt-executeUpdate(sql);

}

catch (SQLException ex)

{

cout"Exception thrown for executeUpdate"

cout"Error number: " tErrorCode() endl;

cout

throw ex;

}

};

}

#endif /*_OCCIDATABASE_H_*/

测试程序p源码如下:

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

//

#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 = "getInt(1)

}

query-close(rs);

delete(query);

return 1;

}

查看本文来源

安顺白癜风在哪里治疗
宝宝积食可以贴丁桂儿脐贴吗
晋中治疗白癜风好的医院
推荐资讯