国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > Oracle > 正文

oracle OCCI 的一個簡單的包裝類的實現

2024-08-29 13:32:53
字體:
來源:轉載
供稿:網友

  最近在學習Oracle 的c++的編程接口OCCI,自己做了一個簡單的包裝類,源碼貼出來供大家參考。此程序并沒有經過嚴格的測試,只是興趣所至,大家如果要商用的話,還需進一步完善,代碼在vs2005和AIX的xlC中測試通過。

  注意:如果需要在vs2005中鏈接,需要到oracle網站上下載最新的vs2005的occi庫文件。

以下是引用片段:
  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_*/
  測試程序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 = "<getInt(1)< 
  }
  query->close(rs);
  delete(query);
  return 1;
  }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 广平县| 囊谦县| 和硕县| 琼海市| 正宁县| 云阳县| 吴忠市| 壶关县| 毕节市| 星座| 乌审旗| 麻江县| 甘德县| 丰台区| 河津市| 岢岚县| 福海县| 凯里市| 肥城市| 油尖旺区| 儋州市| 芦溪县| 蓝田县| 辉县市| 房产| 大石桥市| 南康市| 永州市| 杂多县| 昔阳县| 育儿| 武川县| 渝北区| 克什克腾旗| 绥滨县| 柳林县| 怀宁县| 独山县| 玛沁县| 鹿泉市| 白城市|