C + + Executes SELECT statements, stored procedures, functions by OCCI, and takes cursor value samples

Source: Internet
Author: User
Tags getmessage

#include <stdio.h>

#include <iostream>
#include <String>
using namespace Std;

#include <occi.h>
#include <occiControl.h>
using namespace Oracle::occi;


/*
void Selectdemo ()
{
Environment *penv = NULL; Defining execution Environment objects
Connection *pconn = NULL; Defining a linked object
Statement *pstmt = NULL; Defining Execution Statement objects
ResultSet *prs = NULL; To define a result set object

Const string szusername = "Demo"; Set User name
Const string SZUSERPWD = "Demo"; Set Password
Const string szconnection = "ICSS"; Set the database to link to, "ICSS" is the database name

Try
{
   penv = environment::createenvironment ();//Create an execution Environment
   pconn = Penv->cre Ateconnection (szUserName, Szuserpwd, szconnection);
                                                   //created a link with three parameters.
   if (NULL = = pconn)//If link creation fails, execute the following code
   {
    cout << connectio N Failure "<< Endl;
    return;
  }

   pstmt = pconn->createstatement ()//Create a statement object by Connection object
   string strSQL = " SELECT * from Test1 ";
   pstmt-> setsql (strSQL);//Set the properties of the statement execution Object
   pRs = Pstmt->executequery ();//Execute Call, Returns the result set object
   while (PRs-> next ())        //traversal of the result set, outputting data in the result set
& nbsp;  {
    cout << prs->getint (1) << "" << prs->getstring (2) << E Ndl The use of the
  }   //getint () and GetString () methods depends on the type of data in the dataset. 1 and 2 refers to the first and second fields
}
catch (SQLException ex)
{
   cout << ex.getmessage () << Endl;
}

Pstmt-> Closeresultset (pRs); Statement object invoke method to close data set
Pconn-> terminatestatement (pstmt); The connection object calls the Terminatestatement () method to close the statement object
Penv-> terminateconnection (pconn); Environment object Invoke method to close Connection object
Environment::terminateenvironment (PENV); Closes the Environment object.

return 0;
}

void Proceduredemo ()
{
Environment *pev = NULL;
Connection *pconn = NULL;
Statement *pstmt = NULL;

Const string Username = "Demo";
Const string Password = "Demo";
Const string DATABASE = "ICSS";

Try
{
PEv = Environment::createenvironment ();
Pconn = pev->createconnection (username, password, database);

if (NULL = = Pconn)
{
cout << "Connection failure!" << Endl;
Return
}

pstmt = Pconn->createstatement ("BEGIN sp_selectspecial" (: 1,: 2,: 3); end; ");
Executing a stored procedure differs from executing an SQL statement: When executing an SQL statement, the createstatement argument is the Select parameter. While executing the stored procedure
The createstatement parameters are "BEGIN spname" (PARAM1, PARAM2 ...); End; ".


Pstmt-> setint (1, 43);
Pstmt-> Registeroutparam (2, occistring, 30, "");
Pstmt-> Registeroutparam (3, occistring, 30, "");
Pstmt-> Registeroutparam (4, Occinumber, 10, "");
Pstmt-> Registeroutparam (5,);

int updatecount = Pstmt->execute ();

cout << pstmt->getstring (2) << "" << pstmt->getstring (3) << Endl;

Pconn->terminatestatement (PSTMT);
Pev->terminateconnection (Pconn);
Environment::terminateenvironment (PEV);
}
catch (SQLException ex)
{
cout << ex.getmessage () << Endl;
}
}


void Functiondemo ()
{
Environment *PEVN = NULL;
Connection *pconn = NULL;
Statement *pstmt = NULL;

Const string Username = "Demo";
Const string Password = "Demo";
Const string DATABASE = "ICSS";

Try
{
PEVN = Environment::createenvironment ();
Pconn = pevn->createconnection (username, password, database);
if (NULL = = Pconn)
{
cout << "Connection failure!" << Endl;
Return
}
pstmt = Pconn->createstatement ("Begin:1:=fun_getsalary" (: 2); End; ");

Pstmt->registeroutparam (1, occiint,30, "");

Try
//{
Pstmt->setint (2,2);
//}
catch (SQLException ex)
//{
cout << ex.getmessage () << Endl;
//}

Pstmt->executeupdate ();
cout << pstmt->getint (1) << Endl;

}
catch (SQLException ex)
{
cout << ex.getmessage () << Endl;
}

}
*/

void Cursordemo ()
{
Environment *pev = NULL;
Connection *pconn = NULL;
Statement *pstmt = NULL;
ResultSet *prs = NULL;

Const string Username = "Demo";
Const string Password = "Demo";
Const string DATABASE = "ICSS";

Try
{
PEv = Environment::createenvironment ();
Pconn = pev->createconnection (username, password, database);

if (NULL = = Pconn)
{
cout << "Connection failure!" << Endl;
Return
}

   pstmt = pconn->createstatement ("BEGIN Sp_getspecialbyid (: 1,: 2,: 3,: 4,: 5); end; "); &NBSP
  //executing stored procedures differs from executing SQL statements: When executing an SQL statement, the createstatement parameter is a select parameter. When executing a stored procedure, the
  //createstatement parameter is "BEGIN spname (PARAM1, PARAM2 ...);".
                                                                           &NBSP
     
   pstmt-> setint (1);
   pstmt-> SetString (2, "");
   pstmt-> setstring (3, "");
   pstmt-> setint (4,-1);
   pstmt-> registeroutparam (5, occicursor);

int updatecount = Pstmt->execute ();

PRs = Pstmt->getcursor (5);
Prs->next ();
cout << prs->getstring (2) << Endl;

Pstmt->closeresultset (PRs);
Pconn->terminatestatement (PSTMT);
Pev->terminateconnection (Pconn);
Environment::terminateenvironment (PEV);
}
catch (SQLException ex)
{
cout << ex.getmessage () << Endl;
}
}

int main (int argc, char* argv[])
{
Proceduredemo ();
Selectdemo ();
Functiondemo ();
Cursordemo ();
return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.