C++连接Oracle之OCCI(windows)
时间:2021-07-01 10:21:17
帮助过:130人阅读
#include <iostream>
#define WIN32COMMON //避免函数重定义错误
#include <occi.h>
using namespace std;
using namespace oracle::occi;
int main()
{
system("pause");
Environment *env = Environment::createEnvironment();
if (NULL == env) {
printf("createEnvironment error.\n");
return -1;
}
else
cout << "success" << endl;
string name = "system";
string pass = "123";
string srvName = "192.168.26.74:1521/orcl";
try
{
Connection *conn = env->createConnection(name, pass, srvName);
if(NULL == conn) {
printf("createConnection error.\n");
return -1;
}
else
cout << "conn success" << endl;
Statement *pStmt = NULL;
pStmt = conn->createStatement();
if(NULL == pStmt) {
printf("createStatement error.\n");
return -1;
}
std::string strTemp;
ResultSet *pRs = pStmt->executeQuery(
"SELECT TO_CHAR(SYSDATE, ‘YYYY-MM-DD HH24:MI:SS‘) FROM DUAL");
while(pRs->next()) {
strTemp = pRs->getString(1);
printf("db time:%s.\n", strTemp.c_str());
break;
}
pStmt->closeResultSet(pRs);
pStmt->setAutoCommit(TRUE);
pStmt->setSQL("INSERT INTO TABLE_TEST_WANG (NAME, NUM, AGE) VALUES (‘邓超‘, ‘99‘, ‘41‘)");
unsigned int nRet = pStmt->executeUpdate();
if(nRet == 0) {
printf("executeUpdate insert error.\n");
}
conn->terminateStatement(pStmt);
env->terminateConnection(conn);
}
catch (SQLException e)
{
cout << e.what() << endl;
system("pause");
return -1;
}
Environment::terminateEnvironment(env);
cout << "end!" << endl;
system("pause");
return 0;
}
效果图:
程序显示上传数据成功,用SQL Developer查看数据库数据
可以看到数据库中已经存在,上传成功。
C++连接Oracle之OCCI(windows)
标签: