当前位置:Gxlcms > 数据库问题 > C++连接Oracle之OCCI(windows)

C++连接Oracle之OCCI(windows)

时间:2021-07-01 10:21:17 帮助过:130人阅读

  •   
  • //代码的目的就是验证makefile中oracle的头文件和lib文件路径是否正确了  
  • #include <iostream>  
  • #define WIN32COMMON //避免函数重定义错误  
  • #include <occi.h>  
  • 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 << "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对象  
  •         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 << e.what() << endl;  
  •         system("pause");  
  •         return -1;  
  •     }  
  •   
  •   
  • // 释放OCCI上下文环境    
  •     Environment::terminateEnvironment(env);  
  •     cout << "end!" << endl;  
  •     system("pause");  
  •     return 0;  
  • }  

  • 效果图:

     

    技术分享

    程序显示上传数据成功,用SQL Developer查看数据库数据

    技术分享

    可以看到数据库中已经存在,上传成功。

    C++连接Oracle之OCCI(windows)

    标签:

    人气教程排行