user17276231
user17276231

Reputation:

How do I connect to an Oracle cloud database within a C++ application?

I have a cloud database with Oracle and I'm trying to learn how to execute SQL commands within my c++ application in Windows.

Here's what I've tried.

1) Using Instant Client (OCCI)

    Environment* env;
    Connection* conn;

    env = Environment::createEnvironment(Environment::DEFAULT);
    conn = env->createConnection ("username", "password", "(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp) (HOST=myserver111) (PORT=5521))(CONNECT_DATA = (SERVICE_NAME = bjava21)))");
    
    env->terminateConnection(conn);
    Environment::terminateEnvironment(env);

2) Using ODBC

3) Using Oracle Developer Tools for Visual Studio

Update

createConnection("username", "password", "(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp) (HOST=myserver111) (PORT=5521))(CONNECT_DATA = (SERVICE_NAME = bjava21)))")
createConnection("username", "password", "//host:[port][/service name]")
createConnection("username", "password", "xxx_low")
createConnection("username", "password", "protocol://host:port/service_name?wallet_location=/my/dir&retry_count=N&retry_delay=N")
createConnection("username", "password", "username/password@xxx_low")

Error While Debugging:

Unhandled exception in exe: Microsoft C++ exception: oracle::occi::SQLException at memory location

Full Code

#include <occi.h>
using namespace oracle::occi;

int main() {
    Environment* env;
    Connection* conn;

    env = Environment::createEnvironment(Environment::DEFAULT);
    conn = env->createConnection("username", "password", "(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp) (HOST=myserver111) (PORT=5521))(CONNECT_DATA = (SERVICE_NAME = bjava21)))");

    env->terminateConnection(conn);
    Environment::terminateEnvironment(env);

    return 0;
}

Upvotes: 0

Views: 1130

Answers (1)

Christopher Jones
Christopher Jones

Reputation: 10711

With Instant Client in general:

  • don't set ORACLE_HOME. This can have side effects.
  • you don't need to set TNS_ADMIN since you put the unzipped network files in the default directory.

For cloud:

  • In the app, use one of the network aliases from the tnsnames.ora file (eg. xxxx_low). You can see the descriptors have extra info that your hard coded descriptor is missing.

ODBC will be exactly the same. Once you have the wallet files extracted to the default network/admin subdirectory you just need to connect with the DB credentials and use a network alias from the tnsnames.ora file.

More info is in my blog post How to connect to Oracle Autonomous Cloud Databases.

Official doc is in Connect to Autonomous Database Using Oracle Database Tools

Upvotes: 1

Related Questions