Skip to Main Content

Programming Languages & Frameworks

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Using C++ and OCCI to connect to a external Oracle server fails

Tyson OswaldJan 16 2026 — edited Jan 16 2026

I am using oraocci12 12.2.0.1 and am experiencing an odd issue. When attempting to connect to an external database I get ORA-12560 TNS Protocol Error, when looking at the trace file, it is trying to connect to BEQ, and using the current Windows user's username. I also get the same result using oraocci23 and occi21.

Question I have is does occi only connect to BEQ?

I am using the Environment object o connect. Similar to the examples provided in the documentation.

Here is my code, the error is throw at the line: con = env->createConnection(user, pwd, connectString);

#include <iostream>
#include <string>
#include <occi.h>
#include "MaskedInput.h"

#define	USERNAME_LEN 10
#define PASSWORD_LEN 25
#define CONSTR_LEN 1024

using namespace std;
using namespace oracle::occi;

// prototypes
void connect();
string getParameter(string& destination, const char* prompt, const int maxLength, const bool mask);

int main()
{
	connect();
}

void connect()
{
	Environment* env = NULL;
	Connection* con = NULL;
	string user = "f";
	string connectString;
	
	try
	{
		std::string pwd;

		getParameter(connectString, "Enter the connection string: ", CONSTR_LEN, false);
		getParameter(user, "Enter your username: ", USERNAME_LEN, false);
		getParameter(pwd, "Enter your password: ", PASSWORD_LEN, true);

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

		int majorVersion = 0;
		int minorVersion = 0;
		int updateNum = 0;
		int patchNumber = 0;
		int portUpdateNum = 0;

		env->getClientVersion(
			majorVersion,
			minorVersion,
			updateNum,
			patchNumber,
			portUpdateNum);

		cout << "Client version: "
			<< majorVersion << '.'
			<< minorVersion << '.'
			<< updateNum << '.'
			<< patchNumber << '.'
			<< portUpdateNum
			<< endl;

		try
		{
			cout << "Attempting to connect to " << connectString << endl;

			con = env->createConnection(user, pwd, connectString);

			cout << "Connected" << endl;

			auto stmt = con->createStatement("select * from names");
			if (stmt)
			{
				auto rs = stmt->executeQuery();
				if (rs)
				{
					while (rs->next())
					{
						cout << rs->getString(1) << endl;
					}
				}

				con->terminateStatement(stmt);
				stmt = NULL;
			}
			cout << "terminating connection" << endl;
			env->terminateConnection(con);
			cout << "terminating environment" << endl;
			Environment::terminateEnvironment(env);

		}
		catch (SQLException& ex)
		{
			if (con)
				env->terminateConnection(con);
			if (env)
				Environment::terminateEnvironment(env);

			cout << "Connecting to database caused an error:\n" << ex.what() << endl;
		}
		con = NULL;
		env = NULL;
	}
	catch (SQLException& ex)
	{
		cout << "Creating environment caused an error:\n" << ex.what() << endl;
	}
}

string getParameter(string& destination, const char* prompt, const int maxLength, const bool mask)
{
	if (mask)
	{
		while (destination.length() == 0)
		{
			destination = MaskedInput().Get(prompt, maxLength);
		}
	}
	else
	{
		while (destination.length() == 0)
		{
			cout << prompt;
			cin >> destination;
			destination = destination.substr(0, maxLength);
		}

	}

	return destination;
}

EDIT: I am able to connect using the same connection string in SQL Developer without issue so I know the parameters are correct.

thanks,

Tyson

Comments
Post Details
Added on Jan 16 2026
4 comments
93 views