#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
如果没报错的话,就恭喜你了,哈哈.......