c++向oracle数据库写入图片,C++通过OCCI操作Oracle数据库详解

HelloOracle.cpp

#include #define LINUXOCCI //避免函数重定义错误

#include using namespace std;

using namespace oracle::occi;

#pragma comment(lib,"E:\\app\\Administrator\\product\\11.2.0\\dbhome_2\\OCI\\lib\\MSVC\\oci.lib")

#pragma comment(lib,"E:\\app\\Administrator\\product\\11.2.0\\dbhome_2\\OCI\\lib\\MSVC\\oraocci11.lib")

int mainBak()

{

Environment *env=Environment::createEnvironment(Environment::DEFAULT);

cout

cout

}

catch(SQLException e)

{

cout<

Employees.h

//Employees.h

//复制代码

/*

* A simple OCCI test application

* This file contains the Employees class declaration

*/

#include #include #include 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.cpp

#include "Employees.h"

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:1521/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

}

con->terminateStatement(stmt);

}

}

main.cpp

#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();

cout << "ENTER to continue...";

cin.get();

delete pEmployees;

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值