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;
- }
可以使用Oracle为C++提供的编程开发接口,也就是 OCCI。这是一个博文,可供参考
http://blog.csdn.net/xiaobai1593/article/details/6671722 注意下载sdk时注意和你的vs版本的匹配,在oracle网站搜索“OCCI”,下载对应的版本的 basic 压缩包 和sdk就可以了。
http://www.oracle.com/technetwork/database/occidownloads-083553.html