Examples of Using C++ Driver
A C++ sample NuoDB application can be found in NUODB_HOME/samples/doc/cpp
. The application is called HelloDB
and it incorporates the code from the examples in Database Operations Using C++ Driver.
The application can be built by using the platform’s corresponding build script:
Platform | Script |
---|---|
Linux |
|
Windows |
|
HelloDB
will open a database whose name is passed in as a command line argument. The constructor creates a connection to the database as user “dba
” and password “goalie
”. The createTable()
method creates a table called NAMES
, with two columns, an integer ID
that is always generated and a string NAME
. The addNames()
method generates names and adds them to the NAMES
table. The method getName()
queries the NAMES
table to get a NAME
based on the given ID
.
Here is the include file, HelloDB.h
, used by HelloDB.cpp
below:
HelloDB.h
/*
* A simple class that connects to a NuoDB server. It uses some of the
* basic connection methods to create a table and insert or fetch rows.
*/
class HelloDB
{
public:
HelloDB(const char* dbName) throw (SQLException);
~HelloDB();
void createTable() throw (SQLException);
void addNames() throw (SQLException);
const char* getName(int id) throw (SQLException);
private:
Connection* connection;
};
HelloDB.cpp
Here is the full HelloDB.cpp
example.
/*
* An introductory program that shows how to establish a connection with
* a NuoDB server and execute a few basic SQL commands.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "NuoDB.h"
using namespace NuoDB;
#include "HelloDB.h"
/*
* Creates an instance of HelloDB connected to the database with
* the given name on the localhost. This example class uses the
* default testing name & password.
*/
HelloDB::HelloDB(const char* dbName) throw (SQLException)
{
connection = Connection::create(
dbName,
"dba", // username
"goalie", // password
1, // number of properties
"schema", "hello" ); // schema=hello property
}
/*
* Closes the connection.
*/
HelloDB::~HelloDB()
{
connection->close();
}
/*
* Creates a testing table named "names" which maps integer identifiers
* to names.
*/
void HelloDB::createTable() throw (SQLException)
{
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("The table 'NAMES' already exists, re-using it.\n");
}
}
/*
* Adds an identifier-to-name mapping in the "names" table.
*/
void HelloDB::addNames() throw (SQLException)
{
PreparedStatement* stmt = connection->prepareStatement(
"insert into names (name) values (?)",
NuoDB::RETURN_GENERATED_KEYS );
try {
for (int i = 1; i <= 15; i++) {
char name[100];
sprintf(name, "Fred # %d", i);
stmt->setString(1, name);
int updateCount = stmt->executeUpdate();
}
ResultSet* resultSet = stmt->getGeneratedKeys();
ResultSetMetaData* resultSetMetaData = resultSet->getMetaData();
const char* columnName = resultSetMetaData->getColumnName(1);
while (resultSet->next()) {
printf(
"New id=%d for column %s\n",
resultSet->getInt(1),
columnName );
}
connection->commit();
resultSet->close();
stmt->close();
} catch (SQLException& xcp) {
connection->rollback();
throw;
}
}
/*
* Gets the name mapped to the given identifier. Returns NULL if there
* is no such identifier in the table.
*/
const char* HelloDB::getName(int id) throw (SQLException)
{
PreparedStatement* stmt =
connection->prepareStatement("select name from names where id=?");
stmt->setInt(1, id);
ResultSet* rs = stmt->executeQuery();
char* name;
if (rs->next()) {
name = new char[strlen(rs->getString(1)) + 1];
strcpy(name, rs->getString(1));
} else {
name = NULL;
}
rs->close();
stmt->close();
return name;
}
int main(int argc, char** argv)
{
if (argc != 2) {
printf("Usage: %s <dbname>\n", argv[0]);
return -1;
}
try {
HelloDB helloDB(argv[1]);
helloDB.createTable();
helloDB.addNames();
const char* name = helloDB.getName(12);
printf("The NAME with id of 12 is '%s'\n", name);
delete[] name;
} catch (SQLException& xcp) {
printf("Got exception: %s\n", xcp.getText());
return -1;
}
return 0;
}