求C++连接oracle 数据库的代码
2个回答
展开全部
#include <DbManager.h>
#include <iostream>
using namespace std;
using namespace oracle::occi;
const string sqlString("select empno, ename from employee");
int main(int argc, char **argv)
{
if (argc != 2)
{
cerr << "\nUsage: " << argv[0] << " <db-user-name>\n" << endl;
exit(1);
}
// Initialize OracleServices
DbManager* dbm = NULL;
OracleServices* oras = NULL;
Statement *stmt = NULL;
ResultSet *resultSet = NULL;
try
{
// Obtain OracleServices object with the default args.
dbm = new DbManager(userName);
oras = dbm->getOracleServices();
// Obtain a connection
Connection * conn = oras->connection();
// Create a statement
stmt = conn->createStatement(sqlString);
int empno;
string ename;
// Execute query to get a resultset
resultSet = stmt->executeQuery();
while (resultSet->next())
{
empno = resultSet->getInt(1); // get the first column returned by the query;
ename = resultSet->getString(2); // get the second column returned by the query
if (resultSet->isNull(1))
{
cout << "Employee Number is null... " << endl;
}
if (resultSet->isNull(2))
{
cout << "Employee Name is null..." << endl;
}
cout << empno << "\t" << ename <<endl;
}
// Close ResultSet and Statement
stmt->closeResultSet(resultSet);
conn->terminateStatement(stmt);
// Close Connection and OCCI Environment
delete dbm;
}
catch (SQLException& ex)
{
if (dbm != NULL)
{
dbm->rollbackActions(ex, stmt, resultSet); // free resources and rollback transaction
}
}
return 0;
}
#include <iostream>
using namespace std;
using namespace oracle::occi;
const string sqlString("select empno, ename from employee");
int main(int argc, char **argv)
{
if (argc != 2)
{
cerr << "\nUsage: " << argv[0] << " <db-user-name>\n" << endl;
exit(1);
}
// Initialize OracleServices
DbManager* dbm = NULL;
OracleServices* oras = NULL;
Statement *stmt = NULL;
ResultSet *resultSet = NULL;
try
{
// Obtain OracleServices object with the default args.
dbm = new DbManager(userName);
oras = dbm->getOracleServices();
// Obtain a connection
Connection * conn = oras->connection();
// Create a statement
stmt = conn->createStatement(sqlString);
int empno;
string ename;
// Execute query to get a resultset
resultSet = stmt->executeQuery();
while (resultSet->next())
{
empno = resultSet->getInt(1); // get the first column returned by the query;
ename = resultSet->getString(2); // get the second column returned by the query
if (resultSet->isNull(1))
{
cout << "Employee Number is null... " << endl;
}
if (resultSet->isNull(2))
{
cout << "Employee Name is null..." << endl;
}
cout << empno << "\t" << ename <<endl;
}
// Close ResultSet and Statement
stmt->closeResultSet(resultSet);
conn->terminateStatement(stmt);
// Close Connection and OCCI Environment
delete dbm;
}
catch (SQLException& ex)
{
if (dbm != NULL)
{
dbm->rollbackActions(ex, stmt, resultSet); // free resources and rollback transaction
}
}
return 0;
}
追问
希望能给出示例源码
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
网易云信
2023-12-06 广告
2023-12-06 广告
网易云信提供一站式的 1 对 1 UIKit 组件库,可以更快地搭建 1 对 1 社交平台,能够快速实现音视频呼叫、音视频通话、1对1消息发送、美颜和礼物功能,直接可以复用我们的组件源码就可以了。优势:1、全套1对1 UI组件,接入更快;2...
点击进入详情页
本回答由网易云信提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询