Oracle® Objects for OLE C++ Class Library Developer's Guide 10g Release 2 (10.2) B14308-01 |
|
Consider an application designed to access an Oracle database and look at a personnel database. The program is intended to perform some analysis on the salaries of all the employees. Here is what the code would look like:
// Initialize the C++ Class Library before use. // Usually initialization is done at the beginning of the program. OStartup(); // Construct a database, obtaining a database connection: ODatabase odb("ExampleDB", "scott", "tiger"); // The Odatabase object (odb) allows you to connect to a database // ("ExampleDB") along with other connection information, and so sets // you up to execute SQL statements. // Construct a dynaset, obtaining the data records: ODynaset odyn(odb, "select * from emp"); // The dynaset (odyn) corresponds to a cursor. It gives you access // to the rows that are obtained by an SQL select statement. // Create a variable to contain salary information: double salary; // Now look at each record: while (!odyn.IsEOF()) // This method (IsEOF) returns TRUE when you have moved through the // entire set. { // Get the salary for the current record: odyn.GetFieldValue("sal", &salary); // This overloaded method (GetFieldValue) obtains the value of // a field in the current record of the dynaset. // Do something with the salary: Analyze(salary); // This method (Analyze) is created by the user. // Go to the next record (perhaps moving past the last record): odyn.MoveNext(); // This overloaded method (MoveNext) changes the current record // to be the next record in the dynaset's result set. } // Uninitialize the C++ Class Library when you are finished with it. // Usually uninitialization is done at the end of the program. OShutdown();
This example opens a database and selects records from an employee database. It then navigates through the result set of the query one record at a time. On each record it obtains the value of the salary column and processes it.
Note that when the ODatabase and ODynaset objects go out of scope, all necessary cleanup is performed. That includes disconnecting from the database. You, as the user of the Objects for OLE C++ Class Library, do not need to allocate any memory or free any memory.
More examples are presented in the Workbook.
Note: For more information about sample code, see Sample Code and Applications.