C++通过OCCI操作Oracle数据库详解

发表于:2014-3-18 10:29

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:V明亮    来源:51Testing软件测试网采编

  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;
}
22/2<12
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号