Skip to content

Instantly share code, notes, and snippets.

@cyshallchan
Created June 9, 2020 06:05
Show Gist options
  • Save cyshallchan/c9c7dfb2a0e5db8e3eb3b3cda3968191 to your computer and use it in GitHub Desktop.
Save cyshallchan/c9c7dfb2a0e5db8e3eb3b3cda3968191 to your computer and use it in GitHub Desktop.
Simple example to connect to oracle database with occi
/*------------------------------------------
Name: oradb.cpp
Date: 2015
Auth: Sun Dro
Desc: Simple example to connect to oracle
database with occi.
------------------------------------------*/
#include <iostream>
#include <occi.h>
#include "fcgio.h"
using oracle::occi::Environment;
using oracle::occi::Connection;
using namespace oracle::occi;
using namespace std;
int main(void)
{
/* OCCI Variables */
Environment *env;
Connection *conn;
Statement *stmt;
ResultSet *rs;
/* Used Variables */
int num;
string str;
string user = "username";
string pass = "password";
string constr = "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1515))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=testdb.test)))";
/* Stream buffers */
streambuf * cin_streambuf = cin.rdbuf();
streambuf * cout_streambuf = cout.rdbuf();
streambuf * cerr_streambuf = cerr.rdbuf();
/* Initialise Fast-SGI */
FCGX_Request request;
FCGX_Init();
FCGX_InitRequest(&request, 0, 0);
/* Open Oracle Environment and Connect to Database */
env = Environment::createEnvironment(Environment::DEFAULT);
conn = env->createConnection(user, pass, constr);
while (!FCGX_Accept_r(&request))
{
fcgi_streambuf cin_fcgi_streambuf(request.in);
fcgi_streambuf cout_fcgi_streambuf(request.out);
fcgi_streambuf cerr_fcgi_streambuf(request.err);
/* Create Statement and Execute Query */
try {
stmt = conn->createStatement("BEGIN PKG_WEBTEST.prc_params(:1, :2, :3, :4); END;");
stmt->setInt (1, 10);
stmt->setString (2, "IN");
stmt->registerOutParam (3, OCCINUMBER);
stmt->registerOutParam (4, OCCISTRING, 80, "");
rs = stmt->executeQuery();
/* Save Returned Values */
num = stmt->getNumber (3);
str = stmt->getString (4);
/* Print Returned Values */
cout << "Content-type: text/plain\r\n"
<< "\r\n"
<< "Val 1: " << num << " Val 2: " << str << endl;
/* Close Result and Terminate Statement */
stmt->closeResultSet(rs);
conn->terminateStatement(stmt);
}
catch (SQLException &sqlExcp) {
cout <<sqlExcp.getErrorCode() << ": " << sqlExcp.getMessage();
}
cin.rdbuf(&cin_fcgi_streambuf);
cout.rdbuf(&cout_fcgi_streambuf);
cerr.rdbuf(&cerr_fcgi_streambuf);
}
/* Disconnect from Database and Terminate Environment */
env->terminateConnection(conn);
Environment::terminateEnvironment(env);
cin.rdbuf(cin_streambuf);
cout.rdbuf(cout_streambuf);
cerr.rdbuf(cerr_streambuf);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment