时间:2021-07-01 10:21:17 帮助过:20人阅读
Linux下使用occi连接oracle的方法,不需要安装客户端1. 这里假设你已经安装成功了oracle10g和Eclipse C++环境。2. 替换quot;opt
Linux下使用occi连接Oracle的方法,不需要安装客户端
1. 这里假设你已经安装成功了oracle10g和Eclipse C++环境。
2. 替换"opt/..../db_1/lib"路径下的libocci.so.10.1和libocci10.a。(首先下载occi所需要的库,从这里可以下载到所需要的两个文件)
3.编辑/etc/ld.so.conf,在最后一行输入:/opt.../db_1/lib
4,执行ldconfig
5.
示例,根据互联网修改的
//oracle.h(头文件)
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace oracle::occi;
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;
};
=======oracle.cpp=====
#include "oracle.h"
using namespace std;
using namespace oracle::occi;
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<
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);
}