C++通过OCCI操作Oracle数据库详解
作者:网络转载 发布时间:[ 2014/3/18 11:24:52 ] 推荐标签:C++ Oracle 数据库
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;
}
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11