OCCI Demonstration Programs

更新时间:2024-03-02 11:35:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

OCCI Demonstration Programs

Oracle provides code examples illustrating the use of OCCI calls. These programs are provided for demonstration purposes, and are not guaranteed to run on all platforms.

The demonstration programs (demos) are available with your Oracle

installation. The location, names, and availability of the programs may vary on different platforms. On a UNIX workstation, the programs are installed in the ORACLE_HOME/rdbms/demo directory.

The $ORACLE_HOME/rdbms/demo directory contains not only demos but the file named demo_rdbms.mk that must be used as a template for building your own OCCI applications or external procedures. Building a new

demo_rdbms.mk file consists of customizing the demo_rdbms.mk file by adding your own macros to the link line. However, Oracle requires that you keep the macros provided in the demo_rdbms.mk file, as it will result in simplified maintenance of your own demo_rdbms.mk files.

When a specific header or SQL file is required by the application, these files are also included. Review the information in the comments at the beginning of the demonstration programs for setups and hints on running the programs.

Prior to running any of these demos, the SQL file occidemo.sql must be run using the user name and password SCOTT and TIGER respectively. Table A-1 lists the important demonstration programs and the OCCI features that they illustrate.

Table A-1 OCCI Demonstration Programs

Program Name Features Illustrated occiblob.cpp Demonstrates how to read and write BLOBs occiclob.cpp Demonstrates how to read and write CLOBs occicoll.cpp Demonstrates how to perform simple insert, delete, and update operations on a table column of type Nested Table occidesc.cpp Demonstrates how to obtain metadata about a table, procedure, and object occidml.cpp Demonstrates how to perform insert, select, update, and delete operations of a table row by using OCCI Program Name Features Illustrated occiinh.cpp Demonstrates object inheritance by using insert, select, update, and delete operations on a table row of subtype table occiobj.cpp Demonstrates how to perform insert, select, update, and delete operations on a table row containing an object as one of its columns occipobj.cpp Demonstrates how to perform insert, select, and update operations on persistent objects, as well as how to pin, unpin, mark for deletion, and flush an object occipool.cpp Demonstrates how to use the connection pool interface of OCCI occiproc.cpp Demonstrates how to invoke PL/SQL procedures with bind parameters occistre.cpp Demonstrates how to use OCCI ResultSet streams OCCI Demonstration Programs

This section lists each of the OCCI demonstration program files, in addition to the demo make file.

demo_rdbms.mk

The following code is for the make file that generates the demonstration programs:

#

# Example for building demo OCI programs: #

# 1. All OCI demos (including extdemo2, extdemo4 and extdemo5): #

# make -f demo_rdbms.mk demos #

# 2. A single OCI demo: #

# make -f demo_rdbms.mk build EXE=demo OBJS=\# e.g. make -f demo_rdbms.mk build EXE=oci02 OBJS=oci02.o #

# 3. A single OCI demo with static libraries: #

# make -f demo_rdbms.mk build_static EXE=demo OBJS=\# e.g. make -f demo_rdbms.mk build_static EXE=oci02 OBJS=oci02.o #

# 4. To regenerate shared library: #

# make -f demo_rdbms.mk generate_sharedlib #

# 5. All OCCI demos #

# make -f demo_rdbms.mk occidemos #

# 6. A single OCCI demo: #

# make -f demo_rdbms.mk # e.g. make -f demo_rdbms.mk occidml # OR

# make -f demo_rdbms.mk buildocci EXE=demoname OBJS=\...\# e.g. make -f demo_rdbms.mk buildocci EXE=occidml OBJS=occidml.o #

# 7. A single OCCI demo with static libraries: #

# make -f demo_rdbms.mk buildocci_static EXE=demoname OBJS=\

# e.g. make -f demo_rdbms.mk buildocci_static EXE=occiblob OBJS=occiblob.o # # 8. All OCI Connection Pooling, Session Pooling and Statement Cache demos #

# make -f demo_rdbms.mk cpdemos #

# 9. A single OCI Connection Pooling demo: #

# make -f demo_rdbms.mk # e.g. make -f demo_rdbms.mk ocicp # OR

# make -f demo_rdbms.mk buildcp EXE=demoname OBJS=\# e.g. make -f demo_rdbms.mk buildcp EXE=ocicp OBJS=ocicp.o #

# 10. A single OCI Connection Pooling demo with static libraries: #

# make -f demo_rdbms.mk buildcp_static EXE=demoname OBJS=\

# e.g. make -f demo_rdbms.mk buildcp_static EXE=ocicp OBJS=ocicp.o #

# 11. A single OCI Session Pooling demo: #

# make -f demo_rdbms.mk # e.g. make -f demo_rdbms.mk ocisp # OR

# make -f demo_rdbms.mk buildcp EXE=demoname OBJS=\# e.g. make -f demo_rdbms.mk buildcp EXE=ocisp OBJS=ocisp.o #

# 12. A single OCI Session Pooling demo with static libraries: #

# make -f demo_rdbms.mk buildcp_static EXE=demoname OBJS=\

# e.g. make -f demo_rdbms.mk buildcp_static EXE=ocisp OBJS=ocisp.o #

# 13. A single OCI Statement Cache demo: #

# make -f demo_rdbms.mk # e.g. make -f demo_rdbms.mk ocisc # OR

# make -f demo_rdbms.mk buildcp EXE=demoname OBJS=\# e.g. make -f demo_rdbms.mk buildcp EXE=ocisc OBJS=ocisc.o #

# 14. A single OCI Statement Cache demo with static libraries: #

# make -f demo_rdbms.mk buildcp_static EXE=demoname OBJS=\

# e.g. make -f demo_rdbms.mk buildcp_static EXE=ocisc OBJS=ocisc.o #

# Example for building demo DIRECT PATH API programs: #

# 1. All DIRECT PATH API demos: #

# make -f demo_rdbms.mk demos_dp #

# 2. A single DIRECT PATH API demo: #

# make -f demo_rdbms.mk build_dp EXE=demo OBJS=\

# e.g. make -f demo_rdbms.mk build_dp EXE=cdemdplp OBJS=cdemdplp.o # #

# Example for building external procedures demo programs: #

# 1. All external procedure demos: #

# 2. A single external procedure demo whose 3GL routines do not use the # \#

# make -f demo_rdbms.mk extproc_no_context SHARED_LIBNAME=libname # OBJS=\# e.g. make -f demo_rdbms.mk extproc_no_context SHARED_LIBNAME=epdemo.so

# OBJS=\epdemo2.o\#

# 3. A single external procedure demo where one or more 3GL routines use the

# \#

# make -f demo_rdbms.mk extproc_with_context SHARED_LIBNAME=libname # OBJS=\# e.g. make -f demo_rdbms.mk extproc_with_context SHARED_LIBNAME=epdemo.so

# OBJS=\epdemo2.o\

# e.g. make -f demo_rdbms.mk extproc_with_context

# SHARED_LIBNAME=extdemo2.so OBJS=\# e.g. or For EXTDEMO2 DEMO ONLY: make -f demo_rdbms.mk demos #

# 4. To link C++ demos: #

# make -f demo_rdbms.mk c++demos # #

# NOTE: 1. ORACLE_HOME must be either:

# . set in the user's environment # . passed in on the command line

# . defined in a modified version of this makefile #

# 2. If the target platform support shared libraries (e.g. Solaris) # look in the platform specific documentation for information # about environment variables that need to be properly # defined (e.g. LD_LIBRARY_PATH in Solaris). #

include $(ORACLE_HOME)/rdbms/lib/env_rdbms.mk

# flag for linking with non-deferred option (default is deferred mode) NONDEFER=false

DEMO_DIR=$(ORACLE_HOME)/rdbms/demo

DEMO_MAKEFILE = $(DEMO_DIR)/demo_rdbms.mk

DEMOS = cdemo1 cdemo2 cdemo3 cdemo4 cdemo5 cdemo81 cdemo82 \\ cdemobj cdemolb cdemodsc cdemocor cdemolb2 cdemolbs \\ cdemodr1 cdemodr2 cdemodr3 cdemodsa obndra \\ cdemoext cdemothr cdemofil cdemofor \\

oci02 oci03 oci04 oci05 oci06 oci07 oci08 oci09 oci10 \\

oci11 oci12 oci13 oci14 oci15 oci16 oci17 oci18 oci19 oci20 \\ oci21 oci22 oci23 oci24 oci25 readpipe cdemosyev \\ ociaqdemo00 ociaqdemo01 ociaqdemo02 cdemoucb nchdemo1

DEMOS_DP = cdemdpco cdemdpin cdemdpit cdemdplp cdemdpno cdemdpro cdemdpss

C++DEMOS = cdemo6 OCCIDEMOS = occiblob occiclob occicoll occidesc occidml occipool occiproc \\

occistre

OCCIOTTDEMOS = occiobj occiinh occipobj # OTT Markers Support

OCCIOTTDEMOSWITHMARKER = mdemo1 OTTUSR = scott OTTPWD = tiger

CPDEMOS = ocicp ocicpproxy ocisp ocisc

.SUFFIXES: .o .cob .for .c .pc .cc .cpp

demos: $(DEMOS) extdemo2 extdemo4 extdemo5

demos_dp: $(DEMOS_DP)

generate_sharedlib:

$(SILENT)$(ECHO) \ $(SILENT)$(ECHO) \$$ORACLE_HOME/bin/genclntsh ...\ $(GENCLNTSH)

$(SILENT)$(ECHO) \$$ORACLE_HOME/lib/libclntsh.so... DONE\

BUILD=build $(DEMOS):

$(MAKE) -f $(DEMO_MAKEFILE) $(BUILD) EXE=$@ OBJS=$@.o

$(DEMOS_DP): cdemodp.c cdemodp0.h cdemodp.h

$(MAKE) -f $(DEMO_MAKEFILE) build_dp EXE=$@ OBJS=$@.o

c++demos: $(C++DEMOS)

$(C++DEMOS):

$(MAKE) -f $(DEMO_MAKEFILE) buildc++ EXE=$@ OBJS=$@.o

buildc++: $(OBJS)

$(MAKECPLPLDEMO)

occidemos: $(OCCIDEMOS) $(OCCIOTTDEMOS) $(OCCIOTTDEMOSWITHMARKER)

$(OCCIDEMOS):

$(MAKE) -f $(DEMO_MAKEFILE) buildocci EXE=$@ OBJS=$@.o

$(OCCIOTTDEMOS):

$(MAKE) -f $(DEMO_MAKEFILE) ott OTTFILE=$@

$(MAKE) -f $(DEMO_MAKEFILE) buildocci EXE=$@ OBJS=\$@m.o\

# OTT Markers Support

$(OCCIOTTDEMOSWITHMARKER):

$(MAKE) -f $(DEMO_MAKEFILE) ott_mrkr OTTFILE=$@

$(MAKE) -f $(DEMO_MAKEFILE) buildocci EXE=$@ OBJS=\$@m.o\

buildocci: $(OBJS)

$(MAKEOCCISHAREDDEMO)

buildocci_static: $(OBJS) $(MAKEOCCISTATICDEMO) ott:

$(ORACLE_HOME)/bin/ott \\

userid=$(OTTUSR)/$(OTTPWD) \\ intype=$(OTTFILE).typ \\

outtype=$(OTTFILE)out.type \\ code=cpp \\

hfile=$(OTTFILE).h \\

cppfile=$(OTTFILE)o.cpp \\ attraccess=private

# OTT Markers Suppport

ott_mrkr:

$(ORACLE_HOME)/bin/ott \\

userid=$(OTTUSR)/$(OTTPWD) \\ intype=$(OTTFILE).typ \\

outtype=$(OTTFILE)out.type \\ code=cpp \\

hfile=$(OTTFILE).h \\

cppfile=$(OTTFILE)o.cpp \\ use_marker=true

cpdemos: $(CPDEMOS) $(CPDEMOS):

$(MAKE) -f $(DEMO_MAKEFILE) buildcp EXE=$@ OBJS=$@.o buildcp: $(OBJS)

$(MAKECPSHAREDDEMO) buildcp_static: $(OBJS) $(MAKECPSTATICDEMO)

# Pro*C rules

# SQL Precompiler macros pc1:

$(PCC2C)

.pc.c:

$(MAKE) -f $(DEMO_MAKEFILE) PCCSRC=$* I_SYM=include= pc1

.pc.o:

$(MAKE) -f $(DEMO_MAKEFILE) PCCSRC=$* I_SYM=include= pc1 $(PCCC2O)

.cc.o:

$(CCC2O)

.cpp.o:

$(CCC2O)

build: $(LIBCLNTSH) $(OBJS) $(BUILDEXE)

extdemo2:

$(MAKE) -f $(DEMO_MAKEFILE) extproc_with_context SHARED_ LIBNAME=extdemo2.so OBJS=\

extdemo4:

$(MAKE) -f $(DEMO_MAKEFILE) extproc_with_context SHARED_ LIBNAME=extdemo4.so OBJS=\

extdemo5:

$(MAKE) -f $(DEMO_MAKEFILE) extproc_with_context SHARED_ LIBNAME=extdemo5.so OBJS=\

.c.o:

$(C2O)

build_dp: $(LIBCLNTSH) $(OBJS) cdemodp.o $(DPTARGET)

build_static: $(OBJS) $(O2STATIC)

# extproc_no_context and extproc_with_context are the current names of these

# targets. The old names, extproc_nocallback and extproc_callback are # preserved for backward compatibility.

extproc_no_context extproc_nocallback: $(OBJS) $(BUILDLIB_NO_CONTEXT)

extproc_with_context extproc_callback: $(OBJS) $(LIBCLNTSH) $(BUILDLIB_WITH_CONTEXT)

clean:

$(RM) -f $(DEMOS) $(CPDEMOS) extdemo2 extdemo4 extdemo5 *.o *.so $(RM) -f $(OCCIDEMOS) $(OCCIOTTDEMOS) occi*m.cpp occi*o.cpp occi*.typ

occiobj*.h occiinh*.h occipobj*.h

$(RM) -f $(OCCIOTTDEMOSWITHMARKER) mdemo*m.cpp mdemo*o.cpp mdemo*.typ mdemo*.h

$(RM) -f $(DEMOS_DP)

occiblob.cpp

The following code example demonstrates how to read and write aBLOB: #include #include

using namespace oracle::occi;

using namespace std; /**

* The demo sample has starts from startDemo method. This method is called

* by main. startDemo calls other methods, the supporting methods for * startDemo are,

* insertRows - insert the rows into the table * deleteRows - delete the rows inserted

* insertBlob - Inserts a blob and an empty_blob * populateBlob - populates a given blob

* dumpBlob - prints the blob as an integer stream */

class demoBlob {

private:

string username; string password; string url;

void insertRows (Connection *conn) throw (SQLException) {

Statement *stmt = conn->createStatement (\print_media(product_

id,ad_id,ad_composite,ad_sourcetext) VALUES (6666,11001,'10001','SHE')\ stmt->executeUpdate(); stmt->setSQL (\

print_media(product_id,ad_id,ad_composite,ad_ sourcetext) VALUES (7777,11001,'1010','HEM')\ stmt->executeUpdate(); conn->commit();

conn->terminateStatement (stmt); }

void deleteRows (Connection *conn) throw (SQLException) {

Statement *stmt = conn->createStatement (\product_

id = 6666 AND ad_id=11001\

stmt->executeUpdate();

stmt->setSQL (\ad_id=11001\

stmt->executeUpdate(); conn->commit();

conn->terminateStatement (stmt); }

/**

* populating the blob; */

void populateBlob (Blob &blob, int size) throw (SQLException) {

Stream *outstream = blob.getStream (1,0); char *buffer = new char[size];

memset (buffer, (char)10, size);

outstream->writeBuffer (buffer, size); char *c = (char *)\

outstream->writeLastBuffer (c,0); delete (buffer);

blob.closeStream (outstream); }

/**

* printing the blob data as integer stream */

void dumpBlob (Blob &blob, int size) throw (SQLException) {

Stream *instream = blob.getStream (1,0); char *buffer = new char[size]; memset (buffer, NULL, size);

instream->readBuffer (buffer, size); cout << \

for (int i = 0; i < size; ++i) cout << (int) buffer[i]; cout << endl;

delete (buffer);

blob.closeStream (instream);

}

/**

* public methods */

public:

demoBlob () {

/**

* default values of username & password */

username = \ password = \ url = \ }

void setUsername (string u) {

username = u; }

void setPassword (string p) {

password = p; }

void setUrl (string u) {

url = u; }

void runSample () throw (SQLException) {

Environment *env = Environment::createEnvironment ( Environment::DEFAULT); try {

Connection *conn = env->createConnection (username, password, url);

Statement *stmt1; insertRows (conn);

/**

* Reading a populated blob & printing its property. */

string sqlQuery = \product_ id=6666\

Statement *stmt = conn->createStatement (sqlQuery);

ResultSet *rset = stmt->executeQuery (); while (rset->next ()) {

Blob blob = rset->getBlob (1);

cout << \ blob.open (OCCI_LOB_READONLY); int blobLength=blob.length ();

cout << \ dumpBlob (blob, blobLength); blob.close (); }

stmt->closeResultSet (rset);

/**

* Reading a populated blob & printing its property. */

stmt->setSQL (\product_id =7777 FOR UPDATE\

rset = stmt->executeQuery (); while (rset->next ()) {

Blob blob = rset->getBlob (1);

cout << \ blob.open (OCCI_LOB_READWRITE);

cout << \ populateBlob (blob, 20);

int blobLength=blob.length ();

cout << \ dumpBlob (blob, blobLength); blob.close (); }

stmt->closeResultSet (rset); deleteRows (conn);

conn->terminateStatement (stmt); env->terminateConnection (conn);

}

catch (SQLException ea) {

cout << ea.what(); }

Environment::terminateEnvironment (env); }

};//end of class demoBlob

int main (void) {

demoBlob *b = new demoBlob (); b->setUsername (\ b->setPassword (\ b->runSample (); }

occiclob.cpp

The following code example demonstrates how to read and write a CLOB: #include #include

using namespace oracle::occi; using namespace std; /**

* The demo sample has starts from startDemo method. This method is called

* by main. startDemo calls other methods, the supporting methods for * startDemo are,

* insertRows - inserts the rows into the table1 * deleteRows - delete the rows inserted

* insertClob - Inserts a clob and an empty_clob * populateClob - populates a given clob

* dumpClob - prints the clob as an integer stream */

class demoClob {

private:

string username;

string password; string url;

void insertRows (Connection *conn) throw (SQLException) {

Statement *stmt = conn->createStatement (\print_media(product_

id,ad_id,ad_composite,ad_sourcetext) VALUES (3333,11001,'10001','SHE')\ stmt->executeUpdate(); stmt->setSQL (\

print_media(product_id,ad_id,ad_composite,ad_ sourcetext) VALUES (4444,11001,'1010','HEM')\ stmt->executeUpdate(); conn->commit();

conn->terminateStatement (stmt); }

void deleteRows (Connection *conn) throw (SQLException) {

Statement *stmt = conn->createStatement (\product_

id=3333 AND ad_id=11001\ stmt->executeUpdate();

stmt->setSQL(\ad_id=11001\

stmt->executeUpdate(); conn->commit();

conn->terminateStatement (stmt); }

/**

* populating the clob; */

void populateClob (Clob &clob, int size) throw (SQLException) {

Stream *outstream = clob.getStream (1,0); char *buffer = new char[size];

memset (buffer,'H', size);

outstream->writeBuffer (buffer, size); char *c = (char *)\

outstream->writeLastBuffer (c,0); delete (buffer);

clob.closeStream (outstream); }

/**

* printing the clob data as integer stream */

void dumpClob (Clob &clob, int size) throw (SQLException) {

Stream *instream = clob.getStream (1,0); char *buffer = new char[size]; memset (buffer, NULL, size);

instream->readBuffer (buffer, size); cout << \

for (int i = 0; i < size; ++i) cout << (char) buffer[i]; cout << endl;

delete (buffer);

clob.closeStream (instream); }

/**

* public methods */

public:

demoClob () {

/**

* default values of username & password */

username = \ password = \ url = \ }

void setUsername (string u) {

username = u; }

void setPassword (string p) {

password = p; }

void setUrl (string u) {

url = u; }

void runSample () throw (SQLException) {

Environment *env = Environment::createEnvironment ( Environment::DEFAULT); try {

Connection *conn = env->createConnection (username, password, url);

Statement *stmt1; insertRows (conn); /**

* Reading a populated clob & printing its property. */

string sqlQuery = \product_ id=3333\

Statement *stmt = conn->createStatement (sqlQuery);

ResultSet *rset = stmt->executeQuery (); while (rset->next ()) {

Clob clob = rset->getClob (1);

cout << \ clob.open (OCCI_LOB_READONLY); int clobLength=clob.length ();

cout << \ dumpClob (clob, clobLength); clob.close ();

}

stmt->closeResultSet (rset);

/**

* Reading a populated clob & printing its property. */

stmt->setSQL (\product_id

=4444 FOR UPDATE\

rset = stmt->executeQuery (); while (rset->next ()) {

Clob clob = rset->getClob (1);

cout << \ clob.open (OCCI_LOB_READWRITE);

cout << \ populateClob (clob, 20);

int clobLength=clob.length ();

cout << \ dumpClob (clob, clobLength); clob.close (); }

stmt->closeResultSet (rset); conn->terminateStatement (stmt); deleteRows(conn);

env->terminateConnection (conn); }

catch (SQLException ea) {

cout << ea.what(); }

Environment::terminateEnvironment (env); }

};//end of class demoClob

int main (void) {

demoClob *b = new demoClob (); b->setUsername (\ b->setPassword (\ b->runSample (); }

occicoll.cpp

The following code example demonstrates how to perform simple insert, delete, and update operations on a table column of type Nested Table: /**

*occicoll - To exhibit simple insert, delete & update operations\* \*

*Description

* Create a program which has insert,delete and update on a * table having a Nested table column.

* Perform all these operations using OCCI interface. */

#include #include

using namespace oracle::occi; using namespace std;

typedef vector journal;

class occicoll {

private:

Environment *env; Connection *conn; Statement *stmt; string tableName; string typeName;

public:

occicoll (string user, string passwd, string db) {

env = Environment::createEnvironment (Environment::OBJECT); conn = env->createConnection (user, passwd, db); initRows(); }

~occicoll () {

env->terminateConnection (conn);

Environment::terminateEnvironment (env); }

void setTableName (string s) {

tableName = s; }

void initRows () {

try{

Statement *st1 = conn->createStatement (\FROM journal_tab\ st1->executeUpdate ();

st1->setSQL(\journal

('NATION', 'TIMES'))\ st1->executeUpdate ();

st1->setSQL(\journal

('CRICKET', 'ALIVE'))\ st1->executeUpdate (); conn->commit();

conn->terminateStatement (stmt); }catch(SQLException ex) {

cout<

* Insertion of a row */

void insertRow () {

int c1 = 11; journal c2;

c2.push_back (\ c2.push_back (\ c2.push_back (\

cout << \

\ try{

stmt = conn->createStatement (

\ stmt->setInt (1, c1);

setVector (stmt, 2, c2, \ stmt->executeUpdate (); }catch(SQLException ex) {

cout<<\

cout<<\ cout<

cout << \ conn->terminateStatement (stmt); }

// Displaying all the rows of the table void displayAllRows () {

cout << \ stmt = conn->createStatement (

\

journal c2;

ResultSet *rs = stmt->executeQuery(); try{

while (rs->next()) {

cout << \ cout << \ getVector (rs, 2, c2);

for (int i = 0; i < c2.size(); ++i) cout << c2[i] << \ cout << endl; }

}catch(SQLException ex) {

cout<<\

cout<<\ cout<

stmt->closeResultSet (rs);

conn->terminateStatement (stmt);

} // End of displayAllRows()

// Deleting a row in a nested table void deleteRow (int c1, string str) {

cout << \ stmt = conn->createStatement (

\ journal c2;

stmt->setInt (1, c1);

ResultSet *rs = stmt->executeQuery(); try{

if (rs->next()) {

getVector (rs, 1, c2);

c2.erase (find (c2.begin(), c2.end(), str)); }

stmt->setSQL (\ stmt->setInt (2, c1);

setVector (stmt, 1, c2, \ stmt->executeUpdate (); }catch(SQLException ex) {

cout<<\

cout<<\ cout<

cout << \ conn->commit();

stmt->closeResultSet (rs);

conn->terminateStatement (stmt); } // End of deleteRow (int, string)

// Updating a row of the nested table of strings void updateRow (int c1, string str) {

cout << \ stmt = conn->createStatement (

\

journal c2;

stmt->setInt (1, c1);

ResultSet *rs = stmt->executeQuery(); try{

if (rs->next()) {

getVector (rs, 1, c2); c2[0] = str; }

stmt->setSQL (\ stmt->setInt (2, c1);

setVector (stmt, 1, c2, \ stmt->executeUpdate (); }catch(SQLException ex) {

cout<<\

cout<<\ cout<

cout << \ conn->commit();

stmt->closeResultSet (rs);

conn->terminateStatement (stmt); } // End of UpdateRow (int, string)

};//end of class occicoll

int main (void) {

string user = \ string passwd = \ string db = \

try {

cout << \operations\

\ occicoll *demo = new occicoll (user, passwd, db);

cout << \ demo->displayAllRows ();

demo->insertRow ();

demo->deleteRow (11, \

demo->updateRow (33, \

cout << \ demo->displayAllRows ();

delete (demo);

cout << \ }catch (SQLException ea) {

cerr << \ } }

occidesc.cpp

The following code example demonstrates how to obtain metadata about a table, procedure, and object:

/**

* occidesc - Describing the various objects of the database. *

* DESCRIPTION :

* This program describes the objects of the database, like, table, object

* and procedure. * */

#include #include

using namespace oracle::occi; using namespace std;

class occidesc {

private:

Environment *env; Connection *conn; public :

/**

* Constructor for the occidesc demo program. */

occidesc (string user, string passwd, string db) throw (SQLException) {

env = Environment::createEnvironment (Environment::OBJECT); conn = env->createConnection (user, passwd, db);

}// end of constructor occidesc (string, string, string ) /**

* Destructor for the occidesc demo program. */

~occidesc () throw (SQLException) {

env->terminateConnection (conn);

Environment::terminateEnvironment (env); } // end of ~occidesc ()

// Describing a subtype void describe_type() {

cout << \

MetaData metaData = conn->getMetaData ((char *)\ int mdTyp = metaData.getInt(MetaData::ATTR_PTYPE); if (mdTyp == MetaData::PTYPE_TYPE) {

cout << \ }

int typcode = metaData.getInt(MetaData::ATTR_TYPECODE); if (typcode == OCCI_TYPECODE_OBJECT)

cout << \ else

cout << \

int numtypeattrs = metaData.getInt(MetaData::ATTR_NUM_TYPE_ATTRS); cout << \ try {

cout << \id: \<< metaData.getUInt (MetaData::ATTR_OBJ_ID) << endl; }

catch (SQLException ex) {

cout << ex.getMessage() << endl; }

cout << \

metaData.getString (MetaData::ATTR_OBJ_NAME) << endl; cout << \

(metaData.getString(MetaData::ATTR_OBJ_SCHEMA)) << endl; cout << \

(metaData.getString(MetaData::ATTR_VERSION)) << endl; if (metaData.getBoolean(MetaData::ATTR_IS_INCOMPLETE_TYPE)) cout << \ else

cout << \

if (metaData.getBoolean(MetaData::ATTR_IS_SYSTEM_TYPE)) cout << \ else

cout << \

if (metaData.getBoolean(MetaData::ATTR_IS_PREDEFINED_TYPE)) cout << \ else

cout << \

if (metaData.getBoolean(MetaData::ATTR_IS_TRANSIENT_TYPE)) cout << \ else

cout << \

if (metaData.getBoolean(MetaData::ATTR_IS_SYSTEM_GENERATED_TYPE)) cout << \ else

cout << \

if (metaData.getBoolean(MetaData::ATTR_HAS_NESTED_TABLE)) cout << \ else

cout << \ if (metaData.getBoolean(MetaData::ATTR_HAS_LOB)) cout << \ else

cout << \

if (metaData.getBoolean(MetaData::ATTR_HAS_FILE)) cout << \ else

cout << \

if (metaData.getBoolean(MetaData::ATTR_IS_INVOKER_RIGHTS)) cout << \ else

cout << \

RefAny ref = metaData.getRef (MetaData::ATTR_REF_TDO); MetaData md1 = conn->getMetaData (ref);

vector v1 =

md1.getVector (MetaData::ATTR_LIST_TYPE_ATTRS);

for (int i = 0; i < v1.size (); ++i) {

MetaData md2 = (MetaData)v1[i]; cout << \

(md2.getString(MetaData::ATTR_NAME)) << endl; cout << \

(printType (md2.getInt(MetaData::ATTR_DATA_TYPE))) << endl; cout << \Size :\<< md2.getInt(MetaData::ATTR_DATA_SIZE) << endl; cout << \Precision :\<< md2.getInt(MetaData::ATTR_PRECISION) << endl;

cout << \endl; }

cout << \ } // end of describe_type()

// Describing a table void describe_table () {

cout << \ vector v1;

MetaData metaData = conn->getMetaData(\ cout << \

(metaData.getString(MetaData::ATTR_OBJ_NAME)) << endl; cout << \

(metaData.getString(MetaData::ATTR_OBJ_SCHEMA)) << endl; if (metaData.getInt(MetaData::ATTR_PTYPE) == MetaData::PTYPE_TABLE) {

cout << \ } else

cout << \

if (metaData.getBoolean(MetaData::ATTR_PARTITIONED)) cout << \ else

cout << \

if (metaData.getBoolean(MetaData::ATTR_IS_TEMPORARY)) cout << \ else

cout << \

if (metaData.getBoolean(MetaData::ATTR_IS_TYPED)) cout << \ else

cout << \

if (metaData.getBoolean(MetaData::ATTR_CLUSTERED)) cout << \ else

cout << \

if (metaData.getBoolean(MetaData::ATTR_INDEX_ONLY)) cout << \ else

cout << \ cout << \

switch (metaData.getInt(MetaData::ATTR_DURATION)) {

case MetaData::DURATION_SESSION : cout << \ break;

case MetaData::DURATION_TRANS : cout << \ break;

case MetaData::DURATION_NULL : cout << \endl;

break; } try {

cout << \

metaData.getUInt (MetaData::ATTR_RDBA) << endl; }

catch (SQLException ex) {

cout << ex.getMessage() << endl; } try {

cout << \

metaData.getInt (MetaData::ATTR_TABLESPACE) << endl; }

catch (SQLException ex) {

cout << ex.getMessage() << endl; } try {

cout << \

metaData.getUInt(MetaData::ATTR_OBJID) << endl; }

catch (SQLException ex) {

cout << ex.getMessage() << endl; }

int columnCount = metaData.getInt(MetaData::ATTR_NUM_COLS); cout << \

v1 = metaData.getVector(MetaData::ATTR_LIST_COLUMNS); for(int i=0; i < v1.size(); i++) {

MetaData md = v1[i];

cout << \

(md.getString(MetaData::ATTR_NAME)) << endl; cout << \

(printType (md.getInt(MetaData::ATTR_DATA_TYPE))) << endl; cout << \ cout << \endl;

cout << \ bool isnull = md.getBoolean(MetaData::ATTR_IS_NULL); if (isnull)

cout << \ else

cout << \ }

cout << \ } // end of describe_table ()

// Describing a procedure void describe_proc () {

cout << \ MetaData metaData = conn->getMetaData(\ vector v1 =

metaData.getVector ( MetaData::ATTR_LIST_ARGUMENTS );

cout << \ cout << \

(metaData.getString(MetaData::ATTR_OBJ_NAME)) << endl; cout << \

(metaData.getString(MetaData::ATTR_OBJ_SCHEMA)) << endl;

if (metaData.getInt(MetaData::ATTR_PTYPE) == MetaData:: PTYPE_PROC) {

cout << \ } else {

if (metaData.getInt(MetaData::ATTR_PTYPE) == MetaData:: PTYPE_FUNC) {

cout << \ } } try {

cout << \

metaData.getUInt(MetaData::ATTR_OBJ_ID) << endl; }

catch (SQLException ex) {

cout << ex.getMessage() << endl; } try {

cout << \

(metaData.getString(MetaData::ATTR_NAME)) << endl; }

catch (SQLException ex) {

cout << ex.getMessage() << endl; }

if (metaData.getBoolean(MetaData::ATTR_IS_INVOKER_RIGHTS)) cout << \ else

cout << \ cout << \

metaData.getInt(MetaData::ATTR_OVERLOAD_ID) << endl;

for(int i=0; i < v1.size(); i++) {

MetaData md = v1[i];

cout << \

(md.getString(MetaData::ATTR_NAME)) << endl;

cout << \

(printType (md.getInt(MetaData::ATTR_DATA_TYPE))) << endl;

cout << \

int mode = md.getInt (MetaData::ATTR_IOMODE); if (mode == 0)

cout << \ if (mode == 1)

cout << \ if (mode == 2)

cout << \ cout << \

md.getInt(MetaData::ATTR_DATA_SIZE) << endl; cout << \

md.getInt(MetaData::ATTR_PRECISION) << endl; cout << \

md.getInt(MetaData::ATTR_SCALE) << endl;

int isNull = md.getInt ( MetaData::ATTR_IS_NULL); if (isNull != 0)

cout << \ else

cout << \

int hasDef = md.getInt ( MetaData::ATTR_HAS_DEFAULT); if (hasDef != 0)

cout << \ else

cout << \ }

cout << \ }

// Method which prints the data type string printType (int type) {

switch (type) {

case OCCI_SQLT_CHR : return \ break;

case OCCI_SQLT_NUM : return \ break;

case OCCIINT : return \ break;

case OCCIFLOAT : return \

break;

case OCCI_SQLT_STR : return \ break;

case OCCI_SQLT_VNU : return \ break;

case OCCI_SQLT_LNG : return \ break;

case OCCI_SQLT_VCS : return \ break;

case OCCI_SQLT_RID : return \ break;

case OCCI_SQLT_DAT : return \ break;

case OCCI_SQLT_VBI : return \ break;

case OCCI_SQLT_BIN : return \ break;

case OCCI_SQLT_LBI : return \ break;

case OCCIUNSIGNED_INT : return \ break;

case OCCI_SQLT_LVC : return \ break;

case OCCI_SQLT_LVB : return \ break;

case OCCI_SQLT_AFC : return \ break;

case OCCI_SQLT_AVC : return \ break;

case OCCI_SQLT_RDD : return \ break;

case OCCI_SQLT_NTY : return \ break;

case OCCI_SQLT_REF : return \ break;

case OCCI_SQLT_CLOB: return \ break;

case OCCI_SQLT_BLOB: return \ break;

case OCCI_SQLT_FILE: return \ break; }

} // End of printType (int)

}; // end of class occidesc

int main (void) {

string user = \ string passwd = \ string db = \

cout << \- Describing the various objects of the database\<< endl;

occidesc *demo = new occidesc (user, passwd, db); demo->describe_table(); demo->describe_type(); demo->describe_proc(); delete demo;

}// end of main ()

occidml.cpp

The following code example demonstrates how to perform insert, select, update, and delete operations of a table row by using OCCI:

/**

* occidml - To exhibit the insertion, Selection, updating and deletion of

* a row through OCCI. *

* Description

* Create a program which has insert, select, update & delete as operations.

* Perform all these operations using OCCI interface. */

#include #include

using namespace oracle::occi; using namespace std;

class occidml {

private:

Environment *env; Connection *conn; Statement *stmt; public:

occidml (string user, string passwd, string db) {

env = Environment::createEnvironment (Environment::DEFAULT); conn = env->createConnection (user, passwd, db); }

~occidml () {

env->terminateConnection (conn);

Environment::terminateEnvironment (env); }

/**

* Insertion of a row with dynamic binding, PreparedStatement functionality. */

void insertBind (int c1, string c2) {

string sqlStmt = \ stmt=conn->createStatement (sqlStmt); try{

stmt->setInt (1, c1); stmt->setString (2, c2); stmt->executeUpdate ();

cout << \ }catch(SQLException ex) {

cout<<\

cout<<\ cout<

conn->terminateStatement (stmt); }

/**

* Inserting a row into the table. */

void insertRow () {

string sqlStmt = \ stmt = conn->createStatement (sqlStmt); try{

stmt->executeUpdate ();

cout << \ }catch(SQLException ex) {

cout<<\

cout<<\ cout<

conn->terminateStatement (stmt); }

/**

* updating a row */

void updateRow (int c1, string c2) {

string sqlStmt =

\ stmt = conn->createStatement (sqlStmt); try{

stmt->setString (1, c2); stmt->setInt (2, c1); stmt->executeUpdate ();

cout << \ }catch(SQLException ex) {

cout<<\

cout<<\ cout<

conn->terminateStatement (stmt); }

/**

* deletion of a row */

void deleteRow (int c1, string c2) {

string sqlStmt =

\FROM author_tab WHERE author_id= :x AND author_name = :y\ stmt = conn->createStatement (sqlStmt); try{

stmt->setInt (1, c1); stmt->setString (2, c2); stmt->executeUpdate ();

cout << \ }catch(SQLException ex) {

cout<<\

cout<<\ cout<

conn->terminateStatement (stmt); }

/**

* displaying all the rows in the table */

void displayAllRows () {

string sqlStmt = \ stmt = conn->createStatement (sqlStmt); ResultSet *rset = stmt->executeQuery (); try{

while (rset->next ()) {

cout << \ << rset->getString (2) << endl; }

}catch(SQLException ex) {

cout<<\ cout<<\ cout<

stmt->closeResultSet (rset); conn->terminateStatement (stmt); }

}; // end of class occidml

int main (void) {

string user = \ string passwd = \ string db = \

cout << \- Exhibiting simple insert, delete & update operations\ << endl;

occidml *demo = new occidml (user, passwd, db);

cout << \ demo->displayAllRows ();

cout << \ << endl;

demo->insertRow ();

cout << \ demo->displayAllRows ();

cout << \bind\

<< endl;

demo->insertBind (222, \

cout << \endl;

demo->displayAllRows ();

cout << \<< endl;

demo->deleteRow (222, \

cout << \<< endl;

demo->updateRow (444, \

cout << \ demo->displayAllRows ();

delete (demo);

cout << \}

occiinh.typ

CASE=LOWER

MAPFILE=occiinhm.cpp

TYPE FOREIGN_STUDENT as foreign_student

occiinh.cpp

The following code example demonstrates object inheritance by using insert, select, update, and delete operations on a table row of subtype table:

/**

* occiinh.cpp - To exhibit the insertion, selection, updating and deletion

* of a row of a table of derived object. *

* Description

* Create a program which has insert, select, update & delete as operations

* of a object. Perform all these operations using OCCI interface. * Hierarchy

* person_typ <---- student <----- parttime_stud <----- foreign_student **/

#include #include

using namespace oracle::occi; using namespace std;

#include \

/* Add on your methods in this class*/

class foreign_student_obj : public foreign_student {

/* New methods can be added here */ };

class occiinh {

private:

Environment *env; Connection *con;

// This method will return the Ref RefAny getRefObj(string sqlString) {

Statement *stmt = con->createStatement (sqlString); ResultSet *rs; try {

rs = stmt->executeQuery (); if ( rs->next() ) {

RefAny ref1 = rs->getRef (1); stmt->closeResultSet (rs);

con->terminateStatement (stmt); return ref1; } }

catch(SQLException ex) {

cout << \ }

stmt->closeResultSet (rs);

con->terminateStatement (stmt); }

public:

occiinh (string user, string passwd, string db) throw (SQLException) {

env = Environment::createEnvironment (Environment::OBJECT); occiinhm(env);

con = env->createConnection (user, passwd, db);

}// end of constructor occiinh (string, string, string)

~occiinh ()

throw (SQLException)

{

env->terminateConnection (con);

Environment::terminateEnvironment (env); }// end of destructor

/**

* Insertion of a row */

void insertRow () throw (SQLException) {

cout << \ string sqlStmt =

\ Statement *stmt = con->createStatement (sqlStmt); string fs_name = \ Number fs_ssn (4);

Date fs_dob(env, 2000, 5, 11, 16, 05, 0); Number fs_stud_id (400);

Ref< person_typ > fs_teammate = getRefObj(

\ Number fs_course_id(4000);

Ref< student > fs_partner = getRefObj( \ string fs_country = \

Ref< parttime_stud > fs_leader = getRefObj( \ foreign_student_obj fsobj;

foreign_student_obj *fs_obj=&fsobj; fs_obj->setname(fs_name); fs_obj->setssn(fs_ssn); fs_obj->setdob(fs_dob);

fs_obj->setstud_id(fs_stud_id); fs_obj->setteammate(fs_teammate); fs_obj->setcourse_id(fs_course_id); fs_obj->setpartner(fs_partner); fs_obj->setcountry(fs_country); fs_obj->setleader(fs_leader); stmt->setObject(1, fs_obj); stmt->executeUpdate();

con->terminateStatement (stmt);

cout << \ }// end of insertRow ();

/**

* updating a row */

void updateRow () throw (SQLException) {

cout << \record (Changing name,teammate and course_id)\<< endl;

string sqlStmt =

\course_id=:z\

Statement *stmt = con->createStatement (sqlStmt); string fs_name = \

Ref< person_typ > fs_teammate = getRefObj(

\ Number fs_course_id(5000); stmt->setString(1, fs_name); stmt->setRef(2,fs_teammate); stmt->setInt(3, fs_course_id); stmt->executeUpdate (); con->commit();

con->terminateStatement (stmt);

cout << \ }// end of updateRow (int, string);

/**

* deletion of a row */

void deleteRow () throw (SQLException) {

cout << \

string sqlStmt = \ Statement *stmt = con->createStatement (sqlStmt); string fs_name = \ stmt->setString(1,fs_name); stmt->executeUpdate(); con->commit();

con->terminateStatement (stmt);

cout << \ }// end of deleteRow (int, string);

/**

* displaying all the rows in the table */

void displayAllRows () throw (SQLException) {

int count=0;

string sqlStmt = \ Statement *stmt = con->createStatement (sqlStmt); ResultSet *resultSet = stmt->executeQuery ();

while (resultSet->next ()) {

count++;

RefAny fs_refany = resultSet->getRef(1);

Ref fs_ref(fs_refany); fs_ref.setPrefetch(4);

string fmt = \

string nlsParam = \ Date fs_dob = fs_ref->getdob();

string date1 = fs_dob.toText (fmt, nlsParam); cout << \ cout << \

cout << \ cout << \

cout << \ cout << \ cout << \ Ref fs_teammate = (Ref ) fs_ref->getteammate();

cout << \

cout << \

cout << \ fs_dob = fs_teammate->getdob();

date1 = fs_dob.toText(fmt, nlsParam);

cout << \ /* Leader */

Ref< parttime_stud > fs_leader = (Ref < parttime_stud >) fs_ref->getleader(); /* Leader's Partner */

Ref < student > fs_partner = (Ref ) fs_leader->getpartner();

/* Leader's Partenr's teammate */

fs_teammate = (Ref ) fs_partner->getteammate();

cout << \

cout << \ cout << \ fs_dob = fs_leader->getdob();

date1 = fs_dob.toText(fmt, nlsParam);

cout << \

cout << \

cout << \endl;

cout << \ cout << \ cout << \ fs_dob = fs_partner->getdob();

date1 = fs_dob.toText(fmt, nlsParam); cout << \ cout << \id : \<< (int)fs_partner->getstud_id() << endl;

cout << \ cout << \ cout << \ fs_dob = fs_teammate->getdob();

date1 = fs_dob.toText(fmt, nlsParam);

cout << \

}//end of while (resultSet->next ()); if (count <=0)

cout << \ stmt->closeResultSet (resultSet); con->terminateStatement (stmt); }// end of updateRow (string);

}; // end of class occiinh

int main (void) {

string user = \ string passwd = \ string db = \

try {

cout << \operations\

\

occiinh *demo = new occiinh (user, passwd, db);

cout << \ demo->displayAllRows ();

demo->insertRow ();

cout << \ demo->displayAllRows ();

demo->updateRow ();

cout << \ demo->displayAllRows ();

demo->deleteRow ();

cout << \ demo->displayAllRows ();

delete (demo);

cout << \ }catch (SQLException ea) {

cerr << \ }

}// end of int main (void);

occiobj.typ

CASE=SAME

MAPFILE=occiobjm.cpp TYPE address as address

occiobj.cpp

The following code example demonstrates how to perform insert, select, update, and delete operations on a table row containing an object as one of its columns:

/**

* occiobj.cpp - To exhibit the insertion, selection, updating and deletion

* of a row containing object as one of the column. *

* Description

* Create a program which has insert, select, update & delete as operations

* of a object. Perform all these operations using OCCI interface. */

#include #include

using namespace oracle::occi; using namespace std;

#include \

class occiobj {

private:

Environment *env; Connection *con; Statement *stmt; public:

occiobj (string user, string passwd, string db) {

env = Environment::createEnvironment (Environment::OBJECT); occiobjm (env);

con = env->createConnection (user, passwd, db); }

~occiobj () {

env->terminateConnection (con);

Environment::terminateEnvironment (env); }

/**

* Insertion of a row */

void insertRow (int c1, int a1, string a2)

{

cout << \ \

string sqlStmt = \ try{

stmt = con->createStatement (sqlStmt); stmt->setInt (1, c1);

address *o = new address (); o->setStreet_no (Number ( a1)); o->setCity (a2);

stmt->setObject (2, o); stmt->executeUpdate ();

cout << \ delete (o);

}catch(SQLException ex) {

cout<<\

cout<<\ cout<

con->terminateStatement (stmt); }

/**

* updating a row */

void updateRow (int c1, int a1, string a2) {

cout << \ string sqlStmt =

\= :y\ try{

stmt = con->createStatement (sqlStmt); address *o = new address (); o->setStreet_no (Number ( a1)); o->setCity (a2);

stmt->setObject (1, o); stmt->setInt (2, c1); stmt->executeUpdate ();

cout << \ delete (o);

}catch(SQLException ex) {

cout<<\

cout<<\ cout<

con->terminateStatement (stmt); }

/**

* deletion of a row */

void deleteRow (int c1, int a1, string a2) {

cout << \ string sqlStmt =

\FROM publisher_tab WHERE publisher_id= :x AND publisher_add = :y\ try{

stmt = con->createStatement (sqlStmt); stmt->setInt (1, c1);

address *o = new address (); o->setStreet_no (Number ( a1)); o->setCity (a2);

stmt->setObject (2, o); stmt->executeUpdate ();

cout << \ delete (o);

}catch(SQLException ex) {

cout<<\

cout<<\ cout<

con->terminateStatement (stmt); }

/**

* displaying all the rows in the table */

void displayAllRows ()

{

string sqlStmt = \publisher_tab\ try{

stmt = con->createStatement (sqlStmt); ResultSet *rset = stmt->executeQuery ();

while (rset->next ()) {

cout << \ << \

address *o = (address *)rset->getObject (2);

cout << (int)o->getStreet_no () << \\() << \<< endl; }

stmt->closeResultSet (rset); }catch(SQLException ex) {

cout<<\ cout<<\ cout<

con->terminateStatement (stmt); }

};//end of class occiobj;

int main (void) {

string user = \ string passwd = \ string db = \

try {

cout << \operations\

\

occiobj *demo = new occiobj (user, passwd, db);

cout << \

demo->displayAllRows ();

demo->insertRow (12, 122, \

demo->deleteRow (11, 121, \

demo->updateRow (23, 123, \

cout << \ demo->displayAllRows ();

delete (demo);

cout << \ }catch (SQLException ea) {

cerr << \ } }

occipobj.typ

CASE=SAME

MAPFILE=occipobjm.cpp TYPE address as address

occipobj.cpp

The following code example demonstrates how to perform insert, select, and update operations on persistent objects, as well as how to pin, unpin, mark for deletion, and flush a persistent object:

/**

* occipobj.cpp - Manipulation (Insertion, selection & updating) of * persistant objects, along with pinning, unpinning, marking

* for deletion & flushing. *

* Description

* Create a program which has insert, select, update & delete as operations

* of a persistant object. Along with the these the operations on Ref. are

* pinning, unpinning, marked for deletion & flushing.

* */

#include #include

using namespace oracle::occi; using namespace std;

#include \

class address_obj : public address {

public:

address_obj() { }

address_obj(Number sno,string cty) {

setStreet_no(sno); setCity(cty); } };

class occipobj {

private:

Environment *env; Connection *conn; Statement *stmt; string tableName; string typeName;

public:

occipobj (string user, string passwd, string db) {

env = Environment::createEnvironment (Environment::OBJECT); occipobjm (env);

conn = env->createConnection (user, passwd, db); }

~occipobj ()

本文来源:https://www.bwwdw.com/article/azfa.html

Top