1.安装OCCI
  如果oracle数据库默认没有安装OCCI,可以自己从官网上下载与自己数据库版本一致的API,其中包含四个软件包:
  oracle-instantclient-sqlplus-10.2.0.5-1.i386.rpm
  oracle-instantclient-devel-10.2.0.5-1.i386.rpm
  oracle-instantclient-odbc-10.2.0.5-1.i386.rpm
  oracle-instantclient-basic-10.2.0.5-1.i386.rpm
  安装完成之后,会在/usr/lib下多个oracle 共享库文件夹,在/usr/include下多个oracle 头文件(接口)文件夹(可以将他们放到环境变量中)。我的数据库版本是10.2.0,下面以此为例。
  2.编写HelloWorld程序测试连接
  #include <iostream>
  #define LINUXOCCI //避免函数重定义错误
  #include <occi.h>
  using namespace std;
  using namespace oracle::occi;
  int main()
  {
  Environment *env=Environment::createEnvironment(Environment::DEFAULT);
  cout《"success"《endl;
  string name = "scott";
  string pass = "tiger";
  string srvName = "127.0.0.1:1522/orcl";
  try
  {
  Connection *conn = env->createConnection(name, pass);
  cout《"conn success"《endl;
  env->terminateConnection(conn);
  }
  catch(SQLException e)
  {
  cout《e.what()《endl;
  return -1;
  }
  Environment::terminateEnvironment(env);
  cout《"end!"《endl;
  return 0;
  }
  编译命令:
  g++
  test.cc -o test -I/usr/include/oracle/10.2.0.5/client -L/usr/lib/oracle/10.2.0.5/client/lib -locci -lsqlplus
  我没有将occi的路径加入到环境变量中,所以此处要显示列出目录才能通过编译,找到共享库 www.jx-jf.com
  运行。/test会报错,libocci.so找不到,解决办法很简单:将/usr/lib/oracle/…/lib下的库加入到LD_LIBRARY_PATH中就可以了。
  输出结果:
  success
  conn
  success
  end!
  注:这件不幸的事情可能只发生在我身上了,本人系统中有三个用户,其中一个是oracle,而程序是用另一个用户写的,于是编译通过了,但是运行总是报错:
  ORA-12162:
  TNS:net service name is incorrectly specified
  后来查明,这个是由于没有设置并导出ORACLE_SID.换了oracle用户试试,居然运行通过了,真的很伤心,原来那个用户没有设置Oracle环境变脸怎么能直接本地访问呢。
  3.进行一些操作,执行sql语句
  Employees.h
  /*
  * A simple OCCI test application
  * This file contains the Employees class declaration
  */
  #include <iostream>
  #include <occi.h>
  #include <iomanip>
  using namespace oracle::occi;
  using namespace std;
  class Employees {
  public:
  Employees();
  virtual ~Employees();
  void List();
  private:
  Environment *env;
  Connection *con;
  string user;
  string passwd;
  string db;
  };
  Employees::Employees()
  {
  /*
  69 * connect to the test database as the HR
  70 * sample user and use the EZCONNECT method
  71 * of specifying the connect string. Be sure
  72 * to adjust for your environment! The format
  73 * of the string is host:port/service_name
  74 */
  user = "scott";
  passwd = "tiger";
  db = "127.0.0.1:1522/orcl";
  env = Environment::createEnvironment(Environment::DEFAULT);
  try
  {
  con = env->createConnection(user, passwd, db);
  }
  catch (SQLException& ex)
  {
  cout 《 ex.getMessage();
  exit(EXIT_FAILURE);
  }
  }
  Employees::~Employees()
  {
  env->terminateConnection (con);
  Environment::terminateEnvironment (env);
  }
  void Employees::List()
  {
  /*
  104 * simple test method to select data from
  105 * the employees table and display the results
  106 */
  Statement *stmt = NULL;
  ResultSet *rs = NULL;
  string sql = "select EMPNO, ENAME, JOB " \
  "from EMP order by EMPNO";
  try
  {
  stmt = con->createStatement(sql);
  }
  catch (SQLException& ex)
  {
  cout 《 ex.getMessage();
  }
  if (stmt)
  {
  try
  {
  stmt->setPrefetchRowCount(32);
  rs = stmt->executeQuery();
  }
  catch (SQLException& ex)
  {
  cout 《 ex.getMessage();
  }
  if (rs)
  {
  cout 《 endl 《 setw(8) 《 left 《 "EMPNO"
  《 setw(22) 《 left 《 "ENAME"
  《 setw(27) 《 left 《 "JOB"
  《 endl;
  cout 《 setw(8) 《 left 《 "======"
  《 setw(22) 《 left 《 "===================="
  《 setw(27) 《 left 《 "========================="
  《 endl;
  while (rs->next()) {
  cout 《 setw(8) 《 left 《 rs->getInt(1)
  《 setw(22) 《 left 《 (rs->isNull(2) ? "n/a" : rs->getString(2))
  《 setw(27) 《 left 《 rs->getString(3)
  《 endl;
  }
  cout 《 endl;
  stmt->closeResultSet(rs);
  }
  con->terminateStatement(stmt);
  }
  }
  main.cc
  #include "Employees.h"
  using namespace std;
  using namespace oracle::occi;
  int main (void)
  {
  /*
  48 * create an instance of the Employees class,
  49 * invoke the List member, delete the instance,
  50 * and prompt to continue…
  51 */
  Employees *pEmployees = new Employees();
  pEmployees->List();
  delete pEmployees;
  cout 《 "ENTER to continue…";
  cin.get();
  return 0;
  }

C++通过OCCI操作Oracle数据库详解的更多相关文章

  1. JAVA通过JDBC连接Oracle数据库详解【转载】

    JAVA通过JDBC连接Oracle数据库详解 (2011-03-15 00:10:03) 转载▼http://blog.sina.com.cn/s/blog_61da86dd0100q27w.htm ...

  2. Jmeter操作MySQL数据库详解

    一.jmeter操作数据库的原理 jmeter不可直接操作数据库,必须通过驱动程序来间接操作,但如果数据库不是在本地而是云服务器上的话就需要通过网络来操作. jmeter通过驱动程序来完成对MySQL ...

  3. 使用OCCI操作Oracle数据库写入中文乱码

    解决方法如下: oracle::occi::Environment *pOracleOcciEnv = Environment::createEnvironment(oracle::occi::Env ...

  4. CMD命令操作MySql数据库详解

    第一:mysql服务的启动和停止 1.  net stop mysql 2.  net start mysql 第二:登录 mysql –u用户名 [–h主机名或者IP地址] –p密码 例如:mysq ...

  5. 原生jdbc操作mysql数据库详解

    首先给大家说一下使用JDBC链接数据库的步骤 1.加载链接数据库驱动 2.建立数据库链接 3.创建数据库操作对象 4.编写sql语句,执行sql语句 5.获取结果集 6.释放资源 我这边采用的是mav ...

  6. linux下occi操作oracle数据库,中文乱码的问题

    转载:http://www.linuxidc.com/Linux/2008-02/11238.htm 前几日调通了OCI连接数据库的问题后,用Oracle自带的例子测试了一下,能正常读取数据(都是英文 ...

  7. IOS数据库操作SQLite3使用详解(转)

    iPhone中支持通过sqlite3来访问iPhone本地的数据库.具体使用方法如下1:添加开发包libsqlite3.0.dylib首先是设置项目文件,在项目中添加iPhone版的sqlite3的数 ...

  8. oracle checkpoint 详解

    Oracle checkpoint详解 topcheckpoint扫盲 top什么是checkpoint 在数据库系统中,写日志和写数据文件是数据库中IO消耗最大的两种操作,在这两种操作中写数据文件属 ...

  9. Oracle数据字典详解

    学习笔记:oracle数据字典详解 --- 本文为TTT学习笔记,首先介绍数据字典及查看方法,然后分类总结各类数据字典的表和视图.然后列出一些附例.   数据字典系统表,保存在system表空间中. ...

随机推荐

  1. 【转】Android进阶2之Activity之间数据交流(onActivityResult的用法)----不错

    原文网址:http://blog.csdn.net/sjf0115/article/details/7387467 主要功能: 在一个主界面(主Activity)上能连接往许多不同子功能模块(子Act ...

  2. 将银行读卡设备读取到的身份证头像Bitmap属性转换成路径

    需求是这样的,在项目开发的时候要求读取身份证,读到身份证的所有信息(信息里面包括头像属性,类型是Bitmap的).然后服务器要求我传过去的头像信息是String类型的Uri路径. 这是读卡器读到的身份 ...

  3. 两台windows服务器----SVN的迁移

    两台服务器,进行SVN的迁移:系统平台:windows server 2003 版本库:test源服务器:192.168.1.14目标服务器:192.168.1.12源SVN版本库的path: D:\ ...

  4. 【中国剩余定理】POJ 1006 & HDU 1370 Biorhythms

    题目链接: http://poj.org/problem?id=1006 http://acm.hdu.edu.cn/showproblem.php?pid=1370 题目大意: (X+d)%23=a ...

  5. HDOJ(HDU) 1976 Software Version(简单判断)

    Problem Description 相信大家一定有过在网上下载软件而碰到多个不同版本的情况. 一般来说,软件的版本号由三个部分组成,主版本号(Major Version Number),子版本号( ...

  6. 将使用netTcp绑定的WCF服务寄宿到IIS7上全记录 (这文章也不错)

    原文地址:http://www.cnblogs.com/wengyuli/archive/2010/11/22/wcf-tcp-host-to-iis.html 摘要 在项目开发中,我们可能会适时的选 ...

  7. selenium webdriver python 开始

    学习资料: Selenium with Python: http://selenium-python.readthedocs.org/en/latest/index.html 乙醇的python se ...

  8. weblogic Connection has already been closed解决方法

    今天正式环境下的有一个功能报错,看了下weblogic日志,报连接已经关闭. com.ibatis.common.jdbc.exception.NestedSQLException: --- The ...

  9. NSString copy or not (strong)?

       前些日子笔者一直在维护公司的一些旧项目,项目里面的NSString属性几乎全部用的strong,而我在给项目增加一些新的功能的,又都用的copy,因为在我的脑子里几乎已经把NSString大部分 ...

  10. URL具体解释

    浏览器因特网资源:URL是浏览器寻找信息时所需的资源位置.通过URL.应用程序才干找到并使用共享因特网上大量的数据资源. 大部分URL都遵循一种标准的格式: ①HTTP协议(http://或者http ...