C + + Connect Oracle's OCCI (Windows)

Source: Internet
Author: User

In the previous section we talked about ADO connecting to Oracle, which we tried to connect to the Oracle database under the Windows platform in a Occi way, and the next section discussed connecting the remote Oracle database in a Linux environment by OCCI.

First of all, Oracle C + + callinterface (OCCI) is a set of Oracle's own application programming interfaces that allow C + + programs to interact with one or more Oracle databases, although the OCI is much more difficult to develop, but it is extremely fast, It is also an underlying interface that can manipulate almost any object in an Oracle database.

Environment: Windows7 64bit

Ide:vs 2010

Service side: winserver2008 Oracle 12c

Download the development package:

First download the corresponding Cilent development package on the Oracle website, which contains some necessary library files. Be sure to download the relative version of the client development package. I put my development kit under the D-Drive.


Occi to access the Oracle database, a few parts of the file: include header files, Lib library files, dll files, in fact, the basic steps are how to find these files.

To get these files, you need to download several packages on the Oracle website:

1:Instant Client package-basic: All files required to run OCI, OCCI, and JDBC-OCI applications

2:Instant Client package-sdk: Additional header files and an example makefile for developing Oracle application S with Instant Client

3:occi interface: Oracle C + + call Interface

Download each of these three packages separately:

1:instant client Package-basic, Instant client package-sdk Two-pack: http://www.oracle.com/technetwork/topics/ Winsoft-085727.html (Other platforms: http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html)

With a version selected, the selected version needs to be higher than the version of the Oracle database, and the versions of the two packages are consistent.

2:occi Interface: http://www.oracle.com/technetwork/database/occidownloads-083553.html

Select a version to match the version of the first two packages. As can be seen from the table in this download page, VS2010 provides a zip package, whereas the previous VS version requires an Oracle client to access the database, so it's much easier to use vs2010, just the appropriate library file.

When the download is complete:

Unzip each of the three packages:

1: Required include header file under Instantclient-sdk-nt-\instantclient_11_2\sdk\include path (Instant Client PACKAGE-SDK package)

The 2:lib file is in the Occi interface package: Oraocci11.lib or Oraocci11d.lib (the Lib file is also available in the SDK package, but the test discovery is not used)

3:dll files in the Occi interface package: Oraocci11.dll or Oraocci11d.dll, and instant Client Package-basic package: Oraociei11.dll, Oci.dll, Orannzsbb11.dll (if Orannzsbb11 not also can, the package of other DLLs, sym, EXE files without the tube, as to the specific role has not yet been studied).

Set Environment variables:

Then set the system variable in the environment variable, advanced system settings, right-click Properties, Computer


Nls_lang=american_america. Zhs16gbk

Configuration VS2010:

Create an empty project first occiconnectoracle

1. Add Library file directory:

Solution Right-occiconnectoracle-> property->c/c++-> General-, add-in directory, inbound directory address: D:\instantclient-sdk-nt-\ Instantclient_12_1\sdk\include

2. Add header file directory:

Additional Library directories, general-----linker, properties, and more: D:\INSTANTCLIENT-SDK-NT-\INSTANTCLIENT_12_1\SDK\LIB\MSVC\VC10

3. Add a library file:

Properties, linker, input, Additional dependencies: Here are oraocci12.lib and oraocci12d.lib two files, which are used when Oraocci12.lib is in release mode and oraocci12d.lib for debug mode.

Code section:

  1. /*************************** uses Oracle itself to provide a occi way to operate the database *******************/
  2. The purpose of the code is to verify that the Oracle header and Lib file paths in makefile are correct.
  3. #include <iostream>
  4. #define WIN32COMMON//Avoid function redefinition errors
  5. #include <occi.h>
  6. Using namespace std;
  7. Using namespace Oracle::occi;
  8. int main ()
  9. {
  10. System ("pause");
  11. //Create OCCI Context environment
  12. Environment *env = environment::createenvironment ();
  13. if (NULL = = env) {
  14. printf ("Createenvironment error.\n");
  15. return-1;
  16. }
  17. Else
  18. cout << "Success" << Endl;
  19. String name = "System";
  20. String pass = "123";
  21. String srvname = "";
  22. Try
  23. {
  24. //Create a database connection
  25. Connection *conn = env->createconnection (name, pass, srvname); //username, password, database name
  26. if (NULL = = conn) {
  27. printf ("CreateConnection error.\n");
  28. return-1;
  29. }
  30. Else
  31. cout << "conn Success" << Endl;
  32. Data manipulation, creating statement objects
  33. Statement *pstmt = NULL; //Statement Object
  34. pstmt = Conn->createstatement ();
  35. if (NULL = = pstmt) {
  36. printf ("createstatement error.\n");
  37. return-1;
  38. }
  39. //Query database time
  40. Std::string strtemp;
  41. ResultSet *prs = Pstmt->executequery (
  42. "Select To_char (sysdate, ' Yyyy-mm-dd HH24:MI:SS ') from DUAL");
  43. While (Prs->next ()) {
  44. strtemp = prs->getstring (1);
  45. printf ("db time:%s.\n", Strtemp.c_str ());
  46. //int type value with Getint ()
  47. Break ;
  48. }
  49. Pstmt->closeresultset (PRs);
  50. //--------inserting---------
  51. //Specify DML as auto-commit
  52. Pstmt->setautocommit (TRUE);
  53. //Set SQL statement to execute
  54. //pstmt->setsql ("INSERT into TA (ID, NAME) VALUES (1, ' ZS ')");
  55. Pstmt->setsql ("INSERT into Table_test_wang (NAME, NUM, age) VALUES (' Deng Chao ', ' 99 ', ' 41 ')");
  56. //Execute SQL statement
  57. unsigned int nret = Pstmt->executeupdate ();
  58. if (nret = = 0) {
  59. printf ("executeupdate insert error.\n");
  60. }
  61. //Terminate statement object
  62. Conn->terminatestatement (PSTMT);
  63. Close connection
  64. Env->terminateconnection (conn);
  65. //Penv->terminateconnection (Pconn);
  66. }
  67. catch (SQLException e)
  68. {
  69. cout << e.what () << Endl;
  70. System ("pause");
  71. return-1;
  72. }
  73. Releasing the OCCI context environment
  74. Environment::terminateenvironment (env);
  75. cout << "end!" << Endl;
  76. System ("pause");
  77. return 0;
  78. }

The program displays the upload data successfully, and uses SQL developer to view the database data

You can see that the database already exists and the upload is successful.

C + + Connect Oracle's OCCI (Windows)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.