Implementation of a simple wrapper class for Oracle Occi

Source: Internet
Author: User
Tags commit count stmt wrapper

I made a simple packaging class, the source code posted for your reference. This program is not rigorously tested and needs further refinement, and the code is tested through the xlc of Vs2005 and Aix.

Note: If you need to link in vs2005, you will need to download the latest VS2005 Occi library files on the Oracle Web site.

The following is a reference fragment:

  TOcci.h #ifndef _occidatabase_h_ #define _occidatabase_h_ #include #include #include using Namespac 
  e Oracle::occi; 
  using namespace Std; Namespace Happyever {class Toccidatabase {public:static toccidatabase* getinstance (String usr, string pa 
  SSWD, 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::createenvironmen 
  T (Environment::D efault); 
  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) {_instan 
  CE = 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); The catch (SQLException ex) {cout<< "Exception thrown for Roolback" < cout<< "Error number:" <&l T 
  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:" & 
  lt;< 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:" & 
  lt;< Ex.geterrorcode () << Endl; 
  cout< throw ex; 
  } return RS; 
  }; void Tocciquery::executeupdatE (String sql) {try {stmt = Conn->createstatement (); 
  Stmt->executeupdate (SQL); The catch (SQLException ex) {cout<< "Exception thrown for executeupdate" < cout<< "Error number:" 
  << Ex.geterrorcode () << Endl; 
  cout< throw ex; 
  }   }; 
  #endif/*_occidatabase_h_*/test Program main.cpp source code is as follows://Occi.cpp: Defines the entry point of the console application.  
  #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; }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.