Database Operations Using C++ Driver

Connecting to a Database

In order to perform persistent work, you need to establish a connection to the NuoDB database. This is done by creating a Connection object as in the following example:

Connection* connection = Connection::create(
      "dbName",                // database
      "cloud",                 // username
      "user",                  // password
       1,                      // number of properties to follow
      "schema", "hello" );     // schema=hello property

The first argument, dbName is a string in the following format:



database_name is the name of the database.

host is the name or IP Address of the machine on which the NuoDB broker is running.

port is the port on which the NuoDB broker is listening.
IPv4 literal addresses are specified as a.b.c.d:port
IPv6 literal addresses are specified as [a:b:…]:port

You might want to specify more than one value for dbName, perhaps by varying the host and port. If you specify more than one value, the driver tries the dbName specified first. If the connection succeeds then the other specifications are not tried. If the first specification fails then the driver tries the second specification and so on. Use a comma to separate multiple specifications. For example:


If a connection is successful and later fails, there is no automatic retrying of any connections specified in the dbName argument.

The second argument is a string for the Database username.

The third argument is a string for the Database password.

The fourth argument is an integer that is a count for the number of properties that occur in the remaining arguments.

The remaining arguments are pairs of string arguments. Each string pair is used as a name/value property, where the first string in the pair is the property name and the second string in the pair is the property value. See Connection Properties for a list of valid properties.

Closing a Database Connection

When you are finished with the database connection, close the connection with this command:


Creating a Statement

For SQL statements that contain no parameters, you can use the Statement class. You can create a Statement object from the Connection object. The following code creates a new Statement object and uses it to create a table called NAMES:

Statement* stmt = connection->createStatement();
try {
    printf("The table 'NAMES' was created.\n");
} catch (SQLException& xcp) {
    printf("Caught exception %s\n", xcp.getText());

Creating Prepared Statements

To execute parameterized SQL queries, you need to create a PreparedStatement object from the Connection object. The following example selects NAME from the NAMES table that matches a particular ID parameter:

PreparedStatement* stmt =
    connection->prepareStatement("SELECT NAME FROM NAMES WHERE ID=?");
stmt->setInt(1, id);  // bind integer parameter.
ResultSet* rs = stmt->executeQuery();

Executing SQL Statements

There are three methods for executing SQL statements (with either Statement or PreparedStatement):

Method Description


Executes a query and returns a pointer to a ResultSet object that contains the data produced by the given query. The ResultSet object may be empty but the return value will never be NULL.


Executes an SQL statement which doesn’t return any value. Returns an int containing the number of rows updated by the SQL statement or 0 for SQL Data Definition Language (DDL) statements that return nothing.


Executes an SQL statement and returns a boolean, true if it has an associated ResultSet object and false if not.

Transaction Control

To commit persistent work, you should call the commit method on the Connection object:


To rollback persistent work, you should call the rollback method on the Connection object: