lab-2) develop a java application

43
Instructor Exercises Guide Course materials may not be reproduced in whole or in part without the prior written permission of IBM. © Copyright IBM Corp. 2004, 2007 Exercise 1. Develop a Java Application 1-1 V4.1 EXempty Exercise 1. Develop a Java Application Estimated time 01:00 What this exercise is about This exercise introduces the Java development features in IBM® Rational® Application Developer V7 and later. The IBM Eclipse 3.2 platform, the basis for Rational Application Developer, provides the Java™ development tools demonstrated in this lab. What you should be able to do At the end of the lab, you should be able to: Import an existing Java project into Rational Application Developer from a project interchange file Create the Library database and populate it using an application from the imported project Use the basic functions of the Java editor and associated views Perform rename and move refactoring functions Organize import statements in a Java class Visualize the relationship between classes using a UML diagram Customize the Java development tool settings through the workbench properties Introduction The Library case study provides a simplified model of an item and patron management system. In this lab, you import a Java application that creates and populates database tables needed by the case study. Running the SetupLibraryDatabase class creates a new Apache Derby database with sample patron and item information. The second class, DisplayPatron, retrieves a list of registered library users from the database.

Upload: prashant-jain

Post on 14-Sep-2015

6 views

Category:

Documents


1 download

DESCRIPTION

Java

TRANSCRIPT

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    Copyright IBM Corp. 2004, 2007 Exercise 1. Develop a Java Application 1-1

    V4.1

    EXempty Exercise 1. Develop a Java Application

    Estimated time01:00

    What this exercise is aboutThis exercise introduces the Java development features in IBM Rational Application Developer V7 and later. The IBM Eclipse 3.2 platform, the basis for Rational Application Developer, provides the Java development tools demonstrated in this lab.

    What you should be able to doAt the end of the lab, you should be able to: Import an existing Java project into Rational Application Developer

    from a project interchange file Create the Library database and populate it using an application

    from the imported project Use the basic functions of the Java editor and associated views Perform rename and move refactoring functions Organize import statements in a Java class Visualize the relationship between classes using a UML diagram Customize the Java development tool settings through the

    workbench properties

    IntroductionThe Library case study provides a simplified model of an item and patron management system. In this lab, you import a Java application that creates and populates database tables needed by the case study. Running the SetupLibraryDatabase class creates a new Apache Derby database with sample patron and item information. The second class, DisplayPatron, retrieves a list of registered library users from the database.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    1-2 Developing Web Applications Copyright IBM Corp. 2004, 2007

    RequirementsThis exercise requires the LibraryDatabaseSetup.zip file. Make sure that these files appear in the C:\LabFiles70\Java\setup directory before starting the lab.

    Instructor exercise overviewIn this exercise, students build the Library database. The exercise demonstrates how to create the database in the Derby environment only. Other RDBMS products may be substituted for Derby; however it is up to the instructor to explain the process of building the library database. The databaseInformation.properties resource bundle should contain all of the parameters that need to be changed to use a different RDBMS.Information about Derby and about the adaptation of this material to other database management systems is included at the end of this chapter.The Java project created in this exercise is only used to populate the database. Although a solution to this exercise can be found in C:\LabFiles70\Java\solutions\complete\exercise01, it is more effective to simply unzip the C:\LabFiles70\Java\solutions\complete\exercise01\database.zip file to C:\LabFiles70 folder.TODO: Fix the next few paragraphsThis exercise allows developers to experiment with the Java development tools (JDT) within IBM Rational Application Developer V7. Application Developer inherits these tools from the IBM Eclipse 3.0 platform. The exercise instructions assume that the developers are comfortable with Java application development. Prior experience with a graphical integrated development environment (IDE) would be helpful to students but not necessary.The first part of this exercise allows developers to become familiar with the concept of a workspace, projects and resources. Before importing Java code, developers must create a new project to hold the source code and compiled classes.After importing the Java application into a project, students examine and modify the application through the Java perspective. Some of the steps were intentionally placed to demonstrate the source and refactoring features of the JDT.The latter portion of this exercise provides an opportunity for the students to compile and run the application. You should stress during the review session that the workbench automatically compiles Java code. Selecting the clean command is not necessary in most instances.The last section of this exercise uses the UML visualization tools to map the relationship between classes in the library setup application. Students can modify changes in the diagram to commit actual changes to the code itself. However, this feature is not demonstrated due to time constraints.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    Copyright IBM Corp. 2004, 2007 Exercise 1. Develop a Java Application 1-3

    V4.1

    EXempty There are two main purposes to this lab. First, this lab encourages students to become comfortable with using the workbench. Second, the Java application creates the database used in later labs.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    1-4 Developing Web Applications Copyright IBM Corp. 2004, 2007

    Exercise instructions

    PrefaceOpen a new workspace and examine the code from an imported project interchange .zip file using the Java development tooling (JDT) features in IBM Rational Application Developer V7 and later.After examining the application, run the setup class SetupLibraryDatabase to create the database used in upcoming labs. Examine the database contents using a Java class. Finally, create an UML diagram to visualize the relationship between classes.The following references in the exercise instructions represent directory locations in your workstation:

    : C:\LabFiles70 : c:\IBM\SDP70

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    Copyright IBM Corp. 2004, 2007 Exercise 1. Develop a Java Application 1-5

    V4.1

    EXempty 1.1. Prepare the WorkspaceA workspace is a way to group resources, using a folder structure. You may be more familiar with other tools, that use .project or .prj files to specify and link resources together. All resources belong to projects, which are resources themselves. For these exercises, the workspace will be comprised of a collection of projects that hold the source code and configuration settings for the library applications. Before you begin working with a new application, create a new workspace to hold the applications files.__ 1. Start Rational Application Developer with a new workspace.

    __ a. Select Start Programs IBM Rational Software Delivery Platform IBM Rational Application Developer IBM Rational Application Developer.

    __ b. Enter C:\LabFiles70\workspace as the new workspace directory. Be careful: do not select the Use this as the default and do not ask again check box.

    __ c. When Rational Application Developer opens, close the Welcome page by clicking on the X on the Welcome tab.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    1-6 Developing Web Applications Copyright IBM Corp. 2004, 2007

    __ d. By default, the Java 2 Platform, Enterprise Edition (J2EE) perspective is opened when you create a new workspace in Rational Application Developer. The title bar displays the currently active perspective.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    Copyright IBM Corp. 2004, 2007 Exercise 1. Develop a Java Application 1-7

    V4.1

    EXempty 1.2. Import a Project Interchange FileIn this section, you import a project interchange file that contains a Java project with all the resources needed to build the Library database.__ 2. Import the LibraryDatabaseSetup.zip project interchange file into the workspace.

    This file is located in the C:\LabFiles70\Java\setup directory.__ a. To import the file, click File Import from the main menu. The Import dialog

    appears.__ b. In the Import dialog, type inter to Select an import source. Select Other Project Interchange and click Next.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    1-8 Developing Web Applications Copyright IBM Corp. 2004, 2007

    __ c. On the Import Projects page, click Browse and locate the From zip file \Java\setup\LibraryDatabaseSetup.zip, as shown in the following figure. Select the LibraryDatabaseSetup check box.

    __ d. Click Finish.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    Copyright IBM Corp. 2004, 2007 Exercise 1. Develop a Java Application 1-9

    V4.1

    EXempty 1.3. Create and Populate the Library DatabaseIn this step, you create and populate the Derby database library. With Derby, you can specify ;create=true at the end of the database URL. Derby will then create the database if it does not exist. For example:jdbc:derby:C:/LabFiles70/database/library;create=true

    Note

    Your class may use a database product other than Derby. If this is the case, ask your instructor for alternative instructions for this step.

    Derby can be setup in either embedded or client-server mode. In embedded mode, only one process can access the database at a time. In this course, Derby is accessed using embedded mode.A common problem that occurs in embedded mode is that one process holds on to the connection when a second process attempts to create a connection. For example, the Data perspective or the application server (via a data source) holds on to a connection. This blocks the SetupLibraryDatabase Java application from connecting to the database. The error message displayed indicates that the application is unable to start the database.

    The LibraryDatabaseSetup Java application creates database tables and populates these tables. The Library case study relies on the tables created by this Java application. __ 3. Run the Java application driver.SetUpLibraryDatabase class to create the library

    database. It is located in the LibraryDatabaseSetup project.__ a. In the Project Explorer view, right-click LibraryDatabaseSetup driver SetUpLibraryDatabase.java and select Run As Java Application.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    1-10 Developing Web Applications Copyright IBM Corp. 2004, 2007

    __ b. Locate the Console view. If it is not present, add it to the current perspective by clicking Window Show View Console from the main menu. The console should show a series of messages:

    Note

    You can rerun the SetUpLibraryDatabase.java class at any time, to reset the tables to their default values. In subsequent exercises, you may need to do this in order to renew books in the library that are past their due date.The LibraryDatabaseSetup Java project, contained in the LibraryDatabaseSetup.zip project interchange file, specifies the following jars in its Build Path:ECLISPE_HOME/runtimes/base_v61/derby/lib/derby.jarWAS_50_PLUGINDIR/lib/j2ee.jar

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    Copyright IBM Corp. 2004, 2007 Exercise 1. Develop a Java Application 1-11

    V4.1

    EXempty To view the Build Path of a project, in the Project Explorer, right-click the project and select Build Path Configure Build Path. Click on either the Order and Export tab, or the Libraries tab to see the aforementioned jars.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    1-12 Developing Web Applications Copyright IBM Corp. 2004, 2007

    1.4. Customize the Perspective Switcher Toolbar LocationThe perspective switcher toolbar allows you to change perspectives or to quickly open a perspective. In the Rational Application Developer workbench, the perspective switcher toolbar appears in the top right corner. The icon and perspective name that appear in the toolbar allow you to quickly identify the current perspective.__ 4. Rational Application Developer allows you to dock the perspective switcher toolbar

    on the top right (default location), top left or left side of the screen. Dock the perspective switcher on the part of the screen you prefer.

    __ a. Select Window Preferences from the main menu.__ b. Expand General and select Appearance from the list of preferences under

    General.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    Copyright IBM Corp. 2004, 2007 Exercise 1. Develop a Java Application 1-13

    V4.1

    EXempty __ c. Select the Override presentation settings check box to enable changes to the appearance of the workbench.

    __ d. Under the Perspective switcher positions heading, choose the area of the screen to which you would like to dock the perspective switcher. For example, to dock the toolbar along the left side of the screen, choose Top Left.

    __ e. Click OK.

    __ 5. Hide the perspective names accompanying the icons on the perspective switcher toolbar.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    1-14 Developing Web Applications Copyright IBM Corp. 2004, 2007

    __ a. Right-click the perspective switcher toolbar.

    __ b. Click Show Text to toggle the Show Text feature off.

    Note

    You may also change the location of the perspective switcher by right-clicking the toolbar and choosing the Dock On pop-up menu option, and then choosing the location you prefer.You may open other perspectives by clicking Open Perspective on the perspective switcher toolbar.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    Copyright IBM Corp. 2004, 2007 Exercise 1. Develop a Java Application 1-15

    V4.1

    EXempty 1.5. Examine Code with the Call Hierarchy and Declaration ViewsThe Call Hierarchy view allows you to select a method and view the methods calls to and from that method. The Declaration view displays the source for the declaration of the element selected in the Java editor.__ 6. Open the LoadData.java class.

    __ a. In the Project Explorer double-click library.setup LoadData.java.

    __ 7. If necessary, open the Outline view.__ a. Select Window Show View Outline from the main menu.

    __ 8. Open the call hierarchy for the LoadData constructor.__ a. Right-click the LoadData(Connection,String) constructor in the Outline

    view and select Open Call Hierarchy. Alternatively, you can select the method in the Outline view and drag it into the Call Hierarchy view if you already have the Call Hierarchy view open.

    __ 9. View the members calling the LoadData constructor in this workspace.__ a. Expand each member in the call hierarchy to view:

    The sublcass constructors that directly call the LoadData method.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    1-16 Developing Web Applications Copyright IBM Corp. 2004, 2007

    The main methods indirectly call the LoadData constructor, by invoking the constructors of subclasses of LoadData.

    Notice a chain that starts from LoadData to LoadCopyData to main.

    __ 10. View method calls originating from the main method.__ a. In the Call Hierarchy view, right-click main(String[]) and select Focus on

    main from the pop-up menu. The main method becomes the root in the Call Hierarchy view.

    __ b. Click the Show Call Hierarchy button on the Call Hierarchy view toolbar.

    __ c. The Call Hierarchy view now displays the chain of method calls originating from the main method. Expand them to reveal the LoadCopyData and LoadData constructors.

    __ 11. View the declaration of the variable VARCHAR using the Declaration view.__ a. Open LoadData.java in the Java editor.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    Copyright IBM Corp. 2004, 2007 Exercise 1. Develop a Java Application 1-17

    V4.1

    EXempty __ b. Select Navigate Go to Line... from the main menu or alternatively click Control L.

    __ c. Enter 260 as the line number.__ d. Click OK.__ e. Highlight the word VARCHAR in line 260.

    __ f. Right-click on the word VARCHAR and select Open Declaration.

    Note

    You may need to select VARCHAR before right-clicking and selecting Open Declaration.You can also hold down the CTRL key and then slide over VARCHAR. Click the hyperlink that appears. Try holding down the CTRL key and sliding over types, variables, methods, and even imports. Do not click on the elements, because this will open too many editors.

    __ g. The Java editor automatically loads java.sql.Types.class and highlights the line that declares the VARCHAR variable.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    1-18 Developing Web Applications Copyright IBM Corp. 2004, 2007

    __ h. Close both Types.class and LoadData.java in the editor.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    Copyright IBM Corp. 2004, 2007 Exercise 1. Develop a Java Application 1-19

    V4.1

    EXempty 1.6. Pull Up Code Using Refactoring FeaturesThe pull up refactoring feature takes a method, local field, or variable and adds it to the superclass or interface. This feature allows you to factor out common elements from classes that have a common parent class. By using the refactor feature, all references and links referencing the code will be updated with the new location.__ 12. Add a private field and the corresponding getter and setter methods in

    LoadCopyData.java.__ a. In the Project Explorer view, double-click library.setup LoadCopyData.java

    to open it in the Java editor.__ b. Add a private field String testID to the class.

    Note

    The tool detects unused variables that can clutter your code and cause problems down the road.However, for the purposes of this course, ignore the following warning when you add the testID field:The field LoadCopyData.testID is never read locally

    __ c. Highlight testID in the editor, right-click and select Source Generate Getters and Setters.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    1-20 Developing Web Applications Copyright IBM Corp. 2004, 2007

    __ d. In the Generate Getters and Setters dialog, make sure only getTestID and setTestID are selected. Click OK.

    __ e. Note that the following lines of code have been generated and inserted at the end of the class.

    __ f. Save the changes.__ 13. Use the Pull Up feature to pull up the testID field to the superclass.

    __ a. With LoadCopyData.java as the active item in the editor, highlight the field testID, then select Refactor Pull Up from the main menu.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    Copyright IBM Corp. 2004, 2007 Exercise 1. Develop a Java Application 1-21

    V4.1

    EXempty __ b. In the Refactoring dialog, select library.setup.LoadData as the destination class. Ensure that only testID, getTestID, and setTestID(String) are selected.

    __ c. Click Next.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    1-22 Developing Web Applications Copyright IBM Corp. 2004, 2007

    __ d. The following page in the Refactoring wizard allows you to define which methods should be removed after pulling up the resource to the supertype. Select both the getTestID and setTestID(String) methods.

    __ e. Click Next.__ f. The Changes to be performed page displays a list of files affected by this pull

    up operation. The bottom pane shows a comparison of each file before and after

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    Copyright IBM Corp. 2004, 2007 Exercise 1. Develop a Java Application 1-23

    V4.1

    EXempty the pull up operation. Clear the changes to be made to the SetUpLibraryDatabase.java - LibraryDatabaseSetup/driver file.

    __ g. Click Finish to accept the changes.__ h. Notice that the field variable testID, as well as its getter and setter

    methods, are removed from the LoadCopyData class and placed into the LoadData class. The field and accessor methods become available to all subclasses of LoadData.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    1-24 Developing Web Applications Copyright IBM Corp. 2004, 2007

    1.7. Rename and Move Java ClassesThe rename and move Java class refactor feature provides a safe way to change a Java class name or package without disturbing references within the project. Use this feature to rename the ExecuteSQL.java class within the Java application.__ 14. Rename the ExecuteSQL class to DBCall.

    __ a. In the Project Explorer view, right-click library.setup ExecuteSQL.java and select Refactor Rename.

    __ b. In the Rename Compilation Unit dialog, type DBCall as the New name, and select the Update references check box.

    __ c. Click Next.__ d. The next dialog shows a preview of the changes to be performed. You can

    expand the items to see a detailed description of the changes. Notice that

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    Copyright IBM Corp. 2004, 2007 Exercise 1. Develop a Java Application 1-25

    V4.1

    EXempty CreateTables and DropTables are both subclasses of DBCall, and both are affected by the name change.

    __ e. Click Finish to confirm the rename operation.__ 15. Move the DBCall class from the library.setup package to the database package.

    __ a. Expand database package in the Project Explorer view.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    1-26 Developing Web Applications Copyright IBM Corp. 2004, 2007

    __ b. Drag the DBCall.java class into the database package. Alternatively, you can right-click ExecuteSQL.java and select Refactor Move, then choose database as the destination.

    __ c. Click Preview in the Move dialog to view a list of changes to be made by this refactoring operation.

    __ d. Click OK.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    Copyright IBM Corp. 2004, 2007 Exercise 1. Develop a Java Application 1-27

    V4.1

    EXempty 1.8. Perform a Regular Expression File SearchThe regular expression search functionality provides a powerful search facility within the workbench. Use regular expressions to search for SQL statements within the LibraryDatabaseSetup Java application.__ 16. Search for SQL statements using Regular Expression search.

    __ a. Select Search Search from the main menu, or click the Search button on the toolbar.

    __ b. In the Search dialog, click the tab scroll bar buttons to scroll the tabs and then select the File Search tab.

    __ c. Select the Regular expression and Case-sensitive check boxes.__ d. Enter INSERT|DROP|SELECT|CREATE in the Containing text field. The "|"

    symbol indicates an OR relationship. The inputted phrase searches for all occurrences of either INSERT or DROP or SELECT or CREATE, all of which are possible SQL commands.Make sure the scope is set to Workspace.

    __ e. Enter * in the File names patterns field. This widens the search to all file types.

    __ f. Click Search.__ 17. View the search results in Search view.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    1-28 Developing Web Applications Copyright IBM Corp. 2004, 2007

    __ a. Expand the folders in the Search view to see a list of files containing the search results. The files are kept in the same hierarchy structure as the project.

    __ b. Double-click library setup LoadPatronData.java to open it in the Java editor with the search result highlighted.

    __ c. Click the Show Next Match button in the Search view toolbar. The file containing the next search result is loaded into the editor with the search result highlighted.

    __ d. Use the Show Next Match button and the Show Previous Match button to browse through the search results.

    __ e. Click the toolbar Menu button (the downward triangle icon) in the Search view toolbar, select Show as List. This command shows the search result files in a list format, without any hierarchy information.

    __ f. Close the Search view when you are done examining the search results.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    Copyright IBM Corp. 2004, 2007 Exercise 1. Develop a Java Application 1-29

    V4.1

    EXempty 1.9. Examine Changes Using the QuickDiff Feature__ 18. Open the SetUpLibraryDatabase.java file in editor.

    __ a. In the Project Explorer view, double-click driver SetUpLibraryDatabase.java to open it in the Java editor.

    __ 19. Scroll through the code, using show/hide icons in the vertical ruler along the left edge of the editor to reveal folded portions of code.

    __ a. Click on the plus icon in the vertical ruler to the left of the line import java.io.IOException; at the top of the class to reveal folded (hidden) code.

    __ b. Click the minus icon in the vertical ruler to hide the list of import statements. Folding can also be applied to classes, methods, and JavaDoc comments.

    __ 20. Quick Diff is a color-coded change indicator in the editor's vertical ruler that displays additions, deletions, and modifications to the text as compared to a reference. This function compares changes in the editor against the file stored on disk or the latest CVS revision. Enable the Quick Diff feature and set the reference file to the version on disk.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    1-30 Developing Web Applications Copyright IBM Corp. 2004, 2007

    __ a. To ensure that Quick Diff is enabled, right-click in the editor's vertical ruler along the left side of the editor and verify that the Show Quick Diff is enabled.

    __ 21. Configure Quick Diff to show changes in the overview ruler. This action shows the differences in the file, as compared to a reference, in their relative position in the file using the overview ruler on the right side of the editor.

    __ a. Select Window Preferences from the main menu.__ b. Type quick diff in the text box to find the preferences for Quick Diff.__ c. Click Quick Diff, then select the Show differences in overview ruler check

    box. Note that this dialog also allows you to configure colors for the Quick Diff, and allows you to specify the default reference.

    __ d. Click OK.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    Copyright IBM Corp. 2004, 2007 Exercise 1. Develop a Java Application 1-31

    V4.1

    EXempty __ 22. Use the ALT+DOWN ARROW Key to shift a block of code and examine its Quick Diff consequences.

    __ a. Highlight the block of code that loads the PATRON table in the main method of SetUpLibraryDatabase.java.

    __ b. With the text highlighted, press ALT+DOWN ARROW on the keyboard to move the block of code down. Place it after the block that loads the ITEM table as follows.

    __ c. Notice the pale purple marks in the left side ruler, it denotes a change in the file. Also notice the white sections that have been added in the overview ruler on the right side, it shows the same change in their relative position in the file.

    __ 23. Save your changes to the file. Quick Diff now reverts to the newly saved version on disk.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    1-32 Developing Web Applications Copyright IBM Corp. 2004, 2007

    1.10. Manually Build Java ClassesBy default, Rational Application Developer compiles your Java class as you type your code in the editor. Rational Application Developer also provides manual build options if you want to force a compile from a workbench command. Rational Application Developer features a simplified set of manual build commands in comparison to those that were present in IBM WebSphere Studio Application Developer. Eclipse 3.0 added a task subsystem that reduces the number of actions that cause the tool to block. Many tasks can be run in the background. Rational Application Developer has a Progress view, which displays the status of tasks that are running in the background, including build tasks.__ 24. Turn off auto-build to disable the automatic code compile feature.

    Note

    When first starting out with the tool, it is not recommended to turn off auto-build. You will make changes to code and they will not appear when you run your code. However, as your projects grow in size, you may need to turn off auto-build to speed things up. There are developerWorks articles about optimizing the tool.One last warning: Some of the features within Rational Application Developer require that auto-build be turned on, so that code is correctly generated. Be sure to turn on auto-build once you are finished, as all of the exercises within the course assume that auto-build is on.

    __ a. Select Project Build Automatically from the main menu to toggle auto-build off. Make sure Build Automatically is checked.

    __ 25. Open the Progress view.__ a. Select Window Show View Other from the main menu.__ b. Expand General and select Progress from the list of views that appears.__ c. Click OK.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    Copyright IBM Corp. 2004, 2007 Exercise 1. Develop a Java Application 1-33

    V4.1

    EXempty __ 26. Build the project using the Clean feature. The Build Project feature builds out-of-date prerequisite projects of the selected project if needed.

    __ a. Select Project Clean from the main menu.__ b. Select Clean all projects.__ c. Ensure that the Start a build immediately check box is selected in the Clean

    dialog.

    __ d. Click OK and watch the Progress view display the build status of the workspace. The build may be too fast to see at this time.

    __ e. Select Project Build Automatically from the main menu to toggle auto-build on. Make sure Build Automatically is checked.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    1-34 Developing Web Applications Copyright IBM Corp. 2004, 2007

    1.11. Run the Java Application in the WorkbenchRational Application Developer allows you to run a Java application through a menu item. Selecting a Java Application launch configuration invokes the main method in the Java class. Output to standard output and standard error appears in the Console view.__ 27. Run the project with the default Java Application launch configuration.

    __ a. Make sure that SetUpLibraryDatabase.java is the active item in the Java editor.__ b. Click the down arrow next to the Run icon in the workbench toolbar, and select

    Run As Java Application.

    Note

    Note that in the preceeding figure that 1 SetupLibraryDatabase appears in the Run drop-down menu. The tool remembers the most recent launched configurations. The same list appears in the Debug drop-down menu. This allows you to quickly run or debug your most recent launch configurations.

    __ c. The application sends status messages to standard output, which is displayed in the Console view. The last few status lines indicate that the database tables have been created and populated. If the console view is not visible, select Windows Show View Console from the main menu.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    Copyright IBM Corp. 2004, 2007 Exercise 1. Develop a Java Application 1-35

    V4.1

    EXempty 1.12. View Database Contents with a Java ClassUsing the Java editor, create a simple class with a main method that accesses and retrieves a list of patrons from the Library case study database.__ 28. Create a new Java class called DisplayPatron under the driver package.

    __ a. Right-click the driver package and select New Class.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    1-36 Developing Web Applications Copyright IBM Corp. 2004, 2007

    __ b. In the New Java Class dialog, enter the following: - Source Folder: LibraryDatabaseSetup - Package: driver - Name: DisplayPatron

    __ c. At the bottom of the wizard, select the public static void main(String[] args) option to create a method stub for the main method.

    __ d. Click Finish.__ 29. The DisplayPatron class is displayed in the Java editor. Complete the main

    method so that it retrieves the list of patron names from database and displays it.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    Copyright IBM Corp. 2004, 2007 Exercise 1. Develop a Java Application 1-37

    V4.1

    EXempty __ a. Paste the code from \Java\snippets\snippet01.txt to the main method. The result should appear as shown in the following screenshot.

    __ b. Notice that there are errors noted on the left side marker bar. These are errors due to classes that the compiler cannot resolve.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    1-38 Developing Web Applications Copyright IBM Corp. 2004, 2007

    1.13. Fix the Errors Using Quick Fix and Organize ImportsMissing import statements is one of the most common errors in Java coding. Rational Application Developer provides two solutions to missing import statements. First, the Quick Fix feature examines your workspace and Java build path and suggests possible packages that contain the missing class. Second, the organize imports statement scans through the entire Java class and resolves missing import statements. Use both methods to fix the two errors in the DisplayPatron class.__ 30. Use Quick Fix to fix errors.

    __ a. Locate the light bulb in the left column of the Java editor next to the statement Trace.error(e.getMessage());. Click the lightbulb to bring up the Quick Fix pop-up window. The pop-up suggests ways of fixing the current line of error.

    Note

    You can also trigger the Quick Fix pop-up by either: Right-clicking the light bulb and selecting Quick Fix from the pop-up menu. Clicking the red squiggly underline and pressing CTRL+1.

    __ b. Double-click Import 'Trace' (tools) to insert the suggested import statement.

    __ c. Verify that the statement import tools.Trace has been added to the Java class.__ 31. There are still two more unresolved references in the class. Use the Organize

    Imports feature to fix these errors.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    Copyright IBM Corp. 2004, 2007 Exercise 1. Develop a Java Application 1-39

    V4.1

    EXempty __ a. Right-click anywhere in the Java editor and select Source Organize Imports. You can also trigger the Organize Imports command by selecting Source Organize Imports from the main menu.

    __ b. Select java.util.List in the Organize Imports dialog box, then click Finish.

    __ c. Verify that the two additional import statements have been added to the Java class.

    __ d. Press CTRL-S to save. Alternatively, you can select File Save from the main menu. At this point, no errors should appear in the Java class.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    1-40 Developing Web Applications Copyright IBM Corp. 2004, 2007

    1.14. Run the Java Class from The Outline ViewYou can run a Java class from the pop-up menu in the Outline view. Use this feature to test the newly created DisplayPatron Java class.__ 32. Right-click the DisplayPatron class in the Outline view. From the pop-up menu,

    select Run As Java Application. Notice the C symbol on the icon next to DisplayPatron; it represents the item is a class.

    __ 33. You should see a list of names displayed in the Console view. These default values were previously inserted into the database by SetUpLibraryDatabase.java.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    Copyright IBM Corp. 2004, 2007 Exercise 1. Develop a Java Application 1-41

    V4.1

    EXempty 1.15. Visualize Java Class Relationships with UML DiagramsUnified Modeling Language (UML) provides a graphical notation for describing Java elements, inheritance, and use relationships. Rational Application Developer can automatically build a UML diagram based on a group of classes in your Java application. Explore this feature with a package from the LibraryDatabaseSetup Java application.__ 34. Create a new UML class diagram.

    __ a. From the main menu, select File New Other from the main menu.__ b. In the New dialog, expand the Modeling folder and select Class diagram.

    __ c. Click Next.__ d. In the New Class Diagram dialog, enter the following:

    - Enter or select the parent folder: LibraryDatabaseSetup. - File name: classdiagram

    __ e. Click Finish.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    1-42 Developing Web Applications Copyright IBM Corp. 2004, 2007

    __ f. Click OK in the Confirm Enablement dialog to enable the "Java Modeling" activity.

    __ g. The new empty class diagram, classdiagram.dnx, automatically opens in the editor view.

    __ 35. Add the existing classes from the library.setup package into the UML diagram.__ a. In the Project Explorer view, expand the library.setup package.__ b. Select all classes in the library.setup package. You can do so by holding down

    the SHIFT key on the keyboard, click the first item (CreateTables.java) in the package and then the last item (RetrievePatronData.java) in the package.

    __ c. Drag the selected classes into the editor, where classdiagram.dnx is the active file.

    __ d. The class diagram for the selected classes should be automatically generated and displayed in the editor. Use the pan and zoom feature in the Outline view to view the class diagram.

    __ 36. Format the diagram to hide the attributes and operations, so that only class names are shown.

    __ a. Right-click anywhere in the editor, and select Select All Shapes.

  • Instructor Exercises Guide

    Course materials may not be reproduced in whole or in part without the prior written permission of IBM.

    Copyright IBM Corp. 2004, 2007 Exercise 1. Develop a Java Application 1-43

    V4.1

    EXempty __ b. Right-click any of the selected shapes, and choose Filters Show/Hide Compartment Name Compartment Only.

    __ c. Verify that only the class names appear in the diagram.__ d. Right-click anywhere in the editor and select Arrange All to automatically

    rearrange the items in the diagram.

    __ e. Press CTRL-S to save the changes.__ f. When you are done, select File Close All from the main menu to close all

    editor items.

    End of exercise