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 "tututil.h" //2
#include "conrep.h" //3
 
int main(int argc, char** argv) //4
{
associateStreams("t5in.dat", "t5out.txt", "t5err.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 customerRep(aDB, customerTable); //10
while (!inStream.eof()
&& !inStream.bad() && !inStream.fail()) { //11
unsigned long id; //12
inStream >> id; //13
if (inStream.eof()) //14
break; //15
customerRep.remove( id ); //16
outStream << "Removed customer with ID = "
<< id << " from the database." << endl; //17
}
 
closeStreams("t5in.dat", "t5out.txt", "t5err.txt");
return 0;
} //18
 
 
//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 serves as a handle to the database defined by arguments to the RWDBManager::database function.
//10 An instance of the class VVContactRepository representing the customer table 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 name that holds the customer information.
//11 The while loop cycles through the input file. The loop terminates at the end-of-file or when something untoward happens.
//12 Customer ID numbers are fetched one at a time from the input stream. This variable holds the ID number read from the stream.
//13 Fetches an ID number from the stream.
//14-15 If an attempt to read past the end-of-file is made, breaks out of the while loop.
//16 After successfully reading in the customer ID, the remove() member function of the VVContactRepository class is invoked. This function deletes the customer record from the customer table. The remove() routine is explained in VVContactRepository::remove.
//17 Once a customer has been removed from the database, a log message is created about the deletion.
//18 Destructors for all the objects are called here. The database closes automatically when its destructor is called.