c++使用occi连接oracle数据库的步骤。
安装依赖库
安装 linux下的oracle客户端
1、安装文件,将此四个文件拷至 linux中
oracle-instantclient11.2-basic-11.2.0.3.0-1.x86_64.rpm
oracle-instantclient11.2-devel-11.2.0.3.0-1.x86_64.rpm
oracle-instantclient11.2-odbc-11.2.0.3.0-1.x86_64.rpm
oracle-instantclient11.2-sqlplus-11.2.0.3.0-1.x86_64.rpm
使用命令安装:rpm  -ivh  *.rpm
成功之后,对应的目录下出现以下文件
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 
 | [root@Server ~]total 1644
 -rw-r--r--. 1 root root  42534 Aug 25  2013 ldap.h
 -rw-r--r--. 1 root root  34962 Aug 25  2013 nzerror.h
 -rw-r--r--. 1 root root  74787 Aug 25  2013 nzt.h
 -rw-r--r--. 1 root root  11586 Aug 25  2013 occiAQ.h
 -rw-r--r--. 1 root root  34750 Aug 25  2013 occiCommon.h
 -rw-r--r--. 1 root root  70685 Aug 25  2013 occiControl.h
 -rw-r--r--. 1 root root  39934 Aug 25  2013 occiData.h
 -rw-r--r--. 1 root root   2115 Aug 25  2013 occi.h
 -rw-r--r--. 1 root root  24778 Aug 25  2013 occiObjects.h
 -rw-r--r--. 1 root root   7203 Aug 25  2013 oci1.h
 -rw-r--r--. 1 root root  10361 Aug 25  2013 oci8dp.h
 -rw-r--r--. 1 root root 431569 Aug 25  2013 ociap.h
 -rw-r--r--. 1 root root   6204 Aug 25  2013 ociapr.h
 -rw-r--r--. 1 root root  43001 Aug 25  2013 ocidef.h
 -rw-r--r--. 1 root root   4048 Aug 25  2013 ocidem.h
 -rw-r--r--. 1 root root  11339 Aug 25  2013 ocidfn.h
 -rw-r--r--. 1 root root   8953 Aug 25  2013 ociextp.h
 -rw-r--r--. 1 root root 172157 Aug 25  2013 oci.h
 -rw-r--r--. 1 root root   6494 Aug 25  2013 ocikpr.h
 -rw-r--r--. 1 root root   7506 Aug 25  2013 ocixmldb.h
 -rw-r--r--. 1 root root 104382 Aug 25  2013 ocixstream.h
 -rw-r--r--. 1 root root  23384 Aug 25  2013 odci.h
 -rw-r--r--. 1 root root   6540 Aug 25  2013 oratypes.h
 -rw-r--r--. 1 root root  15083 Aug 25  2013 orid.h
 -rw-r--r--. 1 root root 102775 Aug 25  2013 ori.h
 -rw-r--r--. 1 root root 157901 Aug 25  2013 orl.h
 -rw-r--r--. 1 root root  42626 Aug 25  2013 oro.h
 -rw-r--r--. 1 root root 116645 Aug 25  2013 ort.h
 -rw-r--r--. 1 root root   9892 Aug 25  2013 xa.h
 [root@Server ~]
 total 48
 -rwxr-xr-x. 1 root root  8104 Aug 25  2013 adrci
 -rwxr-xr-x. 1 root root 31920 Aug 25  2013 genezi
 -rwxr-xr-x. 1 root root  4872 Aug 25  2013 sqlplus
 [root@Server ~]
 total 187440
 -rw-r--r--. 1 root root       368 Aug 25  2013 glogin.sql
 lrwxrwxrwx. 1 root root        17 Feb 29 21:42 libclntsh.so -> libclntsh.so.11.1
 -rw-r--r--. 1 root root  53865194 Aug 25  2013 libclntsh.so.11.1
 -rw-r--r--. 1 root root   7996693 Aug 25  2013 libnnz11.so
 lrwxrwxrwx. 1 root root        15 Feb 29 21:42 libocci.so -> libocci.so.11.1
 -rw-r--r--. 1 root root   1973074 Aug 25  2013 libocci.so.11.1
 -rw-r--r--. 1 root root 118738042 Aug 25  2013 libociei.so
 -rw-r--r--. 1 root root    164942 Aug 25  2013 libocijdbc11.so
 -rw-r--r--. 1 root root   1502287 Aug 25  2013 libsqlplusic.so
 -rw-r--r--. 1 root root   1469542 Aug 25  2013 libsqlplus.so
 -rw-r--r--. 1 root root   1003582 Aug 25  2013 libsqora.so.11.1
 -rw-r--r--. 1 root root   2091135 Aug 25  2013 ojdbc5.jar
 -rw-r--r--. 1 root root   2739616 Aug 25  2013 ojdbc6.jar
 -rw-r--r--. 1 root root    301703 Aug 25  2013 ottclasses.zip
 -rw-r--r--. 1 root root     66779 Aug 25  2013 xstreams.jar
 
 | 
**注:如果没有上面指明的路径,则需要重装安装,使用命令: rpm -ivh –force *.rpm
tnsnames.ora文件
手动添加文件
/usr/lib/oracle/11.2/client64/下,创建文件 ./network/admin/tnsnames.ora
tnsnames.ora:
| 12
 3
 4
 5
 6
 7
 8
 9
 
 | orcl =(DESCRIPTION =
 (ADDRESS_LIST =
 (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.2)(PORT = 1521))
 )
 (CONNECT_DATA =
 (SERVICE_NAME = orcl)
 )
 )
 
 | 
**注: ip和端口按各数据库参数而定
写测试程序
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 
 | #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 = "orcl";
 string pwd = "orcl";
 
 string sid = "192.168.1.2/orcl";
 string date;
 try
 {
 Connection *conn = env->createConnection(name, pwd, sid);
 if(conn)  cout << "connect success" << endl;
 else     cout << "connect fail" << endl;
 
 Statement *stmt = conn->createStatement();
 string sSQL = "select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual";
 stmt->setSQL(sSQL);
 ResultSet *rs = stmt->executeQuery();
 if(rs->next())
 {
 date = rs->getString(1);
 }
 cout<< "now time:" << date << endl;
 env->terminateConnection(conn);
 }
 catch(SQLException e)
 {
 cout << e.what() << endl;
 return -1;
 }
 Environment::terminateEnvironment(env);
 cout << "end!" << endl;
 return 0;
 }
 
 
 | 
环境变量配置
1.手动在linux 中执行,但是重启后就无效了;先测试环境可先这样执行
export ORACLE_HOME=/usr/lib/oracle/11.2/client64
export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:/usr/include/oracle/11.2/client64/
export LIBRARY_PATH=$LIBRARY_PATH:/usr/lib/oracle/11.2/client64/lib/
export NLS_LANG=AMERICAN_AMERICA.UTF8
export LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib:/$LD_LIBRARY_PATH
export ORACLE_SID=orcl
(一定要加 NLS_LANG的变量,字符集要和数据库相同,否则实际操作数据库的时候很可能处理不了)
在数据库中执行sql查询字符集
| 1
 | select userenv('language') from dual;
 | 
编译脚本
| 1
 | g++ test.cpp -o test -I/usr/include/oracle/11.2/client64/ -L/usr/lib/oracle/11.2/client64/lib -locci -lnnz11 -lclntsh
 |