blob: 499595a9ba16ba6d40a031f41592afcb2941b047 [file] [log] [blame]
Tang Cheng03c0b0a2015-01-12 11:19:45 +08001/* Copyright (c) 2001, 2006, Oracle. All rights reserved. */
2/*
3 NAME
4 occiobj.cpp - OCCI Embedded Object demo
5
6 DESCRIPTION
7 This demo performs all DML operations using OCCI interface
8 on embedded object column of table
9
10
11 MODIFIED (MM/DD/YY)
12 sudsrini 10/22/06 - Username/Password lower case
13 lburgess 04/14/06 - lowercase passwords
14 sudsrini 07/23/04 - Copyright Info
15 idcqe 03/05/01 - Creation
16
17*/
18
19#include <iostream>
20#include "occiobjm.h"
21
22using namespace oracle::occi;
23using namespace std;
24
25class occiobj
26{
27 private:
28
29 Environment *env;
30 Connection *con;
31 Statement *stmt;
32 public:
33
34 occiobj (string user, string passwd, string db)
35 {
36 env = Environment::createEnvironment (Environment::OBJECT);
37 occiobjm (env);
38 con = env->createConnection (user, passwd, db);
39 }
40
41 ~occiobj ()
42 {
43 env->terminateConnection (con);
44 Environment::terminateEnvironment (env);
45 }
46
47 /**
48 * Insertion of a row
49 */
50 void insertRow (int c1, int a1, string a2)
51 {
52 cout << "Inserting record - Publisher id :" << c1 <<
53 ", Publisher address :" << a1 << ", " << a2 <<endl;
54 string sqlStmt = "INSERT INTO publisher_tab VALUES (:x, :y)";
55 try{
56 stmt = con->createStatement (sqlStmt);
57 stmt->setInt (1, c1);
58 address *o = new address ();
59 o->setStreet_no (Number (a1));
60 o->setCity (a2);
61 stmt->setObject (2, o);
62 stmt->executeUpdate ();
63 cout << "Insert - Success" << endl;
64 delete (o);
65 }catch(SQLException ex)
66 {
67 cout<<"Exception thrown for insertRow"<<endl;
68 cout<<"Error number: "<< ex.getErrorCode() << endl;
69 cout<<ex.getMessage() << endl;
70 }
71
72 con->terminateStatement (stmt);
73 }
74
75
76 /**
77 * updating a row
78 */
79 void updateRow (int c1, int a1, string a2)
80 {
81 cout << "Upadating record with publisher id :"<< c1 << endl;
82 string sqlStmt =
83 "UPDATE publisher_tab SET publisher_add= :x WHERE publisher_id = :y";
84 try{
85 stmt = con->createStatement (sqlStmt);
86 address *o = new address ();
87 o->setStreet_no (Number (a1));
88 o->setCity (a2);
89 stmt->setObject (1, o);
90 stmt->setInt (2, c1);
91 stmt->executeUpdate ();
92 cout << "Update - Success" << endl;
93 delete (o);
94 }catch(SQLException ex)
95 {
96 cout<<"Exception thrown for updateRow"<<endl;
97 cout<<"Error number: "<< ex.getErrorCode() << endl;
98 cout<<ex.getMessage() << endl;
99 }
100 con->terminateStatement (stmt);
101 }
102
103
104 /**
105 * deletion of a row
106 */
107 void deleteRow (int c1, int a1, string a2)
108 {
109 cout << "Deletion of record where publisher id :" << c1 <<endl;
110 string sqlStmt =
111 "DELETE FROM publisher_tab WHERE publisher_id= :x AND publisher_add = :y";
112 try{
113 stmt = con->createStatement (sqlStmt);
114 stmt->setInt (1, c1);
115
116 address *o = new address ();
117 o->setStreet_no (Number (a1));
118 o->setCity (a2);
119 stmt->setObject (2, o);
120 stmt->executeUpdate ();
121 cout << "Delete - Success" << endl;
122 delete (o);
123 }catch(SQLException ex)
124 {
125 cout<<"Exception thrown for deleteRow"<<endl;
126 cout<<"Error number: "<< ex.getErrorCode() << endl;
127 cout<<ex.getMessage() << endl;
128 }
129
130 con->terminateStatement (stmt);
131 }
132
133 /**
134 * displaying all the rows in the table
135 */
136 void displayAllRows ()
137 {
138 string sqlStmt = "SELECT publisher_id, publisher_add FROM publisher_tab \
139 order by publisher_id";
140 try{
141 stmt = con->createStatement (sqlStmt);
142 ResultSet *rset = stmt->executeQuery ();
143
144 while (rset->next ())
145 {
146 cout << "publisher id: " << rset->getInt (1)
147 << " publisher address: address (" ;
148 address *o = (address *)rset->getObject (2);
149 cout << (int)o->getStreet_no () << ", " << o->getCity () << ")" << endl;
150 }
151
152 stmt->closeResultSet (rset);
153 }catch(SQLException ex)
154 {
155 cout<<"Exception thrown for displayAllRows"<<endl;
156 cout<<"Error number: "<< ex.getErrorCode() << endl;
157 cout<<ex.getMessage() << endl;
158 }
159
160 con->terminateStatement (stmt);
161 }
162
163};//end of class occiobj;
164
165
166int main (void)
167{
168 string user = "hr";
169 string passwd = "hr";
170 string db = "";
171
172 try
173 {
174 cout << "occiobj - Exhibiting simple insert, delete & update operations"
175 " on Oracle objects" << endl;
176 occiobj *demo = new occiobj (user, passwd, db);
177
178 cout << "displaying all rows before operations" << endl;
179 demo->displayAllRows ();
180
181 demo->insertRow (12, 122, "MIKE");
182
183 demo->deleteRow (11, 121, "ANNA");
184
185 demo->updateRow (23, 123, "KNUTH");
186
187 cout << "displaying all rows after all operations" << endl;
188 demo->displayAllRows ();
189
190 delete (demo);
191 cout << "occiobj - done" << endl;
192 }catch (SQLException ea)
193 {
194 cerr << "Error running the demo: " << ea.getMessage () << endl;
195 }
196}