// Source File Name = updat.sqC // // Licensed Materials - Property of IBM // // (C) COPYRIGHT International Business Machines Corp. 1995, 2000 // All Rights Reserved. // // US Government Users Restricted Rights - Use, duplication or // disclosure restricted by GSA ADP Schedule Contract with IBM Corp. // // // PURPOSE: This sample program demonstrates the use of static SQL. // It updates all managers in the STAFF table of the // SAMPLE database and changes their job from 'Mgr' to // 'Clerk', deletes all who are 'Sales', and inserts a // row. In all three SQL statements (UPDATE, DELETE, INSERT) // a host variable is implemented. // Finally, a ROLLBACK is done so that the SAMPLE // database remains unchanged. // // For more information about these samples see the README file. // // For more information on programming in C++, see the // - "Programming in C and C++" section of the Application Development Guide // // For more information on Building C++ Applications, see the: // - "Building C++ Applications" section of the Application Building Guide. // // For more information on the SQL language see the SQL Reference. // // For more information about these samples see the README file. // // For more information on programming in C++, see the // - "Programming in C and C++" section of the Application Development Guide // // For more information on Building C++ Applications, see the: // - "Building C++ Applications" section of the Application Building Guide. // // For more information on the SQL language see the SQL Reference. // #include <iostream.h> #include <string.h> #include <stdlib.h> #include <sqlenv.h> #include <sqlca.h> #include "utilemb.h" EXEC SQL INCLUDE SQLCA ; class Update { public: Update (); Update (char *, char *, char *); UpdateStaff (char *); DeleteStaff (char *); ~Update(); private: EXEC SQL BEGIN DECLARE SECTION; char dbname[9]; char userid[9]; char passwd[19]; char jobUpdate[6]; EXEC SQL END DECLARE SECTION; struct sqlca sqlca; }; Update::Update () { cout << "Connecting to database SAMPLE... \n"; EXEC SQL CONNECT TO sample; EMB_SQL_CHECK("CONNECT TO SAMPLE") ; cout << "Connected to database SAMPLE \n"; } Update::Update (char *dbn, char *user, char *pass) { strcpy(dbname,dbn); strcpy(userid,user); strcpy(passwd,pass); cout << "Connecting to database " << dbname << "... " << '\n'; EXEC SQL CONNECT TO :dbname USER :userid USING :passwd; EMB_SQL_CHECK("CONNECT TO SAMPLE"); } Update::UpdateStaff(char *jobUD) { strcpy(jobUpdate,jobUD); EXEC SQL UPDATE staff SET job = :jobUpdate WHERE job = 'Mgr'; EMB_SQL_CHECK("UPDATE STAFF"); cout << "All 'Mgr' have been demoted to '" << jobUpdate << "'!\n"; return 0; } Update::DeleteStaff(char *jobUD) { strcpy(jobUpdate,jobUD); EXEC SQL DELETE FROM staff WHERE job = :jobUpdate; EMB_SQL_CHECK("DELETE FROM STAFF") ; cout << "All '" << jobUpdate << "' people have been deleted!\n"; return 0; } Update::~Update() { EXEC SQL ROLLBACK; EMB_SQL_CHECK("ROLLBACK"); cout << "On second thought ... changes rolled back\n"; cout << "Connect resetting from database\n"; EXEC SQL CONNECT RESET; } int main (int argc, char *argv[]) { cout << "Sample C++ program : updat.sqC\n"; if (argc == 3) { Update updateSample ("SAMPLE", argv[1], argv[2]); updateSample.UpdateStaff("Clerk"); updateSample.DeleteStaff("Sales"); } else if (argc == 1) { Update updateSample; updateSample.UpdateStaff("Clerk"); updateSample.DeleteStaff("Sales"); } else { cout << "\nUSAGE : updat [userid password]\n\n"; } // end if return 0; } // end of program : updat.sqC