通过OCCI连接oracle(C++)
OCCI介绍
OCCI:Oracle C++调用接口(OCCI),即Oracle的C++API,允许你使用面向对象的特性、本地类、C++语言的方法来访问Oracle数据库。
OCCI优势
OCCI头文件及动态链接库
OCCI 头文件
OCCI库
构建应用程序
步骤1:初始化- <span style="color:#000000;">//include 1 header file for all OCCI classes/interfaces
- #include <occi.h>
- //create Environment
- Environment *env = Environment::createEnvironment();//创建连接对象指针
- //use the Environment instance to create connections,
- //database access
- …
- //terminate Environment by calling static method
- //Environment::terminateEnvironment
- Environment::terminateEnvironment(env);//关闭</span>
- Connection *Environment::createConnection( const string &userName,const string &password, const string &connectString )
- //First need Environment
- Environment *env = Environment::createEnvironment();
- Connection *conn=env->createConnection(“scott”,”tiger”,””);
- //3rd parameter is db name/TNS alias
- ..//database access – use the Connection object
- ..
- ..
- //logoff and terminate connection
- env->terminateConnection(conn);//断开连接
- setXXX 方法用于Statement
- getXXX 方法用于Statement & ResultSet
Connection::createStatement(string &sql);
Statement::setSQL(string &sql);
Statement::execute(string &sql); - can be used for any SQL, returnsstatus
Statement::executeUpdate(string &sql); - returns Insert/Update/Delete count
Statement::executeQuery(string &sql); - returns ResultSet(结果集)
- //createStatement() on Connection class gives a Statement
- //instance
- Statement *stmt = conn->createStatement(“ insert into Dept(Deptno,Dname, Loc) values (1, ‘ACCOUNTS’, ‘ZONE1’ ”);
- //executeUpdate for all INSERT/UPDATE/DELETE
- stmt->executeUpdate();
- conn->terminateStatement(stmt);
- Statement *stmt = conn->createStatement(“ insert into
- Emp(EmpNo,Ename) values(:1, :2) ”);
- //1 and 2 are bind placeholders
- int empno = 2;
- string empname = “JOHN W”;
- //first parameter is bind position, second is value
- stmt->setInt(1, empno);
- stmt->setString(2, empname);
- stmt->executeUpdate();
- Statement *stmt = conn->createStatement(“ select Empno,
- Ename, Sal from Emp where Hiredate >= :1”);
- //automatically converted to Date
- stmt->setString(1, “01-JAN-1987”);
- //executeQuery returns a ResultSet
- ResultSet *rs = stmt->executeQuery();
- //ResultSet::next fetches rows and returns FALSE
- //when no more rows
- while (rs->next() == true)
- {
- //get values using the getXXX methods of ResultSet
- empno = rs->getInt(1);
- empname = rs->getString(2);
- empsalary = rs->getFloat(3);
- }
- stmt->closeResultSet(rs);//to free resources
执行PL/SQL:
- //PLSQL function : functionCalculateBonus(EmpNo INNumber,
- // EmpStatus IN OUT VARCHAR2,
- // Bonus OUT Number)RETURN VARCHAR2
- //call function usinganonymous block
- Statement *stmt = conn->createStatement(“ begin :1 := CalculateBonus(
- :2, :3, :4); end;”);
- //bind position 1 is thefunction’s return value
- stmt->setInt(2, 100); //IN parameter
- stmt->setString(3, “Active”); //IN OUT parameter
- //call registerOutParam for each OUT parameter
- stmt->registerOutParam(1, OCCISTRING, 1000);//function’sreturn value
- stmt->setMaxParamSize(1, 100);//setMaxParamSize for STRING types
- stmt->registerOutParam(4, OCCIFLOAT);
- stmt->execute();
- //use getXXX methods of Statement to get OUTparameters, return value
- string msg = stmt->getString(1);//function return value
- string newstatus = stmt->getString(3);//IN OUT parameter
- float bonus = stmt->getFloat(4); //OUT parameter
示例:
- try
- {
- ResultSet *rs = stmt->executeQuery();
- while (rs->next())
- ……….
- }
- catch (SQLException &oraex) //Oracle/OCCI errors
- {
- int errno = oraex->getErrorCode();//returns the ORA number
- string errmsg = oraex->getMessage();
- //more application error handling
- }
- catch (exception &ex) //any other C++/STL error
- {
- cout << “Error “ << ex.what() << endl;
- }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。