SlideShare a Scribd company logo
Part A:
Purpose:
This laboratory provides some experience working with the C++
programming language that has an SQL query and SQL update
embedded in it.
Procedure:
Follow the instructions below.
Be sure to load the C++ to ProQueryExample code presented in
the Appendix A below.
In your
ProQueryExample.cpp source file, modify the source code so
that it changes the customer balance of Streaming Direct from
$210.40 to $337.26. Rerun the query in step 3 above. Modify
the source to return the balance of Streaming Direct to $210.40.
Rerun the query in step 3 above. Print out a copy of your
output.
Submit a cover sheet, a copy of your output, and the source
code that you used to obtain it. Follow any additional
instructions that your instructor may give you.
Appendix A
/*
* Name: ProQueryExample.cpp
*
* Description
* This source code displays a join of the invoice and
* customer tables. In addition this source code updates the
Streaming
* Direct customer name to Direct Sports and then returns the
name back
* to Streaming Direct.
*
* Remarks:
* Author Date Comment
* Mike Lukens 08/08/2008 initial code
*
*/
#include
#include
#include
using namespace oracle::occi;
using namespace std;
class occiIntf
{
public:
//constructor
occiIntf ( string login, string password, string dbStr )
{
//setup the environment which Oracle requires
environ = Environment::createEnvironment (
Environment::DEFAULT );
try
{
//create a connection to the database
connect = environ->createConnection ( login,
password, dbStr );
}
catch ( SQLException excpt )
{
cout << "Exception thrown by createConnection"
<< endl;
cout << "Error code: "<< excpt.getErrorCode ( ) <<
endl;
cout << excpt.getMessage ( ) << endl;
system ( "pause" );
exit ( 1 );
}
}
//destructor
~occiIntf ( )
{
//make sure to clean up the connection and the
environment when
//we are done!
environ->terminateConnection ( connect );
Environment::terminateEnvironment ( environ );
}
//update a field
void updateField ( string name, string id )
{
//change the customer name to/from Direct Sports in
CUSTOMER table
string sqlStatmt
= "UPDATE customer SET cust_name = :x WHERE
cust_id = :y";
statmt = connect->createStatement ( sqlStatmt );
try
{
//substitute the customer name and id into the SQL
statement
statmt->setString ( 1, name );
statmt->setString ( 2, id );
statmt->executeUpdate ();
cout << "Update succeededn" << endl;
}
catch ( SQLException excpt )
{
cout << "Exception thrown by updateField" <<
endl;
cout << "Error code: "<< excpt.getErrorCode ( ) <<
endl;
cout << excpt.getMessage ( ) << endl;
}
connect->terminateStatement ( statmt );
}
//display the invoice number, date, and customer name
void displayAllRows ( )
{
//create the SQL statement
string sqlStatmt
= "SELECT invoice_num, invoice_date, cust_name
FROM invoice, customer WHERE invoice.cust_id =
customer.cust_id";
//instantiate the statement object from our connection object
//using the SQL string to initialize the statement
statmt = connect->createStatement ( sqlStatmt );
try
{
//execute the query and point to the result set
//in order to loop through the returned data.
ResultSet *resSet = statmt->executeQuery ( );
//display the column headers
cout << endl
<< setw ( 5 ) << "INV #" << " "
<< setw ( 9 ) << "DATE " << " "
<< setw ( 20 ) << "CUSTOMER NAME" <<
endl;
//keep getting results until there are none
while ( resSet->next ( ) )
{
//extract each attribute value which is a string
cout << setw ( 5 ) << right << resSet->getString
( 1 ) << " "
<< setw ( 9 ) << resSet->getString ( 2 ) <<
" "
<< setw ( 30 ) << left << resSet->getString
( 3 ) << endl;
}
cout << endl;
//close the result set object when done
statmt->closeResultSet ( resSet );
}
catch ( SQLException excpt )
{
cout << "Exception thrown by displayAllRows." <<
endl;
cout << "Error code: " << excpt.getErrorCode ( ) <<
endl;
cout << excpt.getMessage ( ) << endl;
}
//done with statement
connect->terminateStatement ( statmt );
}
private:
//variables required by OCCI
Environment * environ;
Connection * connect;
Statement * statmt;
}; //end of occiIntf class
void main ( )
{
string dbStr = "ECET450";
string login;
string password;
cout << "Enter your Oracle login: ";
cin >> login;
cout << "Enter your Oracle password: ";
cin >> password;
system ( "CLS" ); //clear the screen
cout << "Demonstration of a query using OCCI" << endl;
// First create an object that is connected with the Oracle
database.
occiIntf * database = new occiIntf ( login, password, dbStr
);
cout << "Display invoice number, date, and customer name"
<< endl;
database->displayAllRows ( );
cout << "Update customer Streaming Direct to Direct
Sports" << endl;
database->updateField ( "Direct Sports" , "1193" );
cout << "Display invoice number, date, and updated
customer name" << endl;
database->displayAllRows ( );
cout << "Update customer Direct Sports to Streaming
Direct" << endl;
database->updateField ( "Streaming Direct" , "1193" );
cout << "Display invoice number, date, and updated
customer name" << endl;
database->displayAllRows ( );
delete database;
cout << "OCCI query and update completen" << endl;
system ( "pause" );
}
Part B:
Procedure:
Be sure to have a copy of the C++ to ProInsertExample code
presented in the Appendix B below.
Modify the member function in the ProInsertExample.cpp file
below to insert a new row in the Line table. Add the following
row: Invoice number: 42447, product ID: KW114, number
ordered: 1, and price: $595.00. Be sure to update the column
headings. Update the spacing between columns if necessary.
Modify the member function that deletes this new row inserted
in step 3 above. Display the contents of the Line table again.
Print out a copy of your output that displays the Line table
before the insertion, of the Line table after the insertion, and
the Line table after the deletion.
Submit a cover sheet, a copy of your output, and the source
code that you used to obtain it. Follow any additional
instructions that your instructor may give you.
Appendix B
/*
* Name: ProInsertExample.cpp
*
* Description:
* This source file inserts and deletes a row in the product
table.
* In addition the displayAllRows member function is
modified to display
* the contents of the product table.
*
* Remarks:
* Author Date Comment
* Mike Lukens 08/08/2008 initial code
*
*/
#include
#include
#include
using namespace oracle::occi;
using namespace std;
class occiIntf
{
public:
//constructor
occiIntf ( string login, string password, string dbStr )
{
//create an environment which Oracle requires
environ = Environment::createEnvironment (
Environment::DEFAULT );
try
{
//create a connection to the database
connect = environ->createConnection ( login,
password, dbStr );
}
catch ( SQLException excpt )
{
cout << "Exception thrown by createConnection"
<< endl;
cout << "Error code: "<< excpt.getErrorCode ( ) <<
endl;
cout << excpt.getMessage ( ) << endl;
system ( "pause" );
exit ( 1 );
}
}
//destructor
~occiIntf ( )
{
// Make sure to clean up the connection and the
environment when we are done!
environ->terminateConnection ( connect );
Environment::terminateEnvironment ( environ );
}
//update the customer name field
void updateField ( string name, string id )
{
//change the customer name in CUSTOMER table
string sqlStatmt
= "UPDATE customer SET cust_name = :x WHERE
cust_id = :y";
statmt = connect->createStatement ( sqlStatmt );
try
{
//substitute the customer name and id into the SQL
statement
statmt->setString ( 1, name );
statmt->setString ( 2, id );
statmt->executeUpdate ();
cout << "Update succeededn" << endl;
}
catch ( SQLException excpt )
{
cout << "Exception thrown by updateRow" << endl;
cout << "Error code: "<< excpt.getErrorCode ( ) <<
endl;
cout << excpt.getMessage ( ) << endl;
}
connect->terminateStatement ( statmt );
}
//display the product table
void displayAllRows ( )
{
//generate the SQL string
string sqlStatmt = "SELECT * FROM product";
//instantiate the statement object from our connection
object
//using the SQL string to initialize the statement
statmt = connect->createStatement ( sqlStatmt );
try
{
//execute the query and point to the result set in
order to
//loop through the returned data
ResultSet * resSet = statmt->executeQuery ( );
//display the column headers
cout << endl
<< setw ( 7 ) << "PROD ID" << " "
<< setw ( 20 ) << "PROD DESC" << " "
<< setw ( 3 ) << "#" << " "
<< setw ( 4 ) << "TYPE" << " "
<< setw ( 9 ) << "WAREHOUSE" << " "
<< setw ( 8 ) << "PRICE" << endl;
//keep processing the records until there are none
while ( resSet->next ( ) )
{
//get the attribute values either as strings or as
integers
cout << setw ( 7 ) << right << resSet->getString
( 1 ) << " "
<< setw ( 20 ) << resSet->getString ( 2 ) <<
" "
<< setw ( 3 ) << resSet->getInt ( 3 ) << " "
<< setw ( 4 ) << resSet->getString ( 4 ) <<
" "
<< setw ( 9 ) << resSet->getString ( 5 ) <<
" "
<< setw ( 8 ) << resSet->getFloat ( 6 ) << "
"
<< endl;
}
cout << endl;
//close the result set object
statmt->closeResultSet ( resSet );
}
catch ( SQLException excpt )
{
cout << "Exception thrown by displayAllRows" <<
endl;
cout << "Error code: " << excpt.getErrorCode ( ) <<
endl;
cout << excpt.getMessage ( ) << endl;
}
//close the statement
connect->terminateStatement ( statmt );
}
//insert a row into the product table
void insertRow ( )
{
//generate a string containing the SQL statement
string sqlStatmt =
"INSERT INTO PRODUCT VALUES ( 'MK140',
'Hair Trimmer', 23, 'AP', 'B', 19.95 )";
//instantiate a statement object from our connection
object using
//the SQL string to initialize the statement
statmt = connect->createStatement ( sqlStatmt );
try{
//execute the SQL statement
statmt->executeUpdate ( );
cout << "Inserting a row into the PRODUCT table
was successfuln" << endl;
}
catch ( SQLException excpt )
{
cout << "Exception thrown by insertRow" << endl;
cout << "Error code: "<< excpt.getErrorCode ( ) <<
endl;
cout << excpt.getMessage ( ) << endl;
}
//close this statement object
connect->terminateStatement ( statmt );
}
//delete a row
void deleteRow ( string product_id )
{
//generate the SQL statement to delete a product row
string sqlStatmt = "DELETE FROM PRODUCT
WHERE PROD_ID = :x";
statmt = connect->createStatement ( sqlStatmt );
try
{
// Substitute the product id into the prepared
statement
statmt->setString ( 1, product_id );
statmt->executeUpdate ( );
cout << "Delete a product rown" << endl;
}
catch ( SQLException excpt )
{
cout << "Exception thrown by deleteRow" << endl;
cout << "Error code: "<< excpt.getErrorCode ( ) <<
endl;
cout << excpt.getMessage ( ) << endl;
}
connect->terminateStatement ( statmt );
}
private:
// Variables required to interact with Oracle
Environment * environ;
Connection * connect;
Statement * statmt;
}; // end of class occiIntf
int main (void)
{
string dbStr = "ECET450";
string login;
string password;
cout << "Enter your Oracle login: ";
cin >> login;
cout << "Enter your Oracle password: ";
cin >> password;
system ( "CLS" );
cout << "Perform an insertion and a deletion using OCCI"
<< endl;
//instantiate an occiIntf object
occiIntf * database = new occiIntf ( login, password, dbStr
);
cout << "Display PRODUCT table" << endl;
database->displayAllRows ( );
cout << "Insert a new row into the PRODUCT table" <<
endl;
database->insertRow ( );
cout << "Display product table with the new row" << endl;
database->displayAllRows ( );
cout << "Delete the new row from the PRODUCT table" <<
endl;
database->deleteRow ( "MK140" );
cout << "Display product table without the new row" <<
endl;
database->displayAllRows ( );
delete database;
cout << "OCCI insertion and deletion completen" << endl;
system ( "pause" );
}

More Related Content

PPT
Sqlapi0.1
PDF
DConf 2016 std.database (a proposed interface & implementation)
PDF
Computer Investigatory Project
PDF
Public Training SQL Implementation & Embedded Programming in IBM i (05-09 Jun...
PDF
Public Training SQL Implementation & Embedded Programming in IBM i
DOCX
C++ COMPUTER SCIENCE PROJECT
PPT
KMUTNB - Internet Programming 6/7
PPT
C++ super market
Sqlapi0.1
DConf 2016 std.database (a proposed interface & implementation)
Computer Investigatory Project
Public Training SQL Implementation & Embedded Programming in IBM i (05-09 Jun...
Public Training SQL Implementation & Embedded Programming in IBM i
C++ COMPUTER SCIENCE PROJECT
KMUTNB - Internet Programming 6/7
C++ super market

Similar to Part APurposeThis laboratory provides some experience work.docx (20)

PPT
Oracle Sql & PLSQL Complete guide
PDF
Durgesh
PPT
PPT
PLplsql study aboutmsnsjskakajsjslajwvsbsns
DOC
Shopping mall
DOCX
Computer
PPT
MY SQL
DOCX
Supermarket
PPTX
Crime record management system project.pptx
PDF
Look Ma, “update DB to HTML5 using C++”, no hands! 
PDF
MySQL for beginners
PPTX
PPT Lecture 2.3.4 Cursortttttttttttttttttttttttttttttttts.pptx
PDF
Computer science class 12 project on Super Market Billing
PPT
Sql dml & tcl 2
PPTX
PostgreSQL Database Slides
PDF
computerscience-170129081612.pdf
PPTX
PL/SQL___________________________________
PPTX
Day 6.pptx
PDF
Drizzles Approach To Improving Performance Of The Server
Oracle Sql & PLSQL Complete guide
Durgesh
PLplsql study aboutmsnsjskakajsjslajwvsbsns
Shopping mall
Computer
MY SQL
Supermarket
Crime record management system project.pptx
Look Ma, “update DB to HTML5 using C++”, no hands! 
MySQL for beginners
PPT Lecture 2.3.4 Cursortttttttttttttttttttttttttttttttts.pptx
Computer science class 12 project on Super Market Billing
Sql dml & tcl 2
PostgreSQL Database Slides
computerscience-170129081612.pdf
PL/SQL___________________________________
Day 6.pptx
Drizzles Approach To Improving Performance Of The Server
Ad

More from dewhirstichabod (20)

DOCX
CASE STUDY 2.1 W. L. Gore and AssociatesHe was ready for anythi.docx
DOCX
Case Study 1Case Study 1Ms. A. is an apparently heal.docx
DOCX
Case study 1Client ProfileMrs. Harriet is a 68-year-old .docx
DOCX
Case Study 11.1 Why the Circus No Longer Comes to TownFor 146 y.docx
DOCX
Case Study 10.3 Regulating Love at the OfficeThe office has bec.docx
DOCX
Case Study 1 Is Business Ready for Wearable ComputersWearable .docx
DOCX
Case Study 1 Headaches Neurological system and continue practicing .docx
DOCX
CASE STUDY 1 HeadachesA 20-year-old male complains of exper.docx
DOCX
Case Study - Stambovsky v. Ackley and Ellis Realty Supreme C.docx
DOCX
CASE STUDY - THE SOCIAL NETWORKThe growing use of social network.docx
DOCX
Case Study #1 Probation or PrisonWrite a 12 to one page (.docx
DOCX
Case Studies of Data Warehousing FailuresFour studies of data .docx
DOCX
Case Studies GuidelinesWhat is a Case StudyCase studies.docx
DOCX
Case Studies Focusing on Fluency StrategiesCase Scenario .docx
DOCX
Case Project 8-2 Detecting Unauthorized ApplicationsIn conducti.docx
DOCX
Case Number 7Student’s NameInstitution Affiliation.docx
DOCX
Case number #10 OVERVIEWAbstract In this case, a local chapt.docx
DOCX
Case GE’s Two-Decade Transformation Jack Welch’s Leadership.docx
DOCX
CASE BRIEF 7.2 Tiffany and Company v. Andrew 2012 W.docx
DOCX
CASE 5Business Performance Evaluation Approaches for Thoughtf.docx
CASE STUDY 2.1 W. L. Gore and AssociatesHe was ready for anythi.docx
Case Study 1Case Study 1Ms. A. is an apparently heal.docx
Case study 1Client ProfileMrs. Harriet is a 68-year-old .docx
Case Study 11.1 Why the Circus No Longer Comes to TownFor 146 y.docx
Case Study 10.3 Regulating Love at the OfficeThe office has bec.docx
Case Study 1 Is Business Ready for Wearable ComputersWearable .docx
Case Study 1 Headaches Neurological system and continue practicing .docx
CASE STUDY 1 HeadachesA 20-year-old male complains of exper.docx
Case Study - Stambovsky v. Ackley and Ellis Realty Supreme C.docx
CASE STUDY - THE SOCIAL NETWORKThe growing use of social network.docx
Case Study #1 Probation or PrisonWrite a 12 to one page (.docx
Case Studies of Data Warehousing FailuresFour studies of data .docx
Case Studies GuidelinesWhat is a Case StudyCase studies.docx
Case Studies Focusing on Fluency StrategiesCase Scenario .docx
Case Project 8-2 Detecting Unauthorized ApplicationsIn conducti.docx
Case Number 7Student’s NameInstitution Affiliation.docx
Case number #10 OVERVIEWAbstract In this case, a local chapt.docx
Case GE’s Two-Decade Transformation Jack Welch’s Leadership.docx
CASE BRIEF 7.2 Tiffany and Company v. Andrew 2012 W.docx
CASE 5Business Performance Evaluation Approaches for Thoughtf.docx
Ad

Recently uploaded (20)

PPTX
Institutional Correction lecture only . . .
PDF
01-Introduction-to-Information-Management.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Complications of Minimal Access Surgery at WLH
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
RMMM.pdf make it easy to upload and study
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Cell Structure & Organelles in detailed.
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Business Ethics Teaching Materials for college
PPTX
PPH.pptx obstetrics and gynecology in nursing
Institutional Correction lecture only . . .
01-Introduction-to-Information-Management.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Complications of Minimal Access Surgery at WLH
2.FourierTransform-ShortQuestionswithAnswers.pdf
Microbial diseases, their pathogenesis and prophylaxis
human mycosis Human fungal infections are called human mycosis..pptx
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
O7-L3 Supply Chain Operations - ICLT Program
RMMM.pdf make it easy to upload and study
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Cell Structure & Organelles in detailed.
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
102 student loan defaulters named and shamed – Is someone you know on the list?
Business Ethics Teaching Materials for college
PPH.pptx obstetrics and gynecology in nursing

Part APurposeThis laboratory provides some experience work.docx

  • 1. Part A: Purpose: This laboratory provides some experience working with the C++ programming language that has an SQL query and SQL update embedded in it. Procedure: Follow the instructions below. Be sure to load the C++ to ProQueryExample code presented in the Appendix A below. In your ProQueryExample.cpp source file, modify the source code so that it changes the customer balance of Streaming Direct from $210.40 to $337.26. Rerun the query in step 3 above. Modify the source to return the balance of Streaming Direct to $210.40. Rerun the query in step 3 above. Print out a copy of your output. Submit a cover sheet, a copy of your output, and the source code that you used to obtain it. Follow any additional instructions that your instructor may give you. Appendix A /* * Name: ProQueryExample.cpp * * Description * This source code displays a join of the invoice and * customer tables. In addition this source code updates the
  • 2. Streaming * Direct customer name to Direct Sports and then returns the name back * to Streaming Direct. * * Remarks: * Author Date Comment * Mike Lukens 08/08/2008 initial code * */ #include #include #include using namespace oracle::occi; using namespace std; class occiIntf { public: //constructor occiIntf ( string login, string password, string dbStr ) { //setup the environment which Oracle requires environ = Environment::createEnvironment ( Environment::DEFAULT ); try { //create a connection to the database connect = environ->createConnection ( login, password, dbStr ); } catch ( SQLException excpt ) { cout << "Exception thrown by createConnection" << endl; cout << "Error code: "<< excpt.getErrorCode ( ) <<
  • 3. endl; cout << excpt.getMessage ( ) << endl; system ( "pause" ); exit ( 1 ); } } //destructor ~occiIntf ( ) { //make sure to clean up the connection and the environment when //we are done! environ->terminateConnection ( connect ); Environment::terminateEnvironment ( environ ); } //update a field void updateField ( string name, string id ) { //change the customer name to/from Direct Sports in CUSTOMER table string sqlStatmt = "UPDATE customer SET cust_name = :x WHERE cust_id = :y"; statmt = connect->createStatement ( sqlStatmt ); try { //substitute the customer name and id into the SQL statement statmt->setString ( 1, name ); statmt->setString ( 2, id ); statmt->executeUpdate (); cout << "Update succeededn" << endl; } catch ( SQLException excpt )
  • 4. { cout << "Exception thrown by updateField" << endl; cout << "Error code: "<< excpt.getErrorCode ( ) << endl; cout << excpt.getMessage ( ) << endl; } connect->terminateStatement ( statmt ); } //display the invoice number, date, and customer name void displayAllRows ( ) { //create the SQL statement string sqlStatmt = "SELECT invoice_num, invoice_date, cust_name FROM invoice, customer WHERE invoice.cust_id = customer.cust_id"; //instantiate the statement object from our connection object //using the SQL string to initialize the statement statmt = connect->createStatement ( sqlStatmt ); try { //execute the query and point to the result set //in order to loop through the returned data. ResultSet *resSet = statmt->executeQuery ( ); //display the column headers cout << endl << setw ( 5 ) << "INV #" << " " << setw ( 9 ) << "DATE " << " " << setw ( 20 ) << "CUSTOMER NAME" << endl; //keep getting results until there are none while ( resSet->next ( ) ) { //extract each attribute value which is a string
  • 5. cout << setw ( 5 ) << right << resSet->getString ( 1 ) << " " << setw ( 9 ) << resSet->getString ( 2 ) << " " << setw ( 30 ) << left << resSet->getString ( 3 ) << endl; } cout << endl; //close the result set object when done statmt->closeResultSet ( resSet ); } catch ( SQLException excpt ) { cout << "Exception thrown by displayAllRows." << endl; cout << "Error code: " << excpt.getErrorCode ( ) << endl; cout << excpt.getMessage ( ) << endl; } //done with statement connect->terminateStatement ( statmt ); } private: //variables required by OCCI Environment * environ; Connection * connect; Statement * statmt; }; //end of occiIntf class void main ( ) { string dbStr = "ECET450"; string login; string password;
  • 6. cout << "Enter your Oracle login: "; cin >> login; cout << "Enter your Oracle password: "; cin >> password; system ( "CLS" ); //clear the screen cout << "Demonstration of a query using OCCI" << endl; // First create an object that is connected with the Oracle database. occiIntf * database = new occiIntf ( login, password, dbStr ); cout << "Display invoice number, date, and customer name" << endl; database->displayAllRows ( ); cout << "Update customer Streaming Direct to Direct Sports" << endl; database->updateField ( "Direct Sports" , "1193" ); cout << "Display invoice number, date, and updated customer name" << endl; database->displayAllRows ( ); cout << "Update customer Direct Sports to Streaming Direct" << endl; database->updateField ( "Streaming Direct" , "1193" ); cout << "Display invoice number, date, and updated customer name" << endl; database->displayAllRows ( ); delete database; cout << "OCCI query and update completen" << endl; system ( "pause" ); }
  • 7. Part B: Procedure: Be sure to have a copy of the C++ to ProInsertExample code presented in the Appendix B below. Modify the member function in the ProInsertExample.cpp file below to insert a new row in the Line table. Add the following row: Invoice number: 42447, product ID: KW114, number ordered: 1, and price: $595.00. Be sure to update the column headings. Update the spacing between columns if necessary. Modify the member function that deletes this new row inserted in step 3 above. Display the contents of the Line table again. Print out a copy of your output that displays the Line table before the insertion, of the Line table after the insertion, and the Line table after the deletion. Submit a cover sheet, a copy of your output, and the source code that you used to obtain it. Follow any additional instructions that your instructor may give you. Appendix B /* * Name: ProInsertExample.cpp * * Description: * This source file inserts and deletes a row in the product table. * In addition the displayAllRows member function is modified to display * the contents of the product table.
  • 8. * * Remarks: * Author Date Comment * Mike Lukens 08/08/2008 initial code * */ #include #include #include using namespace oracle::occi; using namespace std; class occiIntf { public: //constructor occiIntf ( string login, string password, string dbStr ) { //create an environment which Oracle requires environ = Environment::createEnvironment ( Environment::DEFAULT ); try { //create a connection to the database connect = environ->createConnection ( login, password, dbStr ); } catch ( SQLException excpt ) { cout << "Exception thrown by createConnection" << endl; cout << "Error code: "<< excpt.getErrorCode ( ) << endl; cout << excpt.getMessage ( ) << endl; system ( "pause" ); exit ( 1 );
  • 9. } } //destructor ~occiIntf ( ) { // Make sure to clean up the connection and the environment when we are done! environ->terminateConnection ( connect ); Environment::terminateEnvironment ( environ ); } //update the customer name field void updateField ( string name, string id ) { //change the customer name in CUSTOMER table string sqlStatmt = "UPDATE customer SET cust_name = :x WHERE cust_id = :y"; statmt = connect->createStatement ( sqlStatmt ); try { //substitute the customer name and id into the SQL statement statmt->setString ( 1, name ); statmt->setString ( 2, id ); statmt->executeUpdate (); cout << "Update succeededn" << endl; } catch ( SQLException excpt ) { cout << "Exception thrown by updateRow" << endl; cout << "Error code: "<< excpt.getErrorCode ( ) << endl; cout << excpt.getMessage ( ) << endl; }
  • 10. connect->terminateStatement ( statmt ); } //display the product table void displayAllRows ( ) { //generate the SQL string string sqlStatmt = "SELECT * FROM product"; //instantiate the statement object from our connection object //using the SQL string to initialize the statement statmt = connect->createStatement ( sqlStatmt ); try { //execute the query and point to the result set in order to //loop through the returned data ResultSet * resSet = statmt->executeQuery ( ); //display the column headers cout << endl << setw ( 7 ) << "PROD ID" << " " << setw ( 20 ) << "PROD DESC" << " " << setw ( 3 ) << "#" << " " << setw ( 4 ) << "TYPE" << " " << setw ( 9 ) << "WAREHOUSE" << " " << setw ( 8 ) << "PRICE" << endl; //keep processing the records until there are none while ( resSet->next ( ) ) { //get the attribute values either as strings or as integers cout << setw ( 7 ) << right << resSet->getString ( 1 ) << " " << setw ( 20 ) << resSet->getString ( 2 ) << " " << setw ( 3 ) << resSet->getInt ( 3 ) << " "
  • 11. << setw ( 4 ) << resSet->getString ( 4 ) << " " << setw ( 9 ) << resSet->getString ( 5 ) << " " << setw ( 8 ) << resSet->getFloat ( 6 ) << " " << endl; } cout << endl; //close the result set object statmt->closeResultSet ( resSet ); } catch ( SQLException excpt ) { cout << "Exception thrown by displayAllRows" << endl; cout << "Error code: " << excpt.getErrorCode ( ) << endl; cout << excpt.getMessage ( ) << endl; } //close the statement connect->terminateStatement ( statmt ); } //insert a row into the product table void insertRow ( ) { //generate a string containing the SQL statement string sqlStatmt = "INSERT INTO PRODUCT VALUES ( 'MK140', 'Hair Trimmer', 23, 'AP', 'B', 19.95 )"; //instantiate a statement object from our connection object using //the SQL string to initialize the statement statmt = connect->createStatement ( sqlStatmt ); try{
  • 12. //execute the SQL statement statmt->executeUpdate ( ); cout << "Inserting a row into the PRODUCT table was successfuln" << endl; } catch ( SQLException excpt ) { cout << "Exception thrown by insertRow" << endl; cout << "Error code: "<< excpt.getErrorCode ( ) << endl; cout << excpt.getMessage ( ) << endl; } //close this statement object connect->terminateStatement ( statmt ); } //delete a row void deleteRow ( string product_id ) { //generate the SQL statement to delete a product row string sqlStatmt = "DELETE FROM PRODUCT WHERE PROD_ID = :x"; statmt = connect->createStatement ( sqlStatmt ); try { // Substitute the product id into the prepared statement statmt->setString ( 1, product_id ); statmt->executeUpdate ( ); cout << "Delete a product rown" << endl; } catch ( SQLException excpt ) { cout << "Exception thrown by deleteRow" << endl; cout << "Error code: "<< excpt.getErrorCode ( ) << endl;
  • 13. cout << excpt.getMessage ( ) << endl; } connect->terminateStatement ( statmt ); } private: // Variables required to interact with Oracle Environment * environ; Connection * connect; Statement * statmt; }; // end of class occiIntf int main (void) { string dbStr = "ECET450"; string login; string password; cout << "Enter your Oracle login: "; cin >> login; cout << "Enter your Oracle password: "; cin >> password; system ( "CLS" ); cout << "Perform an insertion and a deletion using OCCI" << endl; //instantiate an occiIntf object occiIntf * database = new occiIntf ( login, password, dbStr ); cout << "Display PRODUCT table" << endl; database->displayAllRows ( ); cout << "Insert a new row into the PRODUCT table" << endl; database->insertRow ( );
  • 14. cout << "Display product table with the new row" << endl; database->displayAllRows ( ); cout << "Delete the new row from the PRODUCT table" << endl; database->deleteRow ( "MK140" ); cout << "Display product table without the new row" << endl; database->displayAllRows ( ); delete database; cout << "OCCI insertion and deletion completen" << endl; system ( "pause" ); }