Use OCCI Interface given by Oracle
//
// Example C++ program using OCCI to connect to an Oracle database.
// Only basic setup/teardown is shown, no query logic is presented.
//
#include %26lt;stdlib.h%26gt;
#include %26lt;iostream%26gt;
#include %26lt;occi.h%26gt;
using namespace oracle::occi;
using namespace std;
/**
* Reads the environment and obtains the value of an environment
* variable previously set by the user or system administrator.
*
* If the variable var_name isn’t set in the environment this
* function will print a warning message to STDERR then exit the
* program.
*
* @param var_name the name of the environment variable to get.
* @return the value of the environment variable.
*/
const string
get_env_string (const char * const var_name)
{
char* const v = getenv(var_name);
if (!v)
{
cerr %26lt;%26lt; "Environment variable "
%26lt;%26lt; var_name
%26lt;%26lt; " must be defined."
%26lt;%26lt; endl;
exit(1);
}
return v;
}
int
main (int argc, char* argv[])
{
// Fetch our required environment variable values. The program
// will exit if one of these can’t be obtained.
//
const string user = get_env_string("ORACLE_DIRECTORY_USER");
const string pass = get_env_string("ORACLE_DIRECTORY_PASS");
const string osid = get_env_string("ORACLE_SID");
// Establish the Oracle client environment. Allows the Oracle
// libraries to allocate memory, initialize data structures, etc.
//
Environment* const env = Environment::createEnvironment(
Environment::DEFAULT);
// Open a connection to the database, then close it.
//
int ret = 0;
try
{
Connection* const con = env-%26gt;createConnection(user, pass, osid);
cout %26lt;%26lt; "Connected as " %26lt;%26lt; user %26lt;%26lt; "@" %26lt;%26lt; osid %26lt;%26lt; "." %26lt;%26lt; endl;
cout %26lt;%26lt; "Closing connection." %26lt;%26lt; endl;
env-%26gt;terminateConnection(con);
}
catch (SQLException ea)
{
// We couldn’t connect. what() will give us the Oracle error
// code and message, so we can troubleshoot the issue.
//
cerr %26lt;%26lt; "Can’t connect: " %26lt;%26lt; ea.what();
ret = 1;
}
// Cleanup the environment. It is a very good idea to let Oracle
// cleanup before your program terminates.
//
Environment::terminateEnvironment(env);
return ret;
}
Can i connect to the oracle db with c++ application, if yes pls answer my ? with example.?
To get the precompiler to generate appropriate C++ code, you need to be aware of the following issues:
Code emission by precompiler. To get C++ code, you need to set the option CODE=CPP while executing proc. C users need not worry about this option; the default caters to their needs.
Parsing capability. The PARSE option of proc may take the following values:
PARSE=NONE. C preprocessor directives are understood only inside a declare section, and all host variables need to be declared inside a declare section.
PARSE=PARTIAL. C preprocessor directives are understood; however, all host variables need to be declared inside a declare section.
PARSE=FULL. C preprocessor directives are understood and host variables can be declared anywhere. This is the default when CODE is anything other than CPP; it is an error to specify PARSE=FULL with CODE=CPP.
So, C++ users must specify PARSE=NONE or PARSE=PARTIAL. They therefore lose the freedom to declare host variables anywhere in the code. Rather, the host variables must be encapsulated in declare sections as follows:
EXEC SQL BEGIN DECLARE SECTION;
// declarations...
EXEC SQL END DECLARE SECTION;
You need to follow this routine for declaring the host and indicator variables at all the places you do so.
File extension. You need to specify the option CPP_SUFFIX=cc or CPP_SUFFIX=C.
Location of header files. By default, proc searches for header files like stdio.h in standard locations. However, C++ has its own header files, such as iostream.h, located elsewhere. So you need to use the SYS_INCLUDE option to specify the paths that proc should search for header files.
--------------------------------------...
List of Embedded SQL Statements Supported by Pro*C
Declarative Statements
EXEC SQL ARRAYLEN To use host arrays with PL/SQL
EXEC SQL BEGIN DECLARE SECTION
EXEC SQL END DECLARE SECTION To declare host variables
EXEC SQL DECLARE To name Oracle objects
EXEC SQL INCLUDE To copy in files
EXEC SQL TYPE To equivalence datatypes
EXEC SQL VAR To equivalence variables
EXEC SQL WHENEVER To handle runtime errors
Executable Statements
EXEC SQL ALLOCATE To define and control Oracle data
EXEC SQL ALTER
EXEC SQL ANALYZE
EXEC SQL AUDIT
EXEC SQL COMMENT
EXEC SQL CONNECT
EXEC SQL CREATE
EXEC SQL DROP
EXEC SQL GRANT
EXEC SQL NOAUDIT
EXEC SQL RENAME
EXEC SQL REVOKE
EXEC SQL TRUNCATE
EXEC SQL CLOSE
EXEC SQL DELETE To query and manipulate Oracle data
EXEC SQL EXPLAIN PLAN
EXEC SQL FETCH
EXEC SQL INSERT
EXEC SQL LOCK TABLE
EXEC SQL OPEN
EXEC SQL SELECT
EXEC SQL UPDATE
EXEC SQL COMMIT To process transactions
EXEC SQL ROLLBACK
EXEC SQL SAVEPOINT
EXEC SQL SET TRANSACTION
EXEC SQL DESCRIBE To use dynamic SQL
EXEC SQL EXECUTE
EXEC SQL PREPARE
EXEC SQL ALTER SESSION To control sessions
EXEC SQL SET ROLE
EXEC SQL EXECUTE
END-EXEC To embed PL/SQL blocks
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment