转自:http://www.tidytutorials.com/2009/08/oracle-c-occi-database-example.html
Aim
The aim of this C++ tutorial is to create a simple client that uses Oracle C++ OCI (OCCI Oracle C++ Call Interface) to connect to an Oracle database. We will be using the windows command line C++ compiler cl.exe that comes with Visual Studio C++ for this example. This demonstrates connectiong to the Oracle database and manipulating data. Leave any questions or problems as comments and I will endeavour to answer them.
If you would like to see a database pool implemented for Oracle OCCI the please visit Oracle C++ OCI Database Connection Pool.
Assumptions
This article assumes that you have an Oracle database installed and running and you have downloaded and installed/unzipped the Oracle Client which contains the header files and libs required. For purposes of this example the the project directory is OracleOCCIExample. Also you have VC++ installed and configured. See Problems and Solutions Installing VC++ Express Edition, to install and run vcvars32.bat.
Versions used in this example
Sofware/Component | Image |
Windows XP SP2 | N/A |
Oracle Client | 10201_client_win32.zip |
Visual Studio Express Editions 2008 VC++ | N/A |
Oracle client is installed (expanded) in the "D:\oracle" directory for this example. Please remember to substitute your own directories for these.
NOTE: OCCI uses tnsnames.ora to connect to the database. So make sure you have this file configured with the database details you wish to connect to.
Write and compile the Client
- Write the client and save it as oracleexample.cpp in your working directory.
#include <iostream> #include <occi.h> using namespace std; int main(){ oracle::occi::Environment* environment; oracle::occi::Connection *con; oracle::occi::Statement* stmt; oracle::occi::ResultSet* res; try{ environment = oracle::occi::Environment::createEnvironment(oracle::occi::Environment::DEFAULT); con = environment->createConnection("gldbuser", "gldbuser", "MYDATABSE"); stmt = con->createStatement("select * from example"); res = stmt->executeQuery(); while (res->next()) std::cout<<res->getInt(1)<<" "<<res->getString(2)<<std::endl; stmt->closeResultSet(res); con->terminateStatement(stmt); environment->terminateConnection(con); }catch(oracle::occi::SQLException &e){ std::cout<<e.what(); } return 0; }
- Open a prompt to the working directory and compile the code using the windows cl.exe compiler. Make sure to point '-I' (capital i) option to the include files and to include mysqlcppconn.lib
Substitue your local directories.
..workspace\OracleOCCIExample>cl -o oracletest.exe oracleexample.cpp -I D:\oracle\product\10.2.0\client_1\oci\include D:\oracle\product\10.2.0\client_1\oci\lib\msvc\oraocci10.lib
- This should now create the ocalceexample.exe file. However if you run it now you might get some errors saying that some DLLs are missing.
- Add these paths to your existing path so that the executable can find the required libraries. Again, don't forget to replace the directories with the ones on your local machine. Basically you need to give the 'bin' diretory of your MySQL database installation and the 'lib' directory in your MySQL Connector C++ directory.
..\worksapce\OracleOCCIExample>set path=%path%;D:\oracle\product\10.2.0\client_1\oci\lib\msvc\vc7
..\worksapce\OracleOCCIExample>set path=%path%;D:\oracle\product\10.2.0\client_1\BIN
- Now run the oracleexample.exe and you should see the result. The most likely errors are errors stemming from problems with the tnsnames.ora and maybe DLL entry points when you run the program. To fix the DLL entry points you can set your classpath only to the paths specified above, to make sure you are not picking up any other dll.