|
文件: |
occi_gcc343_102030.tar.gz |
大小: |
547KB |
下载: |
下载 | |
下载
linux版本。
然后下载occi所需要的头
文件:即instantclient-sdk
或者见本文附件。
|
文件: |
instantclient-sdk-linux32-11.2.0.1.zip |
大小: |
620KB |
下载: |
下载 | |
解压后里面有所需要的头文件,主要是occi.h
2. 将occi所需库拷贝到/usr/local/lib/
然后:
ln -s libocci.so libocci.so.11.1
ln -s libclntsh.so.11.1 libclntsh.so
3.
示例,根据互联网修改的
//oracle2.cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace oracle:cci;
class dbAccessor
{
public:
dbAccessor();
~dbAccessor();
int get_dbstat();
bool Connect_DB();
void Disconn_DB();
bool ExecuteUpdate(const char* sql);
ResultSet* ExecuteQuery(const char* sql);
void ClearQuery(ResultSet* set);
void Init(const char* dbName,const char* dbUser,const char* dbPass);
void SetDate(unsigned int paramIndex, time_t timeVal);
private:
Environment* m_env;
Connection* m_conn;
Statement* m_stmt;
const char *m_szUser;
const char *m_szPassword;
const char *m_szDbName;
bool bConnected;
};
using namespace std;
using namespace oracle:cci;
dbAccessor::dbAccessor()
{
bConnected=false;
m_env=0;
m_conn=0;
m_stmt=0;
}
dbAccessor::~dbAccessor()
{
Disconn_DB();
}
void dbAccessor::Init(const char* dbName,const char* dbUser,const char* dbPass)
{
m_szDbName = dbName;
m_szUser = dbUser;
m_szPassword = dbPass;
}
bool dbAccessor::Connect_DB()
{
if(m_env ==0)
m_env = Environment::createEnvironment();
if(m_conn==0){
try {
m_conn=m_env->createConnection(m_szUser,m_szPassword,m_szDbName);
} catch (SQLException& ex) {
m_conn = 0;
cout << "connect to database failed: " << ex.getMessage() << endl;
return false;
}
}
return true;
}
void dbAccessor:isconn_DB()
{
if(m_stmt!=0 && m_conn!=0)
m_conn->terminateStatement(m_stmt);
if(m_conn!=0 && m_env!=0)
m_env->terminateConnection(m_conn);
}
// 执行数据库更新操作,包括insert、update、 delete操作
bool dbAccessor::ExecuteUpdate(const char* sql)
{
bool update_OK = true;
if(sql==0){
exit(1);
}
if(m_conn==0&&m_env==0){
exit(1);
}
try {
m_stmt=m_conn->createStatement(sql);
} catch (SQLException& ex) {
m_stmt = NULL;
cout << "ExecuteUpdate failed: " << ex.getMessage() << endl;
exit(1);
}
update_OK = m_stmt->executeUpdate();
return update_OK;
}
//执行数据库查询操作,即select操作
ResultSet* dbAccessor::ExecuteQuery(const char* sql)
{
ResultSet* set=0;
if(sql==0){
exit(1);
}
if(m_conn==0&&m_env==0){
exit(1);
}
try {
m_stmt=m_conn->createStatement(sql);
} catch (SQLException& ex) {
m_stmt = NULL;
cout << "ExecuteQuery failed: " << ex.getMessage() << endl;
exit(1);
}
set=m_stmt->executeQuery();
return set;
}
void dbAccessor::ClearQuery(ResultSet* set)
{
if(m_stmt==0){
exit(1);
}
if(set==0){
exit(1);
}
m_stmt->closeResultSet(set);
}
void dbAccessor::SetDate(unsigned int paramIndex, time_t timeVal)
{
cout<m_stmt = m_conn->createStatement();
assert(m_stmt != NULL);
tm* timeStruct = localtime(&timeVal);
Date date(m_env,
timeStruct->tm_year + 1900,
timeStruct->tm_mon + 1,
timeStruct->tm_mday,
timeStruct->tm_hour,
timeStruct->tm_min,
(timeStruct->tm_sec == 60) ? 0 : timeStruct->tm_sec);
m_stmt->setDate(paramIndex, date);
}
int main(void)
{
dbAccessor obj;
obj.Init("10.1.99.200:1521/orcl","gist","gist";
bool conn = obj.Connect_DB();
if(!conn)
{
perror("obj.Connect_DB()\n";
exit(1);
}
else
{
printf("obj.Connect_DB() OK\n";
}
/*
// 1、插入数据库操作
string sql = "insert into EMAIL0(emailid,checkintime,clientip,clientport,serverip,serverport,capturetime,type,username,password,sendaddress,receiveaddress,subject,ccaddress) ";
sql += "values(1238673850,to_date('04-3-2009','MM-DD-YYYY'),'127.0.0.1',4327,'serverip',110,to_date('04-3-2009','MM-DD-YYYY'),1,'user','pass','sendaddress','receiveaddress','subject','ccaddress')";
obj.ExecuteUpdate(sql.c_str());
// 2、查询数据库操作
sql = "select emailid,to_char(checkintime,'YYYY-MM-DD HH24:MI:SS'),clientip from EMAIL0";
// 第一列emailid:Int型;第二列checkintime:Date类型,转换为字符串类型输出;第三列clientip:string字符串类型
ResultSet* set=obj.ExecuteQuery(sql.c_str());
while(set->next()){
cout << set->getInt(1) << endl; //获取第一列值,int类型
cout << set->getString(2) << endl; //获取第二列值,date类型转换成了字符串类型
cout << set->getString(3) << endl; //获取第三列值,string类型
}
obj.ClearQuery(set);
*/
return 0;
}
//makefile
CC = g++
PROGS = oracle1 oracle2
INCL_PATH = -I/opt/tools/occi/include
CFLAGS = -g -Wall -Wno-deprecated ${INCL_PATH}
LIBS = -locci -lclntsh
CLEANFILES = core core.* *.core *.o temp.* *.out typescript* \
*.lc *.lh *.bsdi *.sparc *.uw
all: ${PROGS}
oracle1: oracle1.o
${CC} ${CFLAGS} -o $@ oracle1.o ${LIBS}
oracle2: oracle2.o
${CC} ${CFLAGS} -o $@ oracle2.o ${LIBS}
.cpp.o:
$(CC) $(CFLAGS) -o $@ -c $<
clean:
rm -f ${PROGS} ${CLEANFILES}
阅读(1728) | 评论(1) | 转发(0) |