linux oracle远程,在LINUX下用OCCI远程访问Oracle数据库

此代码示例展示了如何使用Oracle OCCCI(Oracle Call Interface for C++)库建立数据库连接,执行SQL查询以获取USER_ID和NAME,并显示查询结果。程序首先创建Environment和Connection对象,然后执行查询并遍历结果集。最后,关闭资源并终止连接。
摘要由CSDN通过智能技术生成

#include void DisplayAllRows(oracle::occi::Connection* conn)

{

std::string sqlStmt = "SELECT USER_ID, NAME FROM IM_USER";

oracle::occi::Statement *stmt;

oracle::occi::ResultSet *rset;

stmt = conn->createStatement( sqlStmt );

try {

rset = stmt->executeQuery();

while ( rset->next() ) {

std::cout

<< "USER_ID: " << rset->getString(1) << "    "

<< "NAME: "    << rset->getString(2) << std::endl;

}

} catch(oracle::occi::SQLException ex) {

std::cout << "Exception thrown for displayAllRows" << std::endl;

std::cout << "Error number: "

}

stmt->closeResultSet(rset);

conn->terminateStatement(stmt);

}

int

main(int argc, char **argv)

{

oracle::occi::Environment *env;

oracle::occi::Connection *conn;

std::string user = "im";

std::string passwd = "123456";

std::string connect_string = "XE";  //与tnsnames文件中的名字一致

try {

env = oracle::occi::Environment::createEnvironment(oracle::occi::Environment::DEFAULT);

conn = env->createConnection(user, passwd, connect_string);

DisplayAllRows(conn);

env->terminateConnection(conn);

oracle::occi::Environment::terminateEnvironment(env);

} catch (oracle::occi::SQLException ea) {

std::cout << "Error: " << ea.getMessage() << std::endl;

}

return 0;

}

编译:

g++ -o occi_dml occi_dml.cc -L/opt/instantclient_11_2 -locci -lclntsh

如果没报错的话,就恭喜你了,哈哈.......

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值