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
executeQuery()
is a general-purpose method that interacts with the database to perform a
database operation. The database operation to be performed must be specified to this
method, using a Structured Query Language (SQL) statement. The result of the SQL execution
is returned as a ResultSet
object. The ResultSet
object contains a table that contains the
query results. The individual table rows can be retrieved using the next()
method from the
ResultSet
class.
Several examples in the book use a database in the business logic. The database schemas for these databases are given below for easy reference.
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 |
---|---|---|
|
Text |
Primary key |
|
Text |
Employee name |
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 |
---|---|---|
|
Text |
Primary key |
|
Text |
Fund name |