|
[Help | API Docs | Short Course| Exercises] Management at the 4J Cafe have informed Hal that their bosses like the JJJJData information, but want to see the days of the week in full, that is Monday as "Monday" and not "Mon". But, 4J Cafe management also wants reports in the old format. In addition, they've decided that "Jus'Joe" is clearly a superior name to "JustJoe" and any columns with the old value should be changed. Duane and Chrissie are very happy to be involved in such a productive project, and, whistling while they work, they review the tasks ahead. Notice that this program changes the structure of the JJJJData table. Even so, all of the other exercise applications still work properly. Of course, they will not pick up the new data without changing SELECT or UPDATE statements ( after all, there is no magic, ) BUT, this is a powerful demonstration of the value of data independence. PrerequisitesSkeleton CodeTasksThe application can use the ConnectU.properties file. Duane decides to set up two arrays, one containing "Mon" through "Sun", the other containing "Monday" through "Sunday" ( to match the old and load the new data, ) and use two prepared statements. This work is done for you. Your job is to set up a query String to select all columns for reporting purposes, and two update Strings for the prepared statements. One String will be used to update the new column ( LDOW ) with a corresponding value from the second array when the DOW column contains a value from the first array. The other String will be used to update the column Type from "JustJoe" to "Jus'Joe". The program goes through the familiar steps of accessing the ResourceBundle and retrieving the driver name, databaseURL, userID and password; loading the driver, getting a Connection and creating a Statement. Your job: add the new column -- the SQL statement should look like this: ALTER TABLE JJJJData ADD COLUMN LDOW VARCHAR (9) -- and print that the column was successfully added. Prepare both PreparedStatements. Now the LDOW column needs to be loaded. The program has two corresponding arrays, asDOW and asLDOW, set up and the int ndx defined for use as an array index. Your job: create a loop in which, for each element of the asDOW array, you set the first parameter of pstmt1 to the current index value of asLDOW and the second parameter to the current index value of asDOW ( the relevant portion of the UPDATE statement looks like SET LDOW = ? WHERE DOW = ? ) Next, perform the update and accumulate the number of rows updated for reporting, once the loop is done. Now it is time to update the Type column if it contains "JustJoe". Set the first parameter of pstmt2 to "Jus'Joe" and the second parameter to "JustJoe" ( the relevant portion of the UPDATE statement looks like SET Type = ? WHERE Type = ? ) Next, perform the update, get and report the number of rows updated. Get and report all columns for all rows of JJJJData, showing the new data. Where help exists, the task numbers above are linked to the step-by-step help page. Solution SourceDemonstrationBefore PrepareAlt is run, the data in the JJJJData table is structured and has values as shown in A Complete Example. After adding the LDOW column and updating the data as discussed in the excercise, PrepareAlt should report ( and the tables should contain ) the data as follows:
Next ExerciseExercisesShort CourseCopyright 1996-2000 jGuru.com. All Rights Reserved. | |||||
|
| ||||||||||||