// This program prompts the user for a stock symbol // and then displays the stock name and current price #include #include #include #include using namespace std; // Include the sql communications area: #include void main () { char repeat_loop; // Declare host variables: EXEC SQL BEGIN DECLARE SECTION; varchar userid[20]; varchar password[20]; char dbstring[10]; char stocksymbol[4]; char stockname[21]; float currentprice; EXEC SQL END DECLARE SECTION; // Prompt user for username and password: cout << "Enter your username: "; cin >> userid.arr; cout << "Enter your password: "; cin >> password.arr; // Connect to INVESTMENTS database: userid.len = strlen((char *) userid.arr); password.len = strlen((char *) password.arr); strcpy(dbstring, "cp01"); EXEC SQL connect :userid identified by :password using :dbstring; // Check connection is OK if (sqlca.sqlcode != 0) { cout << endl << "Connection failed; check username and password" << endl << endl; return; } cout << endl << endl << "Connected to database" << endl; EXEC SQL whenever sqlerror go to error_routine; cout << setiosflags(ios::fixed) << setprecision(2); do // Loop to let user do several retrievals { // Prompt the user for the stock symbol: cout << endl << "Enter the stock symbol: "; cin >> stocksymbol; EXEC SQL select stockname, currentprice into :stockname, :currentprice from stock where stocksymbol = :stocksymbol; // Display the results: if (sqlca.sqlcode == 100) cout << endl << "Stock symbol not found" << endl; else { cout << endl << "The stock name is: " << stockname; cout << endl << "The current price is: " << currentprice; } cout << endl << endl << "Enter another stock symbol Y or N? "; cin >> repeat_loop; }while (tolower(repeat_loop) == 'y'); // Disconnect from the database: cout << endl << "Disconnecting from database" << endl; EXEC SQL commit release; return; error_routine: // Negative SQLCODE returned cout << endl << "*** SQL Error Routine entered ***"; cout << endl << endl << "SQLCODE is: " << sqlca.sqlcode; cout << endl << "Error Message: " << sqlca.sqlerrm.sqlerrmc; cout << endl << "Processing terminated" << endl << endl; return; }