中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 数据库开发 > Oracle
Oracle OCCI的一个简单的包装类的实现
作者:佚名 时间:2007-06-19 15:07 出处:中国IT实验室 责编:月夜寒箫
              摘要:Oracle 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;
              }
关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有