occi的例子2(代码)

来源:互联网 发布:最新免费读书软件 编辑:程序博客网 时间:2024/04/30 14:58
#include <iostream>

#include <occi.h>

using namespace oracle::occi;

using namespace std;

 

int main(int argc, char* argv[])

{// 用以连接数据库的参数

  string strDBName, strUserName, strUserPassword;

 Environment *env;  // 环境

 Connection *con;   // 数据库连接

 // 输入数据库名、用户名和口令

  cout<<"enter database name:";

  cin>>strDBName;

  cout<<"enter user name:";

  cin>>strUserName;

  cout<<"enter password:";

  cin>>strUserPassword;

 // 创建运行环境

  env = Environment::createEnvironment (Environment::DEFAULT);

  // 建立数据库连接

  con = env->createConnection (strUserName, strUserPassword, strDBName);

// 设置要执行的语句

  Statement *stmt = con->createStatement

   ("BEGIN test_proc.test_getempinfo(:v1); END;");

  // 注册输出参数

  stmt->registerOutParam (1, OCCICURSOR, 30, "");

  cout << "Executing the block :" << stmt->getSQL() << endl;

 // 执行

  stmt->executeUpdate ();

 // 获取结果集

  ResultSet *rst = stmt->getCursor(1);

 

// ---------------BEGIN VC++中以调试模式运行时,需修改这段代码-------------

  int c1;

  string c2;

 

  try{

    cout << "Printing the result set:" << endl;

    cout <<"Number"<<"/t"<<"Names"<<endl;

    // 读取并输出各记录

    while(rst->next ())

    {

        c1=rst->getInt(1);

        c2=rst->getString(2);   // VC++6.0调试模式下,这里会报错

        cout << c1<<"/t"<<c2 << endl;

}

// ---------------END VC++中以调试模式运行时,需修改这段代码-------------

  }catch(SQLException ex)

    {

     cout<<"Exception thrown for displayAllRows"<<endl;

     cout<<"Error number: "<<  ex.getErrorCode() << endl;

     cout<<ex.getMessage() << endl;

    }

 // 关闭结果集

  stmt->closeResultSet (rst);

  // 终止“语句”

  con->terminateStatement (stmt);

  cout << "occiproc - done" << endl;

  // 断开数据库连接

  env->terminateConnection (con);

  Environment::terminateEnvironment (env);

    return 0;

}

原创粉丝点击