//代码的目的就是验证makefile中oracle的头文件和lib文件路径是否正确了
#include
#define WIN32COMMON //避免函数重定义错误
#include
using namespace std;
using namespace oracle::occi;
int main()
{
system("pause");
//创建OCCI上下文环境
Environment *env = Environment::createEnvironment();
if (NULL == env) {
printf("createEnvironment error.\n");
return -1;
}
else
cout <
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 <
// 数据操作,创建Statement对象
Statement *pStmt = NULL; // Statement对象
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());
// int类型取值用getInt()
break;
}
pStmt->closeResultSet(pRs);
//--------插入---------
// 指定DML为自动提交
pStmt->setAutoCommit(TRUE);
// 设置执行的SQL语句
//pStmt->setSQL("INSERT INTO TA (ID, NAME) VALUES (1, ‘ZS‘)");
pStmt->setSQL("INSERT INTO TABLE_TEST_WANG (NAME, NUM, AGE) VALUES (‘邓超‘, ‘99‘, ‘41‘)");
// 执行SQL语句
unsigned int nRet = pStmt->executeUpdate();
if(nRet == 0) {
printf("executeUpdate insert error.\n");
}
// 终止Statement对象
conn->terminateStatement(pStmt);
// 关闭连接
env->terminateConnection(conn);
// pEnv->terminateConnection(pConn);
}
catch (SQLException e)
{
cout <
system("pause");
return -1;
}
// 释放OCCI上下文环境
Environment::terminateEnvironment(env);
cout <
system("pause");
return 0;
}
效果图:
程序显示上传数据成功,用SQL Developer查看数据库数据
可以看到数据库中已经存在,上传成功。
C++连接Oracle之OCCI(windows)
标签:
本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉 本文系统来源:http://www.cnblogs.com/aabbcc/p/5954420.html