在CentOS 6.6上搭建C++运行环境并连接Oracle数据库
1、安装运行环境
# yum install gcc-c++
备注:此时会将gcc-c++和libstdc++-devel都安装上。
2、查看g++是否安装成功
[root@MyRHEL 桌面]# g++ -v
使用内建 specs。
目标:x86_64-RedHat-linux
配置为:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
线程模型:posix
gcc 版本 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC)
3、测试
(1)测试C语言程序testc.c
#include<stdio.h>
int main()
{
printf("Hello C!\n");
return 0;
}
运行:
# gcc testc.c -o testc
# ./testc
(2)测试C++程序test.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Come up and C++ me some time.";
cout << endl;
cout << "You won't regret it!" << endl;
return 10;
}
运行:
# g++ -o test1 test.cpp
# ./test1
4 安装OracleClient
主要安装oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm和oracle-instantclient11.2-devel-11.2.0.4.0-1.x86_64.rpm。
# rpm -ivh oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm
# rpm –ivh oracle-instantclient11.2-devel-11.2.0.4.0-1.x86_64.rpm
5 配置
(1)配置ld.so.conf文件,填入如下内容:
# vi /etc/ld.so.conf
include ld.so.conf.d/*.conf
/usr/lib/oracle/11.2/client64/lib
(2)配置/etc/hosts文件(此处填入Linux系统主机的IP地址和主机名称),填入如下内容:
# vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.48.128 MyRHEL
6 测试与Oracle数据库连接
(1)建立一个connclient.cpp文件,输入如下内容:
[root@MyRHEL test]# vi connclient.cpp
#include<iostream>
#include<string>
#include<occi.h>
using namespace std;
using namespace oracle::occi;
int main()
{
Environment *env;
Connection *conn;
Statement *stmt;
string struser("scott");
string strpwd("Orcl0991");
string strconn_string("10.208.17.206:1521/xe");
env = Environment::createEnvironment(Environment::OBJECT);
conn = env->createConnection(struser, strpwd, strconn_string);
if(NULL!= conn)
cout <<"conn success"<< endl;
else
cout <<"conn failed"<<endl;
string strsql("select deptno,dname from dept");
stmt = conn->createStatement(strsql);
ResultSet *rset = stmt->executeQuery();
while(rset->next()){
int deptno = rset->getInt(1);
string dname = rset->getString(2);
cout<<"deptno="<<deptno<<",dname="<<dname<<endl;
}
stmt->closeResultSet(rset);
conn->terminateStatement(stmt);
env->terminateConnection(conn);
Environment::terminateEnvironment(env);
return0;
}
(2)编译运行:
[root@MyRHEL test]# g++ -o connclient connclient.cpp -L /usr/lib/oracle/11.2/client64/lib -I /usr/include/oracle/11.2/client64/ -locci -lclntsh /usr/lib64/libstdc++.so.6
[root@MyRHEL test]# ./connclient
conn success
deptno=10,dname=ACCOUNTING
deptno=20,dname=RESEARCH
deptno=30,dname=SALES
deptno=40,dname=OPERATIONS