Three-tier applications have gained considerable attention in recent years , largely because of the demand for more scalable and load-balanced systems to replace traditional two-tier client/server database systems. A centralized database system does not just handle data access, it also processes the business rules on data. Thus, a centralized database is usually heavily loaded because it requires extensive data manipulation and processing. In some situations, data processing is handled by the client and business rules are stored on the client side. It is preferable to use a middle tier as a buffer between client and database. The middle tier can be used to apply business logic and rules, and to process data to reduce the load on the database.
A three-tier architecture does more than just reduce the processing load on the server. It also provides access to multiple network sites. This is especially useful to Java applets that need to access multiple databases on different servers, since an applet can only connect with the server from which it is downloaded.
To demonstrate , let us rewrite the example in §36.3.1, "Example: Retrieving Student Scores from an RMI Server," to find scores stored in a database rather than a hash map. In addition, the system is capable of blocking a client from accessing a student who has not given the university permission to publish his/her score. An RMI component is developed to serve as a middle tier between client and database; it sends a search request to the database, processes the result, and returns an appropriate value to the client.
For simplicity, this example reuses the StudentServerInterface interface and StudentServerInterfaceClient class from §36.3.1 with no modifications. All you have to do is to provide a new implementation for the server interface and create a program to register the server with the RMI. Here are the steps to complete the program:
1. | Store the scores in a database table named Score that contains three columns : name , score , and permission . The permission value is 1 or 0, which indicates whether the student has given the university permission to release his/her grade. The following is the statement to create the table and insert three records: create table Scores (name varchar( 20 ), score number , permission number); insert into Scores values (' John ', 90.5 , 1 ); insert into Scores values (' Michael ', 100 , 1 ); insert into Scores values (' Michelle ', 100 , ); | |
2. | Create a new server implementation named Student3TierImpl in Listing 36.5. The server retrieves a record from the Scores table, processes the retrieved information, and sends the result back to the client. Listing 36.5. Student3TierImpl.java(This item is displayed on pages 1253 - 1255 in the print version)
The table named Scores consists of three columns, name , score , and permission , where permission indicates whether the student has given permission to show his/her score. Since SQL does not support a boolean type, permission is defined as a number whose value of 1 indicates true and indicates false . The initializeDB() method (lines 27 “49) loads the appropriate JDBC driver, establishes connections with the database, and creates a prepared statement for processing the query. The findScore method (lines 54 “75) sets the name in the prepared statement, executes the statement, processes the result, and returns the score for a student whose permission is true . | |
3. | Write a main method in the class RegisterStudent3TierServer (Listing 36.6) that registers the server object using StudentServerInterfaceImpl , the same name as in Listing 36.2, so that you can use StudentServerInterfaceClient, created in §36.3.1, to test the server. Listing 36.6. RegisterStudent3TierServer.java
| |
4. | Follow the steps below to run this example.
|