C++通过OCCI操作Oracle数据库详解
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数据库详解的更多相关文章
- JAVA通过JDBC连接Oracle数据库详解【转载】
JAVA通过JDBC连接Oracle数据库详解 (2011-03-15 00:10:03) 转载▼http://blog.sina.com.cn/s/blog_61da86dd0100q27w.htm ...
- Jmeter操作MySQL数据库详解
一.jmeter操作数据库的原理 jmeter不可直接操作数据库,必须通过驱动程序来间接操作,但如果数据库不是在本地而是云服务器上的话就需要通过网络来操作. jmeter通过驱动程序来完成对MySQL ...
- 使用OCCI操作Oracle数据库写入中文乱码
解决方法如下: oracle::occi::Environment *pOracleOcciEnv = Environment::createEnvironment(oracle::occi::Env ...
- CMD命令操作MySql数据库详解
第一:mysql服务的启动和停止 1. net stop mysql 2. net start mysql 第二:登录 mysql –u用户名 [–h主机名或者IP地址] –p密码 例如:mysq ...
- 原生jdbc操作mysql数据库详解
首先给大家说一下使用JDBC链接数据库的步骤 1.加载链接数据库驱动 2.建立数据库链接 3.创建数据库操作对象 4.编写sql语句,执行sql语句 5.获取结果集 6.释放资源 我这边采用的是mav ...
- linux下occi操作oracle数据库,中文乱码的问题
转载:http://www.linuxidc.com/Linux/2008-02/11238.htm 前几日调通了OCI连接数据库的问题后,用Oracle自带的例子测试了一下,能正常读取数据(都是英文 ...
- IOS数据库操作SQLite3使用详解(转)
iPhone中支持通过sqlite3来访问iPhone本地的数据库.具体使用方法如下1:添加开发包libsqlite3.0.dylib首先是设置项目文件,在项目中添加iPhone版的sqlite3的数 ...
- oracle checkpoint 详解
Oracle checkpoint详解 topcheckpoint扫盲 top什么是checkpoint 在数据库系统中,写日志和写数据文件是数据库中IO消耗最大的两种操作,在这两种操作中写数据文件属 ...
- Oracle数据字典详解
学习笔记:oracle数据字典详解 --- 本文为TTT学习笔记,首先介绍数据字典及查看方法,然后分类总结各类数据字典的表和视图.然后列出一些附例. 数据字典系统表,保存在system表空间中. ...
随机推荐
- 动态规划初级练习(二):BadNeighbors
Problem Statement The old song declares "Go ahead and hate your neighbor", and the re ...
- win7怎么安装消息队列 MSMQ
win7般都默认装了消息队列只需要进入 控制面板-程序-程序和功能-已安装更新-打开或关闭windows功能 勾选 Microsoft Message Queue (MSMQ)服务器 启动服务 行了: ...
- JQuery属性过滤(转)
属性过滤(Attribute Filters)的内容就是html元素中的属性 其包括以下几个选择器: [attribute] [attribute=value] [attribute!=value] ...
- 安装LVS安装LVS和配置LVS的工作比较繁杂
安装LVS安装LVS和配置LVS的工作比较繁杂,读者在配置的过程中需要非常细心和耐心.在本节我们将对其进行详细地介绍.主要包括如下几个核心步骤:1.获取支持LVS的内核源代码如果读者需要使用LVS,需 ...
- 第二节,CCSpriteBatchNode CCSpriteFrameCache
1,CCSpriteBatchNode 精灵集合类 其中Batch的英文含义是一批,一群的意思.他的对象常常包含了许多的精灵对象,这些精灵对象有一个共同的特点,那就是使用同一张文理图片.虽然是同一个纹 ...
- 批量升级BMC固件asu64、ipmitool
需求:通过服务器远程管理IP批量升级IMM.UEFI固件 工具:asu64.ipmitool.iflash64.cdc_interface.sh 下载:http://pan.baidu.com/s/1 ...
- Virtualbox安装增强工具失败
在安装Virtualbox增强工具安装时出现unable to find the sources of your current Linux kernel,安装失败,导致主机与虚拟机之间不能共享文件夹 ...
- Javascript 第一阶段 学习使用总结
JavaScript 是一种轻量级的编程语言.JavaScript 是可插入 HTML 页面的编程代码.脚本可被放置在 HTML 页面的 <body> 和 <head> 部分中 ...
- 解决 iOS webkit 使用CSS动画时闪烁的问题
-webkit-backface-visibility: hidden;
- PHP编写的SVN类
<?php /** * SVN 外部命令 类 * * @author rubekid * * @todo comment need addslashes for svn commit * */ ...