Example 7.2 shows the Java code for the DatabaseItem interface. This base class provides slots for a global java.sql.Connection object and for a validity flag. The global Connection is used for all transactions made by the data objects with the database to retrieve or update their data. The DatabaseItem interface also includes two abstract methods: updateToDbase() and updateFrom-Dbase(). These have to be implemented in subclasses to perform the necessary database transactions to store the object’s data to the database, and to retrieve the object’s current data from the database, respectively. Anytime during the lifetime of a DatabaseItem, its updateFromDbase() method can be called to refresh the local data from the database, or its updateToDbase() method can be called to update the database with the local data.
With these data objects defined, we can now use them in several distributed application contexts to access local and remote schedule databases. If the schedule database is local, then we simply need to have the JDBC drivers for the DBMS on our class path when we run an application built against these objects. The application will create a java.sql.Connection object with the name of the local database and use it to initialize all schedule-related objects. If we want to put the database on a remote server, then we have two ways to connect these data objects to the database. If the native DBMS interface provides a network interface and if the JDBC drivers we are using support this vendor-specific network interface, then we can install the DBMS network interface and modify the URL used to create the JDBC connection to the database to reflect the remote location. If this is not an option, either because the DBMS has no network interface, or because we cannot use the DBMS network interface on the client machine for some reason, then we have the option of using JDBC drivers with their own network protocol (as we discussed in an earlier section). The database host machine would need to have the corresponding server drivers installed, including the drivers that speak the native DBMS interface protocol.