Implementation of a simple packaging class for Oracle occi

Source: Internet
Author: User

I recently learned about Oracle's c ++ programming interface occi and made a simple packaging class. The source code is posted for your reference. This program has not been strictly tested, but we are only interested in it. If you want to use it commercially, You need to further improve it. The code is tested in the xlc of vs2005 and Aix.

Note: To connect to vs2005, download the latest occi library file on the Oracle website.

Tocci. h

# Ifndef _ occidatabase_h _
# DEFINE _ occidatabase_h _
# Include <occi. h>
# Include <string>
# Include <iostream>

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" <Endl;
Cout <"error number:" <ex. geterrorcode () <Endl;
Cout <ex. getmessage () <Endl;
Throw ex;
}
};

Toccidatabase ::~ Toccidatabase ()
{
Try
{
Env-> terminateconnection (conn );
Environment: terminateenvironment (ENV );
}
Catch (sqlexception ex)
{
Cout <"exception thrown for getconnect" <Endl;
Cout <"error number:" <ex. geterrorcode () <Endl;
Cout <ex. getmessage () <Endl;
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" <Endl;
Cout <"error number:" <ex. geterrorcode () <Endl;
Cout <ex. getmessage () <Endl;
Throw ex;
}
};

Void tocciquery: Commit ()
{
Try
{
Conn-> commit ();
Stmt-> setautocommit (isautocommit );
}
Catch (sqlexception ex)
{
Cout <"exception thrown for commit" <Endl;
Cout <"error number:" <ex. geterrorcode () <Endl;
Cout <ex. getmessage () <Endl;
Throw ex;
}
};

Void tocciquery: roolback ()
{
Try
{
Conn-> rollback ();
Stmt-> setautocommit (isautocommit );
}
Catch (sqlexception ex)
{
Cout <"exception thrown for roolback" <Endl;
Cout <"error number:" <ex. geterrorcode () <Endl;
Cout <ex. getmessage () <Endl;
Throw ex;
}
};

Boolean tocciquery: getautocommit ()
{
Boolean result = false;
Try
{
Result = stmt-> getautocommit ();
}
Catch (sqlexception ex)
{
Cout <"exception thrown for getautocommit" <Endl;
Cout <"error number:" <ex. geterrorcode () <Endl;
Cout <ex. getmessage () <Endl;
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" <Endl;
Cout <"error number:" <ex. geterrorcode () <Endl;
Cout <ex. getmessage () <Endl;
Throw ex;
}
Return Rs;
};

Void tocciquery: executeupdate (string SQL)
{
Try
{
Stmt = Conn-> createstatement ();
Stmt-> executeupdate (SQL );
}
Catch (sqlexception ex)
{
Cout <"exception thrown for executeupdate" <Endl;
Cout <"error number:" <ex. geterrorcode () <Endl;
Cout <ex. getmessage () <Endl;
Throw ex;
}
};

}
# Endif/* _ occidatabase_h _*/

The main. cpp source code of the test program 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 =" <RS-> getint (1) <STD: Endl;
}

Query-> close (RS );

Delete (query );

Return 1;
}

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.