//
// Source File Name = static.sqC
//
// Licensed Materials - Property of IBM
//
// (C) COPYRIGHT International Business Machines Corp. 1995, 1997
// 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 will output the entry in the FIRSTNME column where
// the entry in the LASTNAME column equals "JOHNSON".
// Otherwise, an error message is printed.
//
// An external function "check_error" is contained in the file "util.C"
// which must be compiled along with this file.
//
// EXTERNAL DEPENDENCIES :
// - Existing database for precompile purposes.
// - Precompile with the SQL precompiler (PREP in DB2)
// - Binding to a database (BIND in DB2)
// - Compiling and linking with the IBM Cset++ compiler (AIX and OS/2)
// or the Microsoft Visual C++ compiler (Windows)
// or the compiler supported on your platform.
//
// 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 <stdlib.h>
#include <string.h>
#include "util.h"
EXEC SQL INCLUDE SQLCA; /* :rk.1:erk. */
#define CHECKERR(CE_STR) if (check_error (CE_STR, &sqlca) != 0)
class Static {
public:
Static ();
Static (char *, char *);
Select ();
~Static ();
private:
EXEC SQL BEGIN DECLARE SECTION; /* :rk.2:erk. */
char firstname[13];
char userid[9];
char passwd[19];
EXEC SQL END DECLARE SECTION;
};
Static::Static () {
cout << "Connect to default database with default userid and password\n";
EXEC SQL CONNECT TO sample;
CHECKERR ("CONNECT TO sample") exit(1);
}
Static::Static (char *userid, char *passwd) {
cout << "Connect to sample database with inputted userid and password\n";
EXEC SQL CONNECT TO sample USER :userid USING :passwd;
CHECKERR ("CONNECT TO SAMPLE USING...") exit(1);
}
Static::Select () {
EXEC SQL SELECT FIRSTNME INTO :firstname
FROM employee
WHERE LASTNAME = 'JOHNSON';
CHECKERR ("SELECT statement") return 1;
cout << "First name = " << firstname << '\n';
return 0;
}
Static::~Static () {
EXEC SQL CONNECT RESET; /* :rk.6:erk. */
CHECKERR ("CONNECT RESET") exit(1);
}
int main(int argc, char *argv[]) {
cout << "Sample C program: STATIC\n";
if (argc == 3) {
Static sampleStatic (argv[1], argv[2]);
sampleStatic.Select();
} else if (argc == 1) {
Static sampleStatic;
sampleStatic.Select();
} else {
cout << "\nUSAGE: static userid passwd\n\n";
} // end if
return 0;
}
// end of program : static.sqC