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[@host[:port]]
Where
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:
"test,test@localhost,test@localhost:48004"
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:
connection->close();
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 {
stmt->execute("CREATE TABLE NAMES (ID INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, NAME STRING)");
connection->commit();
stmt->close();
printf("The table 'NAMES' was created.\n");
} catch (SQLException& xcp) {
connection->rollback();
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 |
---|---|
executeQuery() |
Executes a query and returns a pointer to a |
executeUpdate() |
Executes an SQL statement which doesn’t return any value. Returns an |
execute() |
Executes an SQL statement and returns a |