blob: ec5381c44e15a6ad037f1e2bc22af1322502b870 [file] [log] [blame]
Tang Cheng03c0b0a2015-01-12 11:19:45 +08001/* Copyright (c) 2001, 2008, Oracle. All rights reserved. */
2/*
3 NAME
4 occidml.cpp - Basic DML Operations demo
5
6 DESCRIPTION
7 To exhibit the insertion, selection, updating and deletion of
8 a row using OCCI interface
9
10 MODIFIED (MM/DD/YY)
11 mvasudev 05/22/08 - Add try/catch blocks
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 <occi.h>
21using namespace oracle::occi;
22using namespace std;
23
24class occidml
25{
26 private:
27
28 Environment *env;
29 Connection *conn;
30 Statement *stmt;
31 public:
32
33 occidml (string user, string passwd, string db)
34 {
35 env = Environment::createEnvironment (Environment::DEFAULT);
36 conn = env->createConnection (user, passwd, db);
37 }
38
39 ~occidml ()
40 {
41 env->terminateConnection (conn);
42 Environment::terminateEnvironment (env);
43 }
44
45 /**
46 * Insertion of a row with dynamic binding, PreparedStatement functionality.
47 */
48 void insertBind (int c1, string c2)
49 {
50 string sqlStmt = "INSERT INTO author_tab VALUES (:x, :y)";
51 stmt=conn->createStatement (sqlStmt);
52 try{
53 stmt->setInt (1, c1);
54 stmt->setString (2, c2);
55 stmt->executeUpdate ();
56 cout << "insert - Success" << endl;
57 }catch(SQLException ex)
58 {
59 cout<<"Exception thrown for insertBind"<<endl;
60 cout<<"Error number: "<< ex.getErrorCode() << endl;
61 cout<<ex.getMessage() << endl;
62 }
63
64 conn->terminateStatement (stmt);
65 }
66
67 /**
68 * Inserting a row into the table.
69 */
70 void insertRow ()
71 {
72 string sqlStmt = "INSERT INTO author_tab VALUES (111, 'ASHOK')";
73 stmt = conn->createStatement (sqlStmt);
74 try{
75 stmt->executeUpdate ();
76 cout << "insert - Success" << endl;
77 }catch(SQLException ex)
78 {
79 cout<<"Exception thrown for insertRow"<<endl;
80 cout<<"Error number: "<< ex.getErrorCode() << endl;
81 cout<<ex.getMessage() << endl;
82 }
83
84 conn->terminateStatement (stmt);
85 }
86
87 /**
88 * updating a row
89 */
90 void updateRow (int c1, string c2)
91 {
92 string sqlStmt =
93 "UPDATE author_tab SET author_name = :x WHERE author_id = :y";
94 stmt = conn->createStatement (sqlStmt);
95 try{
96 stmt->setString (1, c2);
97 stmt->setInt (2, c1);
98 stmt->executeUpdate ();
99 cout << "update - Success" << endl;
100 }catch(SQLException ex)
101 {
102 cout<<"Exception thrown for updateRow"<<endl;
103 cout<<"Error number: "<< ex.getErrorCode() << endl;
104 cout<<ex.getMessage() << endl;
105 }
106
107 conn->terminateStatement (stmt);
108 }
109
110
111 /**
112 * deletion of a row
113 */
114 void deleteRow (int c1, string c2)
115 {
116 string sqlStmt =
117 "DELETE FROM author_tab WHERE author_id= :x AND author_name = :y";
118 stmt = conn->createStatement (sqlStmt);
119 try{
120 stmt->setInt (1, c1);
121 stmt->setString (2, c2);
122 stmt->executeUpdate ();
123 cout << "delete - Success" << endl;
124 }catch(SQLException ex)
125 {
126 cout<<"Exception thrown for deleteRow"<<endl;
127 cout<<"Error number: "<< ex.getErrorCode() << endl;
128 cout<<ex.getMessage() << endl;
129 }
130
131 conn->terminateStatement (stmt);
132 }
133
134 /**
135 * displaying all the rows in the table
136 */
137 void displayAllRows ()
138 {
139 string sqlStmt = "SELECT author_id, author_name FROM author_tab \
140 order by author_id";
141 stmt = conn->createStatement (sqlStmt);
142 ResultSet *rset = stmt->executeQuery ();
143 try{
144 while (rset->next ())
145 {
146 cout << "author_id: " << rset->getInt (1) << " author_name: "
147 << rset->getString (2) << endl;
148 }
149 }catch(SQLException ex)
150 {
151 cout<<"Exception thrown for displayAllRows"<<endl;
152 cout<<"Error number: "<< ex.getErrorCode() << endl;
153 cout<<ex.getMessage() << endl;
154 }
155
156 stmt->closeResultSet (rset);
157 conn->terminateStatement (stmt);
158 }
159
160 /**
161 * Inserting a row into elements table.
162 * Demonstrating the usage of BFloat and BDouble datatypes
163 */
164 void insertElement (string elm_name, float mvol=0.0, double awt=0.0)
165 {
166 BFloat mol_vol;
167 BDouble at_wt;
168
169 if (!(mvol))
170 mol_vol.isNull = TRUE;
171 else
172 mol_vol.value = mvol;
173
174 if (!(awt))
175 at_wt.isNull = TRUE;
176 else
177 at_wt.value = awt;
178
179 string sqlStmt = "INSERT INTO elements VALUES (:v1, :v2, :v3)";
180 stmt = conn->createStatement (sqlStmt);
181
182 try{
183 stmt->setString(1, elm_name);
184 stmt->setBFloat(2, mol_vol);
185 stmt->setBDouble(3, at_wt);
186 stmt->executeUpdate ();
187 cout << "insertElement - Success" << endl;
188 }catch(SQLException ex)
189 {
190 cout<<"Exception thrown for insertElement"<<endl;
191 cout<<"Error number: "<< ex.getErrorCode() << endl;
192 cout<<ex.getMessage() << endl;
193 }
194 conn->terminateStatement (stmt);
195 }
196
197 /**
198 * displaying rows from element table
199 */
200 void displayElements ()
201 {
202 string sqlStmt =
203 "SELECT element_name, molar_volume, atomic_weight FROM elements \
204 order by element_name";
205 stmt = conn->createStatement (sqlStmt);
206 ResultSet *rset = stmt->executeQuery ();
207 try{
208 cout.precision(7);
209 while (rset->next ())
210 {
211 string elem_name = rset->getString(1);
212 BFloat mol_vol = rset->getBFloat(2);
213 BDouble at_wt = rset->getBDouble(3);
214
215 cout << "Element Name: " << elem_name << endl;
216
217 if ( mol_vol.isNull )
218 cout << "Molar Volume is NULL" << endl;
219 else
220 cout << "Molar Volume: " << mol_vol.value << " cm3 mol-1" << endl;
221
222 if ( at_wt.isNull )
223 cout << "Atomic Weight is NULL" << endl;
224 else
225 cout << "Atomic Weight: " << at_wt.value << " g/mole" << endl;
226 }
227 }catch(SQLException ex)
228 {
229 cout<<"Exception thrown for displayElements"<<endl;
230 cout<<"Error number: "<< ex.getErrorCode() << endl;
231 cout<<ex.getMessage() << endl;
232 }
233
234 stmt->closeResultSet (rset);
235 conn->terminateStatement (stmt);
236 }
237
238}; // end of class occidml
239
240
241int main (void)
242{
243 string user = "hr";
244 string passwd = "hr";
245 string db = "";
246 try{
247 cout << "occidml - Exhibiting simple insert, delete & update operations"
248 << endl;
249 occidml *demo = new occidml (user, passwd, db);
250 cout << "Displaying all records before any operation" << endl;
251 demo->displayAllRows ();
252
253 cout << "Inserting a record into the table author_tab "
254 << endl;
255 demo->insertRow ();
256
257 cout << "Displaying the records after insert " << endl;
258 demo->displayAllRows ();
259
260 cout << "Inserting a records into the table author_tab using dynamic bind"
261 << endl;
262 demo->insertBind (222, "ANAND");
263
264 cout << "Displaying the records after insert using dynamic bind" << endl;
265 demo->displayAllRows ();
266
267 cout << "deleting a row with author_id as 222 from author_tab table" << endl;
268 demo->deleteRow (222, "ANAND");
269
270 cout << "updating a row with author_id as 444 from author_tab table" << endl;
271 demo->updateRow (444, "ADAM");
272
273 cout << "displaying all rows after all the operations" << endl;
274 demo->displayAllRows ();
275
276 cout << "inserting radio active element properties" << endl;
277 demo->insertElement ("Uranium", 12.572, 238.0289 );
278 demo->insertElement ("Plutonium", 12.12, 244.0642 );
279 demo->insertElement ("Curium", 18.17, 247.0703 );
280 demo->insertElement ("Thorium");
281 demo->insertElement ("Radium", 41.337, 226.0254);
282
283 cout << "displaying all radio active element properties" << endl;
284 demo->displayElements ();
285
286 delete (demo);
287 }
288 catch (SQLException ex){
289 cout << ex.getMessage() << endl;
290 }
291 cout << "occidml - done" << endl;
292}