The Main Routine
The following is the main routine for the tutorial. The line numbers correspond to the comments that follow the code.
 
#include <rw/db/db.h> //1
#include "conrep.h" //2
#include "tututil.h" //3
int main(int argc, char** argv) //4
{
associateStreams("t3in.dat", "t3out.txt",
"t3err.txt"); //5
RWDBManager::setErrorHandler(outputStatus); //6
RWCString serverType, serverName, userName,
password, databaseName, pstring; //7
initializeDatabaseArguments(argc, argv, serverType,
serverName, userName, password,
databaseName, pstring); //8
RWDBDatabase aDB = RWDBManager::database
(serverType, serverName, userName, password,
databaseName, pstring); //9
VVContactRepository customerPool(aDB, customerTable); //10
VVContact aCustomer; //11
while (aCustomer.read(inStream)) { //12
customerPool.insert(aCustomer); //13
outStream << aCustomer.name()
<< " has been added to the customer table."
<< endl; //14
}
closeStreams("t3in.dat", "t3out.txt", "t3err.txt");
return 0;
} //15
 
//1-8 These lines are for initialization. They are common to all the tutorials and are explained in the comments in The Main Routine.
//9 Here a connection to a database server is established. The variable aDB will serve as a handle to the database defined by arguments to the RWDBManager::database function.
//10 An instance of the class VVContactRepository is created on this line. The first argument, aDB, identifies the database in which the instance’s data resides. The second argument identifies the specific table that holds the customer information.
//11 An instance of VVContact is created here to hold the information about a new customer.
//12 In this while loop, new customers are read one at a time from the input stream. The input stream is tied to the file t3in.dat. This association is defined on line //5 using the associateStreams() utility method. When no more new customers are available on the input stream, the read routine returns false, and the loop terminates.
//13 This line invokes the insert() member function of the class VVContactRepository. This function takes the customer record as an argument and inserts it into the customer table. The code of the insert() method is described below.
//14 Once a customer record is added to the customer table, the name of the customer, along with a message, is printed to the output stream. Why isn’t there any error checking here? If an insert fails, the error handler in //6 above is called. This routine outputs an error message to the error stream and terminates the program.
//15 Destructors for all the objects are called here. The database closes automatically when its destructor is invoked.