WEB开发网
开发学院数据库Oracle oracle OCCI 的一个简单的包装类的实现 阅读

oracle OCCI 的一个简单的包装类的实现

 2007-05-08 12:11:07 来源:WEB开发网   
核心提示:最近在学习oracle 的c++的编程接口OCCI,自己做了一个简单的包装类,oracle OCCI 的一个简单的包装类的实现,源码贴出来供大家参考,此程序并没有经过严格的测试,注意:如果需要在vs2005中链接,需要到oracle网站上下载最新的vs2005的occi库文件,只是兴趣所至,大家如果要商用的话

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

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

以下是引用片段:
  TOcci.h
  #ifndef_OCCIDATABASE_H_
  #define_OCCIDATABASE_H_
  #include 
  #include 
  #include 
  usingnamespaceoracle::occi;
  usingnamespacestd;
  namespacehappyever
  {
  classTOcciDatabase
  {
  public:
  staticTOcciDatabase*getInstance(stringusr,stringpasswd,stringdb);
  intgetConnectCount(){return_Instance->count;};
  Connection*getConnect(){count++;return_Instance->conn;};
  ~TOcciDatabase();
  protected:
  TOcciDatabase(){};
  TOcciDatabase(stringusr,stringpasswd,stringdb);
  private:
  staticTOcciDatabase*_Instance;
  staticintcount;
  Environment*env;
  Connection*conn;
  };
  intTOcciDatabase::count=0;
  TOcciDatabase*TOcciDatabase::_Instance=0;
  TOcciDatabase::TOcciDatabase(stringusr,stringpasswd,stringdb)
  {
  try
  {
  env=Environment::createEnvironment(Environment::DEFAULT);
  conn=env->createConnection(usr,passwd,db);
  }
  catch(SQLExceptionex)
  {
  cout<<"ExceptionthrownforgetConnect"< 
  cout<<"Errornumber:"<<ex.getErrorCode()<<endl;
  cout< 
  throwex;
  }
  };
  TOcciDatabase::~TOcciDatabase()
  {
  try
  {
  env->terminateConnection(conn);
  Environment::terminateEnvironment(env);
  }
  catch(SQLExceptionex)
  {
  cout<<"ExceptionthrownforgetConnect"< 
  cout<<"Errornumber:"<<ex.getErrorCode()<<endl;
  cout< 
  throwex;
  }
  };
  TOcciDatabase*TOcciDatabase::getInstance(stringusr,stringpasswd,stringdb)
  {
  if(_Instance==0)
  {
  _Instance=newTOcciDatabase(usr,passwd,db);
  }
  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:"<<ex.getErrorCode()<<endl;
  cout< 
  throwex;
  }
  };
  voidTOcciQuery::commit()
  {
  try
  {
  conn->commit();
  stmt->setAutoCommit(isAutoCommit);
  }
  catch(SQLExceptionex)
  {
  cout<<"Exceptionthrownforcommit"< 
  cout<<"Errornumber:"<<ex.getErrorCode()<<endl;
  cout< 
  throwex;
  }
  };
  voidTOcciQuery::roolback()
  {
  try
  {
  conn->rollback();
  stmt->setAutoCommit(isAutoCommit);
  }
  catch(SQLExceptionex)
  {
  cout<<"Exceptionthrownforroolback"< 
  cout<<"Errornumber:"<<ex.getErrorCode()<<endl;
  cout< 
  throwex;
  }
  };
  booleanTOcciQuery::getAutoCommit()
  {
  booleanresult=false;
  try
  {
  result=stmt->getAutoCommit();
  }
  catch(SQLExceptionex)
  {
  cout<<"ExceptionthrownforgetAutoCommit"< 
  cout<<"Errornumber:"<<ex.getErrorCode()<<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:"<<ex.getErrorCode()<<endl;
  cout< 
  throwex;
  }
  returnrs;
  };
  voidTOcciQuery::executeUpdate(stringsql)
  {
  try
  {
  stmt=conn->createStatement();
  stmt->executeUpdate(sql);
  }
  catch(SQLExceptionex)
  {
  cout<<"ExceptionthrownforexecuteUpdate"< 
  cout<<"Errornumber:"<<ex.getErrorCode()<<endl;
  cout< 
  throwex;
  }
  };
  }
  #endif/*_OCCIDATABASE_H_*/
  测试程序main.cpp源码如下:
  //occi.cpp:定义控制台应用程序的入口点。
  //
  #include"stdafx.h"
  #include"TOcci.h"
  int_tmain(intargc,_TCHAR*argv[])
  {
  usingnamespacehappyever;
  TOcciQuery*query=new
  TOcciQuery(TOcciDatabase::getInstance("cal","cal","v2b76")->getConnect());
  stringstrSQL="selectcount(*)fromserv_value_total";
  ResultSet*rs=query->executeQuery(strSQL);
  while(rs->next())
  {
  std::cout<<"count="<getInt(1)< 
  }
  query->close(rs);
  delete(query);
  return1;
  }

Tags:oracle OCCI 一个

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接