[2] | 1 | /*
|
---|
| 2 | * vb1.pc
|
---|
| 3 | *
|
---|
| 4 | * Prompts the user for a branch number,
|
---|
| 5 | * then queries the branch table for the branch's
|
---|
| 6 | * address, phone and fax.
|
---|
| 7 | * 2003, Siegfried Nijssen (snijssen@liacs.nl)
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 | #include <iostream.h>
|
---|
| 11 | #include <string.h>
|
---|
| 12 | #include <stdlib.h>
|
---|
| 13 | #include <sqlda.h>
|
---|
| 14 | #include <sqlcpr.h>
|
---|
| 15 | #include <sqlca.h>
|
---|
| 16 |
|
---|
| 17 | // Define constants for VARCHAR lengths.
|
---|
| 18 | #define UNAME_LEN 20
|
---|
| 19 | #define PWD_LEN 40
|
---|
| 20 |
|
---|
| 21 | //* Declare variables.
|
---|
| 22 |
|
---|
| 23 | EXEC SQL BEGIN DECLARE SECTION;
|
---|
| 24 |
|
---|
| 25 | VARCHAR username[UNAME_LEN];
|
---|
| 26 | VARCHAR password[PWD_LEN];
|
---|
| 27 |
|
---|
| 28 | // Define a host structure for the output values of a SELECT statement.
|
---|
| 29 | struct
|
---|
| 30 | {
|
---|
| 31 | VARCHAR branch_no[10];
|
---|
| 32 | VARCHAR address[60];
|
---|
| 33 | VARCHAR phone[13];
|
---|
| 34 | VARCHAR fax[13];
|
---|
| 35 | } branchrec;
|
---|
| 36 |
|
---|
| 37 | // Input host variable.
|
---|
| 38 | VARCHAR branch_no[10];
|
---|
| 39 |
|
---|
| 40 | EXEC SQL END DECLARE SECTION;
|
---|
| 41 |
|
---|
| 42 | // Declare error handling function.
|
---|
| 43 | void sql_error(char *msg)
|
---|
| 44 | {
|
---|
| 45 | char err_msg[128];
|
---|
| 46 | size_t buf_len, msg_len;
|
---|
| 47 |
|
---|
| 48 | EXEC SQL WHENEVER SQLERROR CONTINUE;
|
---|
| 49 | /* after executing this line, sql_error is no longer used as
|
---|
| 50 | * error handling function. In this way, infinite
|
---|
| 51 | * recursion is avoided if the next SQL statement
|
---|
| 52 | * in this function fails.
|
---|
| 53 | */
|
---|
| 54 |
|
---|
| 55 | // Print the error message:
|
---|
| 56 | cout << msg << endl;
|
---|
| 57 | buf_len = sizeof (err_msg);
|
---|
| 58 | sqlglm(err_msg, &buf_len, &msg_len);
|
---|
| 59 | cout << msg_len << "*" << err_msg << endl;
|
---|
| 60 |
|
---|
| 61 | // Make sure the database is consistent:
|
---|
| 62 | EXEC SQL ROLLBACK RELEASE;
|
---|
| 63 |
|
---|
| 64 | // Stop the program.
|
---|
| 65 | exit(EXIT_FAILURE);
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | void main()
|
---|
| 69 | {
|
---|
| 70 | char temp_char[32]; /* strings are of type VARCHAR in Oracle and not \0
|
---|
| 71 | * terminated. We use this temporary variable
|
---|
| 72 | * whenever a \0 terminated string is necessary.
|
---|
| 73 | */
|
---|
| 74 |
|
---|
| 75 |
|
---|
| 76 | cout << "Enter your ORACLE username: " << endl;
|
---|
| 77 | cin >> temp_char;
|
---|
| 78 |
|
---|
| 79 | /* Connect to ORACLE--
|
---|
| 80 | * Copy the username into the VARCHAR.
|
---|
| 81 | */
|
---|
| 82 | strncpy((char *) username.arr, temp_char, UNAME_LEN);
|
---|
| 83 |
|
---|
| 84 | // Set the length component of the VARCHAR. This is always required!
|
---|
| 85 | username.len = (unsigned short) strlen((char *) username.arr);
|
---|
| 86 |
|
---|
| 87 | cout << "Enter your ORACLE password: " << endl;
|
---|
| 88 | cin >> temp_char;
|
---|
| 89 |
|
---|
| 90 | // Copy the password.
|
---|
| 91 | strncpy((char *) password.arr, temp_char, PWD_LEN);
|
---|
| 92 | password.len = (unsigned short) strlen((char *) password.arr);
|
---|
| 93 |
|
---|
| 94 | // Register sql_error() as the error handler. */
|
---|
| 95 | EXEC SQL WHENEVER SQLERROR DO sql_error("ORACLE error--\n");
|
---|
| 96 |
|
---|
| 97 | /* Connect to ORACLE. Program will call sql_error()
|
---|
| 98 | * if an error occurs when connecting to the default database.
|
---|
| 99 | */
|
---|
| 100 | EXEC SQL CONNECT :username IDENTIFIED BY :password;
|
---|
| 101 |
|
---|
| 102 | cout << "Connected to ORACLE as user: " << username.arr << endl;
|
---|
| 103 |
|
---|
| 104 | /* Loop, asking for branch IDs */
|
---|
| 105 |
|
---|
| 106 | do {
|
---|
| 107 |
|
---|
| 108 | cout << "Enter branch number: (0 to quit) ";
|
---|
| 109 | cin >> temp_char;
|
---|
| 110 |
|
---|
| 111 | if ( strcmp ( temp_char, "0" ) == 0 )
|
---|
| 112 | break; // stop the while loop
|
---|
| 113 |
|
---|
| 114 | // Fill in branch_no that we will use in the query.
|
---|
| 115 | strncpy((char *) branch_no.arr, temp_char, 10);
|
---|
| 116 | branch_no.len = (unsigned short) strlen((char *) branch_no.arr);
|
---|
| 117 |
|
---|
| 118 | /* Branch to the notfound label when the
|
---|
| 119 | * 1403 ("No data found") condition occurs.
|
---|
| 120 | * Although labels are required here, this does not mean that
|
---|
| 121 | * using labels in general is good programming practice!
|
---|
| 122 | * You are only allowed to use labels as target for an SQL GOTO statement.
|
---|
| 123 | */
|
---|
| 124 | EXEC SQL WHENEVER NOT FOUND GOTO notfound;
|
---|
| 125 |
|
---|
| 126 | EXEC SQL SELECT branch_no, address, phone, fax
|
---|
| 127 | INTO :branchrec
|
---|
| 128 | FROM Branch
|
---|
| 129 | WHERE branch_no = :branch_no;
|
---|
| 130 |
|
---|
| 131 | /* Print data.
|
---|
| 132 | * The branchrec.address VARCHAR is not \0 terminated. We have
|
---|
| 133 | * to make it \0 terminated first to print it.
|
---|
| 134 | */
|
---|
| 135 | branchrec.address.arr[branchrec.address.len] = '\0';
|
---|
| 136 | cout << "Address: " << branchrec.address.arr << endl;
|
---|
| 137 |
|
---|
| 138 | branchrec.phone.arr[branchrec.phone.len] = '\0';
|
---|
| 139 | cout << "Phone: " << branchrec.phone.arr << endl;
|
---|
| 140 |
|
---|
| 141 | branchrec.fax.arr[branchrec.fax.len] = '\0';
|
---|
| 142 | cout << "Fax: " << branchrec.fax.arr << endl;
|
---|
| 143 |
|
---|
| 144 | // Continue the loop, the other lines in this loop are
|
---|
| 145 | // for error handling:
|
---|
| 146 | continue;
|
---|
| 147 |
|
---|
| 148 | notfound:
|
---|
| 149 | cout << endl << "Not a valid branch number - try again." << endl;
|
---|
| 150 |
|
---|
| 151 | }
|
---|
| 152 | while ( true );
|
---|
| 153 | // continue after an invalid branch number.
|
---|
| 154 |
|
---|
| 155 | // Disconnect from ORACLE.
|
---|
| 156 | EXEC SQL ROLLBACK WORK RELEASE;
|
---|
| 157 |
|
---|
| 158 | exit(EXIT_SUCCESS);
|
---|
| 159 | }
|
---|
| 160 |
|
---|