Linux下C++连接oracle数据库 OCCI 接口

来源:互联网 发布:大数据交易市场 问题 编辑:程序博客网 时间:2024/05/17 21:51

 

环境条件

1 ORACLE 11.2.0.4版本数据库 

2 NETBEANS IDE 8.1

3 JDK 1.8

4 开发包和LIBS

Instant Client Package - SDK: Additional header files and an examplemakefile for developing Oracle applications with Instant Client

 instantclient-sdk-linux.x64-11.2.0.4.0.zip (643,089 bytes) (cksum - 3927039586)

Instant Client Package - Basic: All files required to run OCI, OCCI, andJDBC-OCI applications 

 instantclient-basic-linux.x64-11.2.0.4.0.zip (60,704,657 bytes) (cksum - 41267059)

5 WINDOS XMANGER 4.0  Xmanager - Passive

 

6 LINUX 中文版本

[oracle@svr3 instantclient_11_2]cat/etc/issue

CentOS release 6.4 (Final)

Kernel \r on an \m

 

1 数据库已经安装并运行良好 虽然同一台虚拟机上

2 NETBEANS IDE8.1 FOR LINUX C++

/u01/netbean/bin

[oracle@svr3 bin]ll

总用量 2.3M

-rwxr-xr-x. 1 root root 7.7K 10月 23 2015netbeans

-rw-r--r--. 1 root root 643K 10月 23 2015netbeans.exe

-rw-r--r--. 1 root root 1.6M 10月 23 2015netbeans64.exe

 

3 JDK

[oracle@svr3 bin]java -version

java version "1.8.0_91"

Java(TM) SE Runtime Environment (build1.8.0_91-b14)

Java HotSpot(TM) 64-Bit Server VM (build25.91-b14, mixed mode)

安装在/usr/java/jdk1.8.0_91/jre/bin/

 

4开发包和LIBS

 LIBS: instantclient-basic-linux.x64-11.2.0.4.0.zip

在WINDOS解压好了后上传到 /u01/netbean/OCCI/instantclient_11_2

-rw-r--r--. 1 root root  25K 5月  17 15:41 adrci

-rw-r--r--. 1 root root  439 5月  17 15:41 BASIC_README

-rw-r--r--. 1 root root  47K 5月  17 15:41 genezi

-rw-r--r--. 1 root root 7.7M 5月  17 15:41 libnnz11.so

-rw-r--r--. 1 root root 1.9M 5月  17 15:41 libocci.so

-rw-r--r--. 1 root root  52M 5月  17 15:41 libclntsh.so.11.1

-rw-r--r--. 1 root root 162K 5月  17 15:41 libocijdbc11.so

-rw-r--r--. 1 root root 2.0M 5月  17 15:41 ojdbc5.jar

-rw-r--r--. 1 root root 2.7M 5月  17 15:41 ojdbc6.jar

-rw-r--r--. 1 root root 188K 5月  17 15:41 uidrvci

-rw-r--r--. 1 root root  66K 5月  17 15:41 xstreams.jar

-rw-r--r--. 1 root root 114M 5月  17 15:41 libociei.so

drwxr-xr-x. 4 root root 4.0K 5月  17 15:41 sdk

-rw-r--r--. 1 root root 1.5M 5月  17 15:42 libsqlplus.so

-rw-r--r--. 1 root root 1.5M 5月  17 15:42 libsqlplusic.so

 

Include

instantclient-sdk-linux.x64-11.2.0.4.0.zip 同样解压好了后上传到

[oracle@svr3 instantclient_11_2]pwd

/u01/netbean/OCCI/instantclient_11_2

[oracle@svr3 instantclient_11_2]cd sdk/

[oracle@svr3 sdk]ll

总用量 312K

drwxr-xr-x. 2 root root 4.0K 5月  17 15:41 demo

drwxr-xr-x. 2 root root 4.0K 5月  17 15:41 include

-rw-r--r--. 1 root root  869 5月  17 15:41 ott

-rw-r--r--. 1 root root 295K 5月  17 15:41 ottclasses.zip

-rw-r--r--. 1 root root  435 5月  17 15:41 SDK_README

 

配置环境

#c++ occi

exportLD_LIBRARY_PATH=$LD_LIBRARY_PATH:/u01/netbean/OCCI/instantclient_11_2

export DISPLAY=192.168.0.67:0.0

 

OK 这样运行NETBEANS后 就把图形界面导在WINDOWS下了,当然你得先开启Xmanager– Passive来接受

然后运行个HELLO WORD 来测试下NETBEANS C++ 运行环境

http://blog.csdn.net/ZengMuAnSha/article/details/5826949

 

关于LINUX C++安装可以看看 http://blog.csdn.net/ZengMuAnSha/article/details/8141666

 

然后呢我们得把头文件和LIBS添加到NETBEAN环境中

 

 它在文件->项目属性中


我们把INCLUDE文件添加

 

添加LIBS 在连接器当中

 

我们把那个LIBOCCI.SO.11.1 得改名否则不会显示出来的. 要添加两个库

 

再然后呢就是输入代码

/*

 * Tochange this license header, choose License Headers in Project Properties.

 * Tochange this template file, choose Tools | Templates

 *and open the template in the editor.

 */

 

/*

 *File:   main.cpp

 *Author: oracle

 *

 *Created on May 12, 2016, 6:30 PM

 */

#include <iostream> 

#define LINUXOCCI //避免函数重定义错误 

#include <string> 

#include <occi.h>

using namespace std; 

using namespace oracle::occi; 

 intmain(void) 

  { 

           Environment *env; 

           Connection *conn; 

           Statement *stmt; 

           //build a connection 

           string struser("shark"); 

           string strpwd("SX_@1893-&x"); 

           string strconn_string("192.168.1.3:1521/orcl"); 

           env= Environment::createEnvironment (Environment::OBJECT);

           conn =env->createConnection(struser, strpwd, strconn_string); 

         / * con =env->createConnection (user, passwd, db);*/

           if( NULL != conn) 

                     cout << "connsuccess" << endl; 

           else 

                     cout << "connfailed" <<endl; 

           //execute sql statement 

           string strsql("select  tr_no,tr_mer_no from trade_record_hist"); 

           stmt =conn->createStatement(strsql); 

           ResultSet *rset =stmt->executeQuery(); 

           while(rset->next()){ 

                     string tr_no = rset->getString(1); 

                     string tr_mer_no =rset->getString(2); 

                     cout<<"tr_no="<<tr_no<<",tr_mer_no="<<tr_mer_no<<endl; 

           } 

           //close resourses 

           stmt->closeResultSet(rset); 

           conn->terminateStatement(stmt); 

           env->terminateConnection(conn); 

           Environment::terminateEnvironment(env); 

           return 0; 

  } 

 我在WIN7上 IDE最大化的 菜单无法稳定下来.

 

构建完了后它IDE 会把项目放在/HOME/ORACLE目录下

运行时候报错

terminate called after throwing an instanceof 'oracle::occi::SQLException'

 what():  ORA-12154: TNS: 无法解析指定的连接标识符

运行 FINISHED; Aborted; 核心已转储; 实时:  140ms; 用户:  10ms; 系统:  10ms

 

监听和TNS配置都好好的

 

[oracle@svr3 sdk]lsnrctl status

 

LSNRCTL for Linux: Version 11.2.0.4.0 -Production on 17-5月 -2016 17:58:51

 

Copyright (c) 1991, 2013, Oracle.  All rights reserved.

 

正在连接到 (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1521)))

LISTENER 的 STATUS

------------------------

别名                      LISTENER

版本                      TNSLSNRfor Linux: Version 11.2.0.4.0 - Production

启动日期                  17-5月 -201611:22:29

正常运行时间              0 天 6 小时 36 分 21 秒

跟踪级别                  off

安全性                    ON: LocalOS Authentication

SNMP                      OFF

监听程序参数文件         /u01/app/oracle/product/11.2.0.4/db1/network/admin/listener.ora

监听程序日志文件         /u01/app/oracle/diag/tnslsnr/svr3/listener/alert/log.xml

监听端点概要...

 (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))

 (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.1.3)(PORT=1521)))

服务摘要..

服务 "orcl" 包含 1 个实例。

  实例 "orcl", 状态 UNKNOWN, 包含此服务的 1 个处理程序...

命令执行成功

[oracle@svr3 sdk]cd /u01/app/oracle/product/11.2.0.4/db1/network/admin/

[oracle@svr3 admin]ll

总用量 20K

-rw-r--r--. 1 oracle oinstall  381 12月 17 2012 shrept.lst

drwxr-xr-x. 2 oracle oinstall 4.0K 5月  17 10:30 samples

-rw-r--r--. 1 oracle oinstall  212 5月  17 11:17 sqlnet.ora

-rw-r--r--. 1 oracle oinstall  549 5月  17 11:22 listener.ora

-rw-r-----. 1 oracle oinstall  389 5月  17 11:27 tnsnames.ora

[oracle@svr3 admin]cat tnsnames.ora

# tnsnames.ora Network Configuration File:/u01/app/oracle/product/11.2.0/dbhome_1/network/admin/tnsnames.ora

# Generated by Oracle configuration tools.

 

ORCL =

 (DESCRIPTION =

   (ADDRESS = (PROTOCOL = TCP)(HOST = svr3)(PORT = 1521))

   (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.3)(PORT = 1521))

   (CONNECT_DATA =

     (SERVER = DEDICATED)

     (SERVICE_NAME = orcl)

    )

  )

 

[oracle@svr3 admin]tnsping orcl

 

TNS Ping Utility for Linux: Version11.2.0.4.0 - Production on 17-5月 -2016 17:59:08

 

Copyright (c) 1997, 2013, Oracle.  All rights reserved.

 

已使用的参数文件:

/u01/app/oracle/product/11.2.0.4/db1/network/admin/sqlnet.ora

 

 

已使用 TNSNAMES 适配器来解析别名

尝试连接 (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = svr3)(PORT =1521)) (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.3)(PORT = 1521))(CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = orcl)))

OK (20 毫秒)

[oracle@svr3 admin]

 

我们直接用TNSNAME

conn = env->createConnection(struser,strpwd, "orcl");

 

OK!! 小功告成

0 0