More Related Content
Similar to Lab 4) working with databases
Similar to Lab 4) working with databases (20)
Recently uploaded
SOLIDE WASTE in Cameroon,,,,,,,,,,,,,,,,,,,,,,,,,,,.pptxSOLIDE WASTE in Cameroon,,,,,,,,,,,,,,,,,,,,,,,,,,,.pptxSyedNadeemGillANi
Lab 4) working with databases
- 1. V4.1
Instructor Exercises Guide
EXempty Exercise 7. Connect to a Database
Estimated time
01:00
What this exercise is about
In this exercise you will explore the Data perspective. You will
manipulate database information, both by importing it into Rational
Application Developer and by generating a new database and
exporting it.
What you should be able to do
At the end of this lab, you will be able to:
• Connect to a database and sample the data
• Import the database structure into Rational Application Developer
• Create and populate new database tables
• Use the SQL Builder to create a SQL statement and query the
library database
• Create a diagram from the tables
Introduction
Developers are generally more concerned with examining existing
database structures and sampling data, rather than creating new
structures and writing them back to the database. Nevertheless, there
are times when it is necessary to create a new database for test
purposes, and IBM Rational Application Developer (V7 and later) has
the tools to do this.
This lab takes you through both processes: importing information (both
structural and sample data), and exporting artifacts created in Rational
Application Developer.
© Copyright IBM Corp. 2004, 2007 Exercise 7. Connect to a Database 7-1
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
- 2. Instructor Exercises Guide
Requirements
This exercise requires one code fragment. Make sure that the file
appears in the <LAB_FILES> DataSnippets directory before
starting the lab.
It also requires that the Derby database (LIBRARY) exist. This should
have been set up in Lab 1. If it has not been set up, see Appendix A for
instructions on how to do this.
Instructor exercise overview
This exercise divides into four sections, of which the first and third are the most important
for developers:
1. Importing structure from a database, and sampling data.
There are several different ways to create the project to contain the database structure.
The one used in this lab is not necessarily the fastest, but it is expected that students will
discover other ways themselves when they need to use this process. Step 3b) has the key
screenshot, the dialog where the database type is chosen, the address entered and the
connection tested. You should have gone over this in the presentation. It would be good, if
there is time, to revisit this dialog after the lab before continuing with the next presentation.
2. Creating structure in Rational Application Developer and exporting it to a
database.
Students should understand that they would not usually have the permission to create
databases this way in the real world! This is strictly a development environment. DBAs may
have their own tools to create databases.
3. Querying the database.
This section also should be clearly understood by students. They will need to create SQL
statements, so this part of the lab is crucial. If after forty minutes you see that some
students are still in part 1, suggest that they skip step 2 and do this section.
4. Creating a diagram.
A very brief section, and not too important for the understanding of the Data Perspective
and the data tools.
If students are behind in lab work, try to make sure that they have at least accomplished
the first part and the third part.
7-2 Developing Web Applications © Copyright IBM Corp. 2004, 2007
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
- 3. V4.1
Instructor Exercises Guide
EXempty Exercise instructions
Preface
Open the workspace from the previous lab and add a database connection. Import
structural information into a new project in Rational Application Developer, and create a
new structure that will be deployed to a database. Create a diagram to see the
relationships between the different tables.
The following references in the exercise instructions represent directory locations in your
workstation:
• <LAB_FILES> : C:LabFiles70
• <SDP_DIR> : c:IBMSDP70
© Copyright IBM Corp. 2004, 2007 Exercise 7. Connect to a Database 7-3
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
- 4. Instructor Exercises Guide
7.1. Prepare the Workspace
__ 1. Start Rational Application Developer with the workspace from the previous lab.
__ a. In the Workspace Launcher dialog, specify <LAB_FILES>workspace as the
workspace directory (it should already be pointing to that directory from the
previous lab).
__ b. Click OK to open the workspace. If this is a new workspace, follow the
instructions in Appendix A to set up the projects for this exercise.
7-4 Developing Web Applications © Copyright IBM Corp. 2004, 2007
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
- 5. V4.1
Instructor Exercises Guide
EXempty 7.2. Import the Library Database Schema
__ 2. Create a Data Design project to hold the database information.
__ a. Switch to the Data perspective. This can be accomplished by selecting Window
Open Perspective Other from the main menu and choosing Data from the
Select Perspective dialog.
__ b. Right-click in the Data Project Explorer and select New Data Design Project.
__ c. In the New Data Design Project dialog, enter LibraryData in the Project
name field.
__ d. Click Finish.
__ 3. Create a connection to the library database that was created in the first exercise.
Note
You will not be able to connect to the database if the test server is running. To stop the
server, go to the Servers view, right-click the server and select Stop.
© Copyright IBM Corp. 2004, 2007 Exercise 7. Connect to a Database 7-5
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
- 6. Instructor Exercises Guide
__ a. In the Database Explorer view, right-click anywhere in the view and select New
Connection.
7-6 Developing Web Applications © Copyright IBM Corp. 2004, 2007
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
- 7. V4.1
Instructor Exercises Guide
EXempty __ b. In the New Connection dialog, enter the following:
- Database Manager: Derby 10.1
- JDBC Driver: Derby Embedded JDBC Driver
- Database location: Browse to <LAB_FILES>databaselibrary
- Class location: Browse to
<SDP_DIR>runtimesbase_v61derbylibderby.jar
__ c. Click Test Connection to check if the parameters are valid. The following
message should display.
__ d. Click Finish.
__ 4. Copy the Data model to the LibraryData project.
© Copyright IBM Corp. 2004, 2007 Exercise 7. Connect to a Database 7-7
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
- 8. Instructor Exercises Guide
__ a. In the Database Explorer, expand the library connection to show the library
database.
__ b. Drag the library database from the Database Explorer onto the LibraryData
project in the Data Project Explorer. The Physical Data Model Editor for
library.dbm will open.
__ c. Expand the Library connection in the Database Explorer view. It should have the
following structure. (Note that some systems are not shown at the bottom of the
image.)
7-8 Developing Web Applications © Copyright IBM Corp. 2004, 2007
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
- 9. V4.1
Instructor Exercises Guide
EXempty __ 5. Sample the data from the Patron table.
__ a. In the Database Explorer view, expand the connection to Connections
LibraryConnection library LIBRARY Tables.
__ b. Right-click LIBRARY.PATRON and select Data Sample contents.
__ c. The result is shown in the Data Output view.
© Copyright IBM Corp. 2004, 2007 Exercise 7. Connect to a Database 7-9
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
- 10. Instructor Exercises Guide
7.3. Create and Populate New Database Tables
With the library database schema copied into the LibraryData project, you can
examine the database schema even after disconnecting from the actual database.
You can also create a new database schema within Rational Application Developer
and export the tables to the actual database.
Note
Another way to work with a connection offline is to right-click the connection in the
DatabaseExplorer and select Save Offline. Once disconnected, you can right-click the
connection and select Work Offline. Saving the connection offline saves the database
schema, but not the data. Another issue is that you cannot get metrics or live information
from the underlying database management system.
__ 6. Create a new database.
__ a. In the Data Project Explorer, right-click LibraryData and select New Physical
Data Model.
7-10 Developing Web Applications © Copyright IBM Corp. 2004, 2007
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
- 11. V4.1
Instructor Exercises Guide
EXempty __ b. In the New Physical Data Model dialog, enter the following:
• Destination Folder: /LibraryData
• File name: BOOKSTORE
• Database: Derby
• Version: 10.1
__ c. Click Finish. A Physical Data Model editor and a Diagram editor will open for the
new database. Close the Diagram editor.
__ 7. The Data Project Explorer now displays the BOOKSTORE database model.
Rename the database to BOOKSTORE and the schema to CATALOG.
__ a. In the Data Project Explorer, select LibraryData Data
Models BOOKSTORE.dbm New Database.
__ b. Either click New Database or use the Properties view to change the name to
BOOKSTORE.
© Copyright IBM Corp. 2004, 2007 Exercise 7. Connect to a Database 7-11
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
- 12. Instructor Exercises Guide
__ c. In the Data Project Explorer, select the Schema node below the BOOKSTORE
database node. Click the node name again and change the name to CATALOG.
__ d. Save your changes by clicking within the BOOKSTORE.dbm Physical Data
Model Editor and pressing CTRL+S.
__ 8. Add a new table to the schema.
__ a. Right-click CATALOG in the Data Project Explorer and select Add Data Object
Table.
__ b. Change the table name from Table1 to NewRelease.
__ c. Select the NewRelease node in the Data Project Explorer.
__ d. In the Properties view, select the Columns tab.
__ e. Click the New icon (circled above) to add each of the columns listed below:
Column 1
— Name: title
— Data Type: VARCHAR
— Length: 30
Column 2
7-12 Developing Web Applications © Copyright IBM Corp. 2004, 2007
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
- 13. V4.1
Instructor Exercises Guide
EXempty — Name: author
— Data Type: VARCHAR
— Length: 30
Column 3
— Name: price
— Data Type: DOUBLE
Column 4
— Name: releasedate
— Data Type: DATE
__ f. Save your changes by pressing CTRL+S.
__ 9. With the BOOKSTORE database completed, you can export the schema and table
to the actual BOOKSTORE Derby database.
© Copyright IBM Corp. 2004, 2007 Exercise 7. Connect to a Database 7-13
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
- 14. Instructor Exercises Guide
__ a. In the Data Project Explorer, right-click BOOKSTORE and select Generate DDL.
__ b. In the Generate DDL dialog, on the Options page, select the following check
boxes: Fully qualified name, DROP statements, and CREATE statements.
__ c. Click Next.
__ d. Accept the defaults on the Objects page. Click Next.
7-14 Developing Web Applications © Copyright IBM Corp. 2004, 2007
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
- 15. V4.1
Instructor Exercises Guide
EXempty __ e. On the Save and Run DDL page, change the File Name to
createBookStoreTables.sql. Select the Run DDL on server check box.
__ f. Click Next.
© Copyright IBM Corp. 2004, 2007 Exercise 7. Connect to a Database 7-15
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
- 16. Instructor Exercises Guide
__ g. On the Select Connection page, select the Create a new connection check
box. A new connection is required as this is a different database.
__ h. Click Next.
__ i. On the Connection Parameters page, fill in the following information:
• Database Location: <LAB_FILES>databasebookstore
• Class Location: Browse to
<SDP_DIR>runtimesbase_v61derbylibderby.jar
7-16 Developing Web Applications © Copyright IBM Corp. 2004, 2007
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
- 17. V4.1
Instructor Exercises Guide
EXempty • User ID: CATALOG )
Note
This database does not exist. The Create the database if required check box is selected.
This means that the Connection URL in the proceeding image will have ;create=true
appended to the end. This will create the database if it does not exist.
The User ID will create the schema - (CATALOG in this case).
__ j. Click Next. Click Finish on the Summary page.
__ k. Check that the Data Output view indicates success. Note: There may be a
failure related to the attempt to drop the CATALOG schema that does not exist.
© Copyright IBM Corp. 2004, 2007 Exercise 7. Connect to a Database 7-17
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
- 18. Instructor Exercises Guide
__ 10. After creating the new schema and new table in the Derby database, you can
populate it through an SQL/DDL Script.
__ a. In the Data Project Explorer, right-click the SQL Scripts node and select New
SQL Statement.
Note
The createBookStoreTables.sql script was created in the same location.
__ b. In the New SQL Statement dialog, click SQL Editor first. Enter
populateNewRelease.sql as the Statement name and select Sample
Statements from the Statement template drop-down list.
__ c. Click Finish.
7-18 Developing Web Applications © Copyright IBM Corp. 2004, 2007
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
- 19. V4.1
Instructor Exercises Guide
EXempty __ d. The Connection Selection dialog will open to allow the SQL editor to create
SQL for a specific database type. Click Use an existing connection and select
bookstore from the Existing connections list.
__ e. Click Finish.
© Copyright IBM Corp. 2004, 2007 Exercise 7. Connect to a Database 7-19
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
- 20. Instructor Exercises Guide
Information
An SQL editor appears with the text as shown below. Notice that because you selected
Generate sample DML statements, you have four example statements to use in your
SQL file. The SQL is tailored for the type of database specified by the connection.
Note that these statements will not work as-is with the database. The default parameters
need to be changed.
__ 11. Write the SQL statements to populate the NewRelease table.
__ a. Enter the following lines to replace the template statements. Notice the delete
statement at the beginning; this statement deletes all existing rows in the
newrelease table. This is not standard practice for a production environment, but
7-20 Developing Web Applications © Copyright IBM Corp. 2004, 2007
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
- 21. V4.1
Instructor Exercises Guide
EXempty it is useful for testing purposes. You can also copy and paste the statements
from <LAB_FILES>Datasnippets snippet01.txt.
__ b. Save the changes.
__ 12. Run the SQL/DDL script on the database.
__ a. In the Data Project Explorer, right-click LibraryData SQL Scripts
populateNewRelease.sql and select Run SQL.
__ b. In the Connection Selection dialog, click Use an existing connection and
select bookstore from the Existing connections list.
__ c. Click Finish.
__ d. Check that the Data Output view indicates success. There should be five entries
for the script that was just run. The first entry for the SQL Editor is a warning
indicating that the NewRelease table was empty when a DELETE was executed.
© Copyright IBM Corp. 2004, 2007 Exercise 7. Connect to a Database 7-21
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
- 22. Instructor Exercises Guide
__ 13. Sample the database contents to ensure that the correct data has been populated.
__ a. In the Database Explorer, right-click the bookstore connection and select
Refresh.
__ b. Right-click bookstore bookstore Schemas CATALOG Tables
NEWRELEASE, and select Data Sample contents.
__ c. The result is shown in the Data Output view.
7-22 Developing Web Applications © Copyright IBM Corp. 2004, 2007
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
- 23. V4.1
Instructor Exercises Guide
EXempty 7.4. Use the SQL Query Builder to Query the Library Database
You will now use the Query builder to display a list of items a patron has on loan.
You will join the ITEM, ONLOAN, and PATRON tables to display the patron’s full
name, and the title of the items currently loaned to that patron.
__ 14. Create a select statement in the library database.
__ a. In the Data Project Explorer, right-click LibraryData Data Models
library.dbm library SQL Statements and select New SQL Statement..
__ b. In the New SQL Statement dialog, enter selectOnLoanItems as the
Statement name and choose SELECT as the Statement Template. Click OK.
__ c. The SQL Builder for selectOnLoans will open.
__ 15. In the SQL Builder, add the ITEM (alias I), ONLOAN(alias O) and PATRON(alias P)
tables to the select. Display the I.TITLE, P.FIRST_NAME and P.LAST_NAME fields
in the select. Join I.ITEM_KEY to O.PATRON_ID and O.PATRON_ID to
P.PATRON_ID.
© Copyright IBM Corp. 2004, 2007 Exercise 7. Connect to a Database 7-23
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
- 24. Instructor Exercises Guide
__ a. Right-click in the middle pane to add a table to the select statement.
__ b. In the Add Table dialog, select LIBRARY.ITEM in the Table name list, and enter
I (uppercase ‘i’) as the Table alias. Click OK.
__ c. Add two more tables in the same fashion, using the following parameters.
Table 2
— Table name: LIBRARY.ONLOAN
— Table alias: O
Table 3
— Table name: LIBRARY.PATRON
— Table alias: P
__ d. Specify the fields to display in the SELECT, by selecting the following check
boxes: TITLE in Table I, and FIRST_NAME and LAST_NAME in Table P.
7-24 Developing Web Applications © Copyright IBM Corp. 2004, 2007
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
- 25. V4.1
Instructor Exercises Guide
EXempty __ e. Join the tables, by dragging I.ITEM_KEY to O.ITEM_KEY. Drag O.PATRON_ID
to P.PATRON_ID. Lines should appear to indicate a join operation.
__ 16. Set the sort order in selectOnLoansItems to sort ascending P.LAST_NAME and
P.FIRST_NAME.
__ a. Click the Columns tab near the bottom of the SQL Builder.
__ b. In the P.LAST_NAME row, select Sort Order to be 1, and select Sort Type
Ascending.
Note
Tip to set the Sort Type and Sort Order.
• For the Sort Type, select Ascending, and then press TAB.
• Do the same for Sort Order.
__ c. In the P.FIRST_NAME row, select Sort Order to be 2, and select Sort Type to
Ascending.
__ d. The SQL source should now be as follows. Note that carriage returns were
added to display the full SQL.
__ e. Save your changes.
__ 17. Run the selectOnLoanItems statement in the SQL Builder.
© Copyright IBM Corp. 2004, 2007 Exercise 7. Connect to a Database 7-25
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
- 26. Instructor Exercises Guide
__ a. In the SQL Builder, right-click in the SQL pane and select Run SQL.
__ b. In the Connection Selection dialog, click Use an existing connection and
select library from the Existing connections list.
__ c. If you are not connected to the library database, click Reconnect. When
prompted, enter X as the password. Click Finish.
__ d. Check the result in the Data Output view.
7-26 Developing Web Applications © Copyright IBM Corp. 2004, 2007
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
- 27. V4.1
Instructor Exercises Guide
EXempty 7.5. Create a Diagram for the Data Tables
__ 18. Display the tables from the LIBRARY schema in the Library database.
__ a. In the Data Project Explorer, right-click LibraryData Data Models
library.dbm library Library Diagrams and select New Blank
Diagram.
__ b. In the Data Project Explorer, navigate to LibraryData Data Models
library.dbm library Library folder.
__ c. Select all five tables, LIBRARY.COPY, LIBRARY.ITEM, LIBRARY.ONLOAN,
LIBRARY.PATRON, and LIBRARY.TABLEMAX. You can select multiple items by
holding down the CTRL key while clicking on the individual items.
__ d. Drag the five tables into the Diagram1 editor.
__ e. The tables should now appear in the diagram with connections signifying their
relationships.
© Copyright IBM Corp. 2004, 2007 Exercise 7. Connect to a Database 7-27
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
- 28. Instructor Exercises Guide
__ 19. Right-click anywhere in the diagram and select Arrange All.
__ 20. Save your changes.
7-28 Developing Web Applications © Copyright IBM Corp. 2004, 2007
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
- 29. V4.1
Instructor Exercises Guide
EXempty 7.6. Close the Database Connections
__ 21. In the Database Explorer view, right-click the Library connection and select
Disconnect. This will release the database so that it will be available for later
exercises. Do the same for the bookstore connection.
__ 22. [OPTIONAL] Close the LibraryData project.
__ a. In the Data Project Explorer, right-click the Library Data project and select
Close Project.
End of exercise
© Copyright IBM Corp. 2004, 2007 Exercise 7. Connect to a Database 7-29
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.