Oracle OCCI的一個(gè)簡(jiǎn)單的包裝類(lèi)的實(shí)現(xiàn)
注意:如果需要在vs2005中鏈接,需要到Oracle網(wǎng)站上下載最新的vs2005的occi庫(kù)文件。
TOcci.h #ifndef _OCCIDATABASE_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: '<< ex.getErrorCode() << endl; cout<throw ex; } }; TOcciDatabase::~TOcciDatabase() { try { env->terminateConnection (conn); Environment::terminateEnvironment (env); } catch(SQLException ex) { cout<<'Exception thrown for getConnect'<cout<<'Error number: '<< ex.getErrorCode() << 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: '<< ex.getErrorCode() << endl; cout<throw ex; } }; void TOcciQuery::commit() { try { conn->commit(); stmt->setAutoCommit(isAutoCommit); } catch(SQLException ex) { cout<<'Exception thrown for commit'<cout<<'Error number: '<< ex.getErrorCode() << endl; cout<throw ex; } }; void TOcciQuery::roolback() { try { conn->rollback(); stmt->setAutoCommit(isAutoCommit); } catch(SQLException ex) { cout<<'Exception thrown for roolback'<cout<<'Error number: '<< ex.getErrorCode() << 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: '<< ex.getErrorCode() << endl; cout<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'<cout<<'Error number: '<< ex.getErrorCode() << 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: '<< ex.getErrorCode() << endl; cout<throw ex; } }; } #endif /*_OCCIDATABASE_H_*/ 測(cè)試程序main.cpp源碼如下: // occi.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。 // #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; }
相關(guān)文章:
1. MYSQL(電話號(hào)碼,身份證)數(shù)據(jù)脫敏的實(shí)現(xiàn)2. 如何實(shí)現(xiàn)MySQL數(shù)據(jù)庫(kù)的備份與恢復(fù)3. MySQL實(shí)現(xiàn)數(shù)據(jù)批量更新功能詳解4. Mysql使用concat函數(shù)實(shí)現(xiàn)關(guān)鍵字模糊查詢(xún)功能(列表數(shù)據(jù)過(guò)濾含前后端代碼)5. mybatis plus代碼生成工具的實(shí)現(xiàn)代碼6. MySQL Router實(shí)現(xiàn)MySQL的讀寫(xiě)分離的方法7. MySQL實(shí)現(xiàn)查詢(xún)分位值的示例代碼8. 利用mysql實(shí)現(xiàn)的雪花算法案例9. Mysql分組排序取每組第一條的2種實(shí)現(xiàn)方式10. MySQL多表查詢(xún)與7種JOINS的實(shí)現(xiàn)舉例
