Pro*C/C++ Getting Started 10g Release 2 (10.2) for Microsoft Windows (32-Bit) Part Number B14321-01 |
|
|
View PDF |
This chapter describes how to build Oracle database applications with Pro*C/C++ using the sample programs that are included with this release.
This chapter contains these topics:
When you install Pro*C/C++, Oracle Universal Installer copies a set of Pro*C/C++ sample programs to the ORACLE_BASE\ORACLE_HOME
\precomp\demo\proc
directory. These sample programs are listed in Table 3-1, "Sample Programs" and described in the subsequent section.
When built, the sample programs that Oracle provides produce .exe
executables.
For some sample programs, as indicated in the Notes column of the table, you must run the SQL scripts in the sample directory before you precompile and run the sample program. The SQL scripts set up the correct tables and data so that the sample programs run correctly. These SQL scripts are located in the ORACLE_BASE
\
ORACLE_HOME
\precomp\demo\sql
directory.
Oracle Corporation recommends that you build and run these sample programs to verify that Pro*C/C++ has been installed successfully and operates correctly. You can delete the programs after you use them.
You can build the sample program using a batch file called pcmake.bat
or using Visual C++ 6.0.
Table 3-1 Sample Programs
The following subsections describe the functionality of the sample programs.
Demonstrates using ANSI dynamic SQL to process SQL statements that are not known until runtime. This program is intended to demonstrate the simplest (though not the most efficient) approach to using ANSI dynamic SQL.
Demonstrates using ANSI dynamic SQL to process SQL statements that are not known until runtime. This program uses the Oracle extensions for batch processing and reference semantics.
Fetches census information for California counties. This program demonstrates various ways to navigate through collection-typed database columns.
Demonstrates how the connection pool feature can be used. It also shows how different connection pool options can be used to optimize performance.
Demonstrates connection pool feature with relatively complex set of SQL statements and shows how performance gain depends on the kind of SQL statements used by the program.
Prompts the user for an employee number, then queries the emp
table for the employee's name, salary, and commission. This program uses indicator variables (in an indicator struct) to determine whether the commission is NULL
.
Retrieves the names of all employees in a given department from the emp
table (dynamic SQL Method 3).
Finds all salespeople and prints their names and total earnings (including commissions). This program is an example of C++ inheritance.
Declares and opens a ref cursor.
The EMPCLASS
and CPPDEMO2
files were written to provide an example of how to write Pro*C/C++ programs within a C++ framework. EMPCLASS
encapsulates a specific query on the emp
table and is implemented using a cursor variable. EMPCLASS
instantiates an instance of that query and provides cursor variable functionality (that is: open
, fetch
, close
) through C++ member functions that belong to the emp
class. The empclass.pc
file is not a standalone demo program. It was written to be used by the cppdemo2
demo program. To use the emp
class, you have to write a driver (cppdemo2.pc
) which declares an instance of the emp
class and issues calls to the member functions of that class.
Fetches and adds crime records to the database based on the person's Social Security Number. This program demonstrates the mechanisms for accessing and storing large objects (LOBs) to tables and manipulating LOBs through the stored procedures available through the DBMS_LOB
package.
Shows how to use threading in conjunction with precompilers. The program creates as many sessions as there are threads.
Demonstrates navigational access to objects in the object cache.
Demonstrates the use of objects. This program manipulates the object types person and address.
Demonstrates how to use ORACA to determine various performance parameters at runtime.
Demonstrates the use of embedded PL/SQL blocks. This program prompts you for an employee name that already resides in a database. It then executes a PL/SQL block, which returns the results of four SELECT
statements.
Adds new employee records to the personnel database and checks database integrity. The employee numbers in the database are automatically selected using the current maximum employee number +10.
Logs on to an Oracle database, prompts the user for an employee number, queries the database for the employee's name, salary, and commission, and displays the result. The program continues until the user enters 0 as the employee number.
Logs on to an Oracle database, declares and opens a cursor, fetches the names, salaries, and commissions of all salespeople, displays the results, and closes the cursor.
Logs on to an Oracle database, declares and opens a cursor, fetches in batches using arrays, and prints the results using the print_rows()
function.
Demonstrates the use of type equivalencies using the LONG VARRAW
external datatype.
Prompts the user for an account number and a debit amount. The program verifies that the account number is valid and that there are sufficient funds to cover the withdrawal before it debits the account. This program shows the use of embedded SQL.
Creates a table, inserts a row, commits insert, and drops the table (dynamic SQL Method 1).
Inserts two rows into the emp
table and deletes them (dynamic SQL Method 2).
Retrieves the names of all employees in a given department from the emp
table (dynamic SQL Method 3).
Connects to an Oracle database using the scott/tiger
account. The program declares several host arrays and calls a PL/SQL stored procedure (GET_EMPLOYEES
in the CALLDEMO
package). The PL/SQL procedure returns up to ASIZE
values. The program keeps calling GET_EMPLOYEES
, getting ASIZE
arrays each time, and printing the values, until all rows have been retrieved.
Connects to an Oracle database using your username and password and prompts for a SQL statement. You can enter any legal SQL statement, but you must use regular SQL syntax, not embedded SQL. Your statement is processed. If the statement is a query, the rows fetched are displayed (dynamic SQL Method 4).
Fetches from the emp
table, using a cursor variable. The cursor is opened in the stored PL/SQL procedure open_cur
, in the EMP_DEMO_PKG
package.
Demonstrates how to do array fetches using dynamic SQL Method 4.
Demonstrates how the scrollable cursor can be used with Oracle dynamic SQL Method 4. Scrollable cursor can also be used with ANSI dynamic SQL Method 4.
Demonstrates the use of scrollable cursor with host arrays.
Demonstrates how you can use the sqlvcp()
function to determine the actual size of a VARCHAR
struct. The size is then used as an offset to increment a pointer that steps through an array of VARCHARs
.
This program also demonstrates how to use the SQLStmtGetText()
function to retrieve the text of the last SQL statement that was executed.
Adds new employee records to the personnel database and checks database integrity. You can enter as many employee names as you want and perform the SQL commands by selecting the appropriate buttons in the Employee Record dialog box. This is a GUI version of the sample program.
To run the sample programs, you must have a database account with the username scott
and the password tiger
. Also, you must have a database with the sample tables emp
and dept.
This account is included in the starter database for your Oracle Database 10g server. If the account does not exist on your database, create the account before running the sample programs. If your database does not contain emp
and dept
tables, you can use the demobld.sql
script to create them.
To build the sample tables:
Start SQL*Plus
Connect as username scott
with the password tiger
.
Run the demobld.sql
script:
SQL> @ORACLE_BASE\ORACLE_HOME\sqlplus\demo\demobld.sql;
You can build the sample programs in two ways:
Using the pcmake.bat
file provided
Using Microsoft Visual C++ 6.0
The pcmake.bat
file for compiling Pro*C/C++ demos is found in the following location:
ORACLE_BASE\ORACLE_HOME\precomp\demo\proc
This batch file is designed to illustrate how Pro*C/C++ applications can be built at the command prompt.
In order to use this batch file, Microsoft Visual Studio must be installed. The environment variable MSVCDir
must be set. Pro*C/C++ command line options and linker options vary depending on your application.
You can use this file to build a demo, to build sample1
for example:
Navigate to the location of the demo file and enter the following at the command prompt:
C:\> CD ORACLE_BASE\ORACLE_HOME\precomp\demo\proc\sample1
Enter the following:
% pcmake sample1Microsoft Visual C++ 6.0 project files have an extension of.dsp
. The.dsp
files in the ORACLE_BASE\ORACLE_HOME
\precomp\demo\proc
directory guide and control the steps necessary to precompile, compile, and link the sample programs.
Pro*C/C++, SQL*Plus, and the Object Type Translator have been integrated into the Microsoft Visual C++ sample project files. You do not have to run Pro*C/C++, SQL*Plus, and the Object Type Translator separately before compilation.
See Also:
|
To build a sample program:
Open a Visual C++ project file, such as sample1.dsp
.
Check the paths in the project file to ensure that they correspond to the configuration of your system. If they do not, change the paths accordingly. Your system may produce error messages if the paths to all components are not correct.
Select Build > Rebuild All. Visual C++ creates the executable.
By default the sample .pre
files search for their corresponding .pc
files in the C:\oracle\ora92
directory where C:\
is the drive that you are using, and oracle\ora92
represents the location of the Oracle home. If the Oracle base and Oracle home directories are different on your computer, you must change the directory path to the correct path.
To change the directory path for a sample .pre file:
In Pro*C/C++, open the .pre
file.
Double-click the filename in the Input File area to display the Input File dialog box.
Change the directory path to the correct path.
Click Open.