The Main Routine
This code sample shows how to use the classes VVContact and VVContactRepository. 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("", "t1out.txt", "t1err.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, customerTableName); //10
customerPool.mailingLabels(outStream); //11
 
closeStreams("", "t1out.txt", "t1err.txt");
return 0;
} //12
 
Here is a line-by-line description of the program:
//1 Include the declarations for the DB Interface Module classes used in this program.
//2 Include the declaration for the class VVContactRepository.
//3 Include the declarations for the utility routines commonly used by all the tutorials.
//4 Start of the main routine of the tutorial. It accepts database connection arguments which can be used to override the defaults.
//5 Again, because of cross-platform development issues, we decided that all input and output for these tutorials should take place to and from files. This is because Windows applications do not necessarily have access to the standard cin, cout, and cerr streams. This function allows file names to be associated with the three streams. The first argument represents input to the program. Since no input from a theoretical user is necessary in this program, an empty string is offered. The second argument is the stream for standard output. The mailing labels go out to this file. The third argument is for error messages. Although associateStreams() is not part of the DB Interface Module, it is provided for cross-platform portability of these tutorials.
//6 This line sets up an error handler. If this program is running, the database reports an error while this program is running the function pointed to by outputStatus() is invoked. The function outputStatus() is in tutdefs.h. It checks the severity of the error and decides if the program should be aborted after it sends the error details to a file specified on the previous line.
//7 Here, six RWCString instances are declared. They will hold information used in logging into the database server.
//8 The routine called here places values into the RWCString instances declared on the preceding line. The user of this program can override the default values on the command line. This routine hides the complexity of the parsing. The function initializeDatabaseArguments() is not part of the DB Interface Module and exists only for the convenience of these tutorials. You can find the definitions in the tututil.h include file..
//9 Here an actual connection to a database server is established. The variable aDB will contain the means to access 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 The class VVContactRepository contains several useful member functions. The one invoked on this line spools mailing labels out for all customers to the stream specified as the argument.
//12 Destructors of all the objects are called here. The database closes automatically as its destructor is called.
This program is really quite short, considering all that it accomplishes. Lines //10 and //11 do most of the work. A detailed examination of the code behind these lines is important for understanding how the DB Interface Module is best used.