Appendix F. Database Wrapper Utility

This book uses several examples that use a database in the business logic. This database access is provided through the Java Database-Open Database Connectivity (JDBC-ODBC) bridge. The Data Source Name (DSN) entries required for this are explained in Appendix E. The database access logic required in all these examples is similar and is packaged separately in the DatabaseWrapper class. This class is in the Shared directory.

The constructor for this class requires three parameters — the DSN name, as registered with the operating system, user name, and password. Both the user name and the password for all the databases in the book are set to null.

Example . DatabaseWrapper.java

public DatabaseWrapper(String database, String usrName, 
                                           String password) 
{ 
   dbName = database; 
   dbUserName = usrName; 
   dbPassword = password; 
} 

The method connectToDb() has the logic to connect to the database. The method DatabaseWrapper.java executeQuery() is a general-purpose method that interacts with the database to perform a DatabaseWrapper.java database operation. The database operation to be performed must be specified to this DatabaseWrapper.java method, using a Structured Query Language (SQL) statement. The result of the SQL execution DatabaseWrapper.java is returned as a ResultSet object. The ResultSet object contains a table that contains the DatabaseWrapper.java query results. The individual table rows can be retrieved using the next() method from the DatabaseWrapper.java ResultSet class.

Several examples in the book use a database in the business logic. The DatabaseWrapper.java database schemas for these databases are given below for easy reference.

Employee Contribution Database

The Employee Contribution database, with DSN name Emp401k, consists of two tables. The table EmpTable contains information about employees within a company. The schema for this table in shown in Table F.1, followed by a sample entry in Table F.2.

Table F.1. Schema for Table EmpTable

Field Name

Type

Description

EmpID

Text

Primary key

EmployeeName

Text

Employee name

Table F.2. Sample Entry for Table EmpTable

EmpID

EmployeeName

1001

Carl Cooper

The table FundTable contains information about the mutual funds that are available for investing. The Table F.3 depicts the schema for the table followed by a sample entry in Table F.4. Similarly, the table EmpContrib information about employee contribution percentages for participating employees. The fields in this table are related to employee and fund tables through foreign key relationship. Table F.5 shows the schema for this table. A sample entry is provided in Table F.6.

Table F.3. Schema for Table FundTable

Field Name

Type

Description

FundID

Text

Primary key

FundName

Text

Fund name

Table F.4. Sample Entry for Table FundTable

FundID

FundName

24675

Growth and Income Fund

Table F.5. Schema for Table EmpContrib

Field Name

Type

Description

EmpID

Text

Foreign key

FundID

Text

Foreign key

ContribPercent

Number

Contribution percentage

Table F.6. Sample Entry for Table EmpContrib

EmpID

FundID

ContribPercent

1001

24675

50

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset