Aqua Phoenix
     >>  Lectures >>  Java 6  
 

Navigator
   
 
       
   

6.8 Homework Assignment 6

Due date and time: November 2, 2005 at 11:59:59pm (midnight)

Points: 20

This homework focuses on File I/O, and examples from SQL. A crucial feature of any data-driven application is preservation of state, whether through ubiquitous file management or database interaction. You will be modifying your previous work on creating a User Interface and functionality for an Employee Manager to include file handling.

For logistical reasons, the homework is split into 3 parts. The individual parts are outlined in some detail. At this point of you Java career, you should have a good feel for some of the design decisions that are necessary to successfully implement an idea.

6.8.1 Part A - Saving your database

You will extend your UI to include an additional Button to save the current data structure to a file. It is up to you to decide where to place the button.

Upon hitting the Save button, you should save the contents of your data structure (Hashtable) to a text file. You will have to think about how to save each person's credentials as well as account information.

For simplicity, you can hard-code the filename in your code.

Hints:

  • Each person should appear on one line.
  • Decide on some common structure to follow. I would suggest using CSV (comma separated values).
  • You may want to make the first field of each line the type of Person (Professor, Student, Support Staff). Perhaps you would like to encode each with a number (0, 1, and 2, respectively). When reading the file (Part B), you can then decide depending on the value of the first field how to read the remaining fields and what kind of object needs to be created.
  • For example, a line for a Professor may look something like this:

    0,John,Smith,Department of Basket Weaving,...,...

You can test your implementation by opening the saved file in a text editor.

6.8.2 Part B - Opening your database

Now that you have saved your data, you will implement functionality to read that file and re-create the data structure. Add another Button to your User Interface.

Upon pressing the Open button, you should open the file, whose name you chose previously, read line-by-line, and put each person into the data structure.

Hints:

  • Each line should be parsed using the StringTokenizer. Given that all data fields are separated by commas, the StringTokenizer should return each field separately.
  • You can assume that commas will not be used in any input fiels in the UI. If you did use commas, this would obviously skew the order and the contents of fields in the saving/opening proces.
  • When reading from a file, you should first empty out the entire data structure in memory. This way, you can assure that each file read starts with an empty data structure, and no old data remains in memory.

You can test your implementation by saving some test entries, close your interface, open the interface, and read the previously save file.

At this point you should have a fully functional interface, with which you can keep track of data, keep track of that data visually, and save/restore state.

6.8.3 Part C - Reading from a Database source

You will now implement functionality to read data from another external data source. You can use this step to get a decently-sized database for testing your file I/O. Add another button to your interface for reading from a student database.

You should closely follow the examples given in class to implement database functionality. Here are a few highlights, all of which are illustrated in lecture.

  • You must download and include mysql.jar in your classpath.
  • You must import java.sql.*.
  • You should copy/paste the 4 private methods from the in-class example. They are used to open/close connections/statements, and can be handy in minimizing your code.
  • Copy the section that creates the SQL statement, and iterates through the results.

You will use a pre-defined SQL statement to select a number of fields that you should use to create new objects, which should be placed into your data structure.

The SQL statement to select fields we are interested in is as follows:

SELECT firstname,lastname,dept,affiliation FROM users;
Hints/Suggestions:
  • The table from which you are selecting data contains only students. For simplicity, this step of importing data from a database is only applicable to students, and hence the only objects you have to create are objects of class Student.
  • The field dept in the database table is equivalent to the field Major in your interface
  • The field affiliation in the database table is equivalent to the field School in your interface
  • There is no information regarding the year. Assume that all student in the database will be graduating in 2006.

After you have read the table entries from the database, your interface should include 690 users from that table. While there are students with the same firstname or the same last name, there are no duplicates in terms of first name AND last name pairs.

Depending on how you have implemented your Hashtable data structure, you may need to rethink the way you are using keys. For instance, if your key is ONLY first name or ONLY last name, then you will run into problems storing all 690 students. However, if your key is firstname AND lastname (with a space in-between), then you will have no problem, since "firstname lastname" is a unique key in the table of data you are working with. Simply stated, you SHOULD make your Hashtable key the combination of firstname and lastname.

Once your interface contains all student records, you should be able to save the state of your data structure, and end up with a large file of students with empty accounts.

6.8.4 Submission instructions

Use the submit script to submit your assignment (instructions on class web site).