F Sample Programs
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 the following topics:
F.1 Sample Program Descriptions
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 F-1 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 by using a batch file called pcmake.bat
or by using Microsoft Visual Studio.
See Also:
Table F-1 Sample Programs
Sample Program | Source Files | Pro*C/C++ GUI Project File | MSVC Compiler Project File | Notes |
---|---|---|---|---|
|
|
|
|
- |
|
|
|
|
- |
|
|
|
|
Run |
|
|
|
|
- |
|
|
|
|
- |
|
|
|
|
- |
|
|
|
|
Run |
|
|
|
|
- |
|
|
|
|
Run |
|
|
|
|
Run |
|
|
|
|
Run |
|
|
|
|
Run |
|
|
|
|
Run |
|
|
|
|
Run |
|
|
|
|
Run |
|
|
|
|
- |
|
|
|
|
- |
|
|
|
|
- |
|
|
|
|
- |
|
|
|
|
- |
|
|
|
|
- |
|
|
|
|
Run |
|
|
|
|
- |
|
|
|
|
- |
|
|
|
|
- |
|
|
|
|
Run |
|
|
|
|
- |
|
|
|
|
Run |
|
|
|
|
- |
|
|
|
|
- |
|
|
|
|
- |
|
|
|
|
- |
|
|
|
|
- |
The following subsections describe the functionality of the sample programs.
- ANSIDYN1
-
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.
- ANSIDYN2
-
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.
- COLDEMO1
-
Fetches census information for California counties. This program demonstrates various ways to navigate through collection-typed database columns.
- CPDEMO1
-
Demonstrates how the connection pool feature can be used. It also shows how different connection pool options can be used to optimize performance.
- CPDEMO2
-
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.
- CPPDEMO1
-
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 isNULL
. - CPPDEMO2
-
Retrieves the names of all employees in a given department from the
emp
table (dynamic SQL Method 3). - CPPDEMO3
-
Finds all salespeople and prints their names and total earnings (including commissions). This program is an example of C++ inheritance.
- CVDEMO
-
Declares and opens a ref cursor.
- EMPCLASS
-
The
EMPCLASS
andCPPDEMO2
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 theemp
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 theemp
class. Theempclass.pc
file is not a standalone demo program. It was written to be used by thecppdemo2
demo program. To use theemp
class, you have to write a driver (cppdemo2.pc
) which declares an instance of theemp
class and issues calls to the member functions of that class. - LOBDEMO1
-
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. - MLTTHRD1
-
Shows how to use threading in conjunction with precompilers. The program creates as many sessions as there are threads.
See Also:
- NAVDEMO1
-
Demonstrates navigational access to objects in the object cache.
- OBJDEMO1
-
Demonstrates the use of objects. This program manipulates the object types person and address.
- ORACA
-
Demonstrates how to use ORACA to determine various performance parameters at runtime.
- PLSSAM
-
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. - SAMPLE
-
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.
- SAMPLE1
-
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.
- SAMPLE2
-
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.
- SAMPLE3
-
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. - SAMPLE4
-
Demonstrates the use of type equivalencies using the
LONG VARRAW
external datatype. - SAMPLE5
-
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.
- SAMPLE6
-
Creates a table, inserts a row, commits insert, and drops the table (dynamic SQL Method 1).
- SAMPLE7
-
Inserts two rows into the
emp
table and deletes them (dynamic SQL Method 2). - SAMPLE8
-
Retrieves the names of all employees in a given department from the
emp
table (dynamic SQL Method 3). - SAMPLE9
-
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 theCALLDEMO
package). The PL/SQL procedure returns up toASIZE
values. The program keeps callingGET_EMPLOYEES
, gettingASIZE
arrays each time, and printing the values, until all rows have been retrieved. - SAMPLE10
-
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).
- SAMPLE11
-
Fetches from the
emp
table, using a cursor variable. The cursor is opened in the stored PL/SQL procedureopen_cur
, in theEMP_DEMO_PKG
package. - SAMPLE12
-
Demonstrates how to do array fetches using dynamic SQL Method 4.
- SCDEMO1
-
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.
- SCDEMO2
-
Demonstrates the use of scrollable cursor with host arrays.
- SQLVCP
-
Demonstrates how you can use the
sqlvcp()
function to determine the actual size of aVARCHAR
struct. The size is then used as an offset to increment a pointer that steps through an array ofVARCHARs
.This program also demonstrates how to use the
SQLStmtGetText()
function to retrieve the text of the last SQL statement that was executed. - WINSAM
-
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.
F.2 Building the Demonstration Tables
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:
F.3 About Building the Sample Programs
You can build the sample programs in two ways:
-
Using the
pcmake.bat
file provided -
Using Microsoft Visual Studio
F.3.1 Using pcmake.bat
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:
F.4 Using Microsoft Visual Studio
Microsoft Visual Studio 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 Studio sample project files. You do not have to run Pro*C/C++, SQL*Plus, and the Object Type Translator separately before compilation.
To build a sample program:
-
Open a Microsoft Visual Studio 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. Microsoft Visual Studio creates the executable.
F.5 Setting the Path for the Sample .pre Files
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.