Sunteți pe pagina 1din 10

DB2 and Rational: Working together, Part 1: Introduction to DB2 development with Rational Application Developer

Create DB2 DDL, DML and stored procedures using the Rational Application Developer Data and Debug perspectives
Skill Level: Introductory Debra R. Eaton (deaton@us.ibm.com) DB2 Software Information Technology Specialist IBM

15 Dec 2005 The IBM DB2 Universal Database (UDB) plug-ins for Rational Application Developer give developers the ability to connect, browse and alter DB2 databases. In this tutorial learn how the Rational Application Development perspectives, views and wizards assist you with defining and working with database object DDL such as schemas and tables. Learn how to create, execute and edit DML statements such as SELECTs and INSERTS. Create, execute, run, and debug DB2 stored procedures with the Rational Data and Debug perspectives.

Section 1. Before you start


You should understand the concepts of the following Data Definition Language (DDL) statements: CREATE DATABASE, CREATE TABLE and CREATE SCHEMA. You should understand the concepts of the following Data Manipulation Language (DML) statements: INSERT and SELECT. You should understand the concept of the DB2 routine statement CREATE PROCEDURE. If you are not familiar with these concepts, you can access the information at the at DB2 Information Center.

About this tutorial


This tutorial demonstrates the steps to create DDL statements; deploy database

Introduction to DB2 development with Rational Application Developer Copyright IBM Corporation 1994, 2005. All rights reserved.

Page 1 of 10

developerWorks

ibm.com/developerWorks

objects; create and execute DML statements; and create, execute, and debug stored procedure routines using the Rational Application Developer (RAD) Data and Debug perspectives.

Objectives
In this tutorial, you learn about the RAD Data perspective, Debug perspective, Data Definition view, Database Explorer view, and the DB Output view. Step-by-step instructions give you hands-on experience in using the perspectives, views, and wizards.

Prerequisites
This tutorial is written for DB2 database developers, DB2 database administrators, and Rational developers whose skills using RAD for DB2 database development are at a beginning to intermediate level. You should have a general familiarity with database objects and working with a GUI interface.

System requirements
This tutorial was created with trial versions of Rational Application Developer (RAD) V6.0 and DB2 UDB V8.2 running on Windows XP. See resources for download information. The tutorial was written from the viewpoint of a new installation of the Rational software. The assumption is that all settings, views, and perspectives are set to the RAD default configuration. We will use the DB2 SAMPLE database. The operating system userid and password is used to gain access to DB2.

Section 2. Overview of the Data perspective


This section will introduce you to RAD perspectives and views. You can access relational database tools from the Data and Debug perspective. With these tools you can create and manipulate the data definitions for your project. Also you can use the tools to create, edit, and execute DML statements and stored procedure routines. The purpose of this section is to learn how to move between the perspectives. If your installation of RAD is new, your perspectives will be empty. In future sections we will create objects in the perspectives. In this section we will accomplish the following tasks: Start Rational Open the Data perspective

Introduction to DB2 development with Rational Application Developer Page 2 of 10 Copyright IBM Corporation 1994, 2005. All rights reserved.

ibm.com/developerWorks

developerWorks

Open the Data Definition view Open the Database Explorer view Open the DB Output view

Start Rational
Open RAD on your Windows desktop.

Data perspective
Open the Data perspective to observe the relational database tools in the RAD workbench. The tools appear within the perspective as views and wizards.

Data Definition view


The Data Definition view is used to create, work with, and change database schemas. You can export data definitions to another database that is installed either locally or remotely. In the Data Definition view you can work locally with relational data objects. The Data Definition view can hold: Local copies of data definitions that were imported from the Database Explorer view Designs that were created by running DDL scripts Designs that you created in the workbench Using the Data Definition you can also: Rename logical objects Create, edit and use relational views, DB2 UDB stored procedures, and DB2 user-defined functions Define filters Drag and drop database objecs from the Database Explorer view or within the Data Definition view Expand the Data Defintion view to observe all the features.

Database Explorer view


The Database Explorer view is used to browse or import database schemas, make
Introduction to DB2 development with Rational Application Developer Copyright IBM Corporation 1994, 2005. All rights reserved.

Page 3 of 10

developerWorks

ibm.com/developerWorks

connections to an existing database, modify schema designs, and run database objects with results appearing in the DB output view. Using the Database Explorer view, you can: Create a database connection Define connection filters Connect to existing databases and view their designs Disconnect from a database if you are connected Import database designs to another folder in the Data Definition view, where you can extend or modify the designs Refresh the database objects that are defined for a database connection Delete a database connection Drag and drop database objects to the Data Definition view Generate an SQL DDL statement for tables and schemas Run sample contents and generate an XML schema for tables Run stored procedures and user-defined functions and view the results in the DB Output view Expand the Database Explorer view to observe all the features.

DB Output view
The DB Output view includes an actions list on the left side of the view. Messages, parameters, and results related to database objects that you manipulate appear on the tabbed pages on the right side. The tabbed pages on the right side of the DB Output view display information that is related to the actions list on the left side: A Messages page displays messages, including the progress of actions A Parameters page displays the parameters for a routine or object A Results page displays the results of SQL statements that are run by a routine The left side of the DB Output view contains the following fields: The Status field shows the current state of the statement The Action field shows what kind of action occurred

Introduction to DB2 development with Rational Application Developer Page 4 of 10 Copyright IBM Corporation 1994, 2005. All rights reserved.

ibm.com/developerWorks

developerWorks

The Object Name shows the name of the statement Expand the DB Output view to observe all the features.

Debug perspective
In the Debug Perspective you can debug a DB2 stored procedure. You can provide input arguments, view results, set breakpoints, monitor variables, and run the stored procedure in debug mode.

Section 3. Database connections


The New Database Connection wizard allows a user to create a database connection. The wizard is launched from the Database Explorer view. You can refresh, reconnect, disconnect, and delete your database connection. Also, you can view the properties of a database connection. In this section we will accomplish the following tasks: Using the New Database Connection wizard Managing database connection

New Database Connection wizard


The New Database Connection wizard is used to create a new database connection.

Manage database connection


In the Database Explorer view you can disconnect, reconnect, and delete your database connection. You can also view the properties of a database connection or refresh the catalog metadata.

Section 4. Define database objects


The Data Definition view is used to define new database objects. The database and the database objects must reside in a project. A project contains folders and files that can be opened, closed, or built. In this section we will accomplish the following
Introduction to DB2 development with Rational Application Developer Copyright IBM Corporation 1994, 2005. All rights reserved.

Page 5 of 10

developerWorks

ibm.com/developerWorks

tasks: Define a project Define a database Define a schema Define a table

Define a project
In the Data Definition view, we will define a project for our database.

Define a database
In our project, we will define a database called MUSICDB using the New Database Definition wizard in the Data Definition view.

Define a schema
In our database we will define a schema called MYMUSIC using the New Schema Definition wizard in the Data Definition view. The schema is a collection of named objects. The schema provides a logical classification of objects in the database.

Define a table
In our schema, we will define a new table called FAVORITES using the New Table Definition wizard in the Data Definition view. The table consists of columns and rows. Tables are logical structures that are maintained by the database manager. Table 1. Table columns
Column Name TITLE CATEGORY ID Data Type VARCHAR VARCHAR INTEGER

Section 5. Deploying data objects to a database


You can deploy a database or database objects to a database server. The database

Introduction to DB2 development with Rational Application Developer Page 6 of 10 Copyright IBM Corporation 1994, 2005. All rights reserved.

ibm.com/developerWorks

developerWorks

server can be on the local computer or remote server. In this section we will deploy our MUSICDB data definitions to a database server outside of the workbench. The database must exist on the DB2 server before you deploy the database from the workbench. In this section we will accomplish the following tasks: Define a database on the server Deploy database objects to a database server

Define a database on the server


In the DB2 Control Center we will define a database on the DB2 server as the target for the deployment of our database objects from the RAD workbench.

Deploy database objects to a database server


In the RAD Data Definition view we will deploy database objects to our new database on the server.

Section 6. Working with SQL statements


You can create, edit, and execute DML SQL statements using the SQL Builder and the Create a New SQL Statement wizard. In this section we will accomplish the following tasks: Create an SQL statement Execute an SQL statement Edit an SQL statement Sample the contents of a table

Create an SQL statement.


In the Data Definition view, we will create a DML SQL statement to run against our MUSICDB database.

Execute an SQL statement.


In the Data Definition view, we will select a DML SQL statement and then execute the statement against the MUSICDB database.

Introduction to DB2 development with Rational Application Developer Copyright IBM Corporation 1994, 2005. All rights reserved.

Page 7 of 10

developerWorks

ibm.com/developerWorks

Edit an SQL statement


We will turn on the SQL statement validating tool to validate the SQL syntax. In the Data Definition view, we will add several rows to the MYMUSIC table by editing an existing DML SQL statement. Table 2. Table column values for mulitple execution
ARTIST Bruce Springsteen U2 Johnny Cash Glen Campbell Etta James Miles Davis George Gershwin Ferde Grofe TITLE Born to Run Where the Streets Have No Name Ring of Fire Wichita Lineman Cry Me a River All Blue Rhapsody in Blue Grand Canyon Suite CATEGORY Rock Rock Country Country Jazz Jazz Classical Classical ID 02 03 04 05 06 07 08 09

4.

Execute each statement and check the DB Output view for successful execution.

Sample the contents of a table


In the Database Explorer view we will sample the contents of a table.

Section 7. Developing stored procedures


The workbench provides numerous features to help you develop DB2 stored procedures. Stored procedures contain procedural constructs with SQL statements. The procedures are stored in the DB2 database and run on DB2 servers. You can use the New Stored Procedure wizard to create DB2 stored procedures in SQL. Using the New Stored Procedure wizard, you name the stored procedure, define the structure and SQL statement, and specify the parameters and additional options. In this section we will accomplish the following tasks: Configure workbench for stored procedures Create stored procedure

Introduction to DB2 development with Rational Application Developer Page 8 of 10 Copyright IBM Corporation 1994, 2005. All rights reserved.

ibm.com/developerWorks

developerWorks

Run stored procedures Debug stored procedures

Configure workbench for stored procedures


The default options need to be set for stored procedure development. We will enable workbench capabilities for database development.

Create stored procedure


In the Data Definition view you can use the New Stored Procedure wizard to create DB2 stored procedures.

Run stored procedure


After you create and build a routine such as a stored procedure, you can run the routine on the DB2 database server from the Data Defintion view.

Debug stored procedures


The SQL stored procedure debugging plug-in enables you to detect and diagnose errors in SQL procedures that are running on a local or remote DB2 server. With the plug-in you can control the execution of your code by setting line and variable breakpoints, suspending execution, stepping through your code, and examining the contents of variables. The SQL Stored Procedure Debug settings must be set in the workbench Preferences.

Introduction to DB2 development with Rational Application Developer Copyright IBM Corporation 1994, 2005. All rights reserved.

Page 9 of 10

developerWorks

ibm.com/developerWorks

Resources
Learn The Rational training site and DB2 training sites list several learning delivery methods for the use of DB2 and Rational. Visit the developerWorks Rational zone and the developerWorks DB2 zone to expand your skills. Stay current with Rational events and DB2 events via developerWorks technical events and Webcasts. Get products and technologies Download a free trial version of DB2 Universal Database Enterprise Server Edition V8.2. Download a free trial version of Rational Application Developer for Websphere Software V6.0. Build your next development project with Rational trial software and DB2 trial software, available for download directly from developerWorks. Discuss Participate in the discussion forum for this content.

About the author


Debra R. Eaton Debra Eaton has worked in DB2 Technical Sales as a Software Information Technology Specialist on the DB2 Migration Team for 10 years. She specializes in converting customer applications from non-DB2 databases to DB2. She has authored redbooks, white papers and DB2 Magazine articles on topics related to DB2 application development. In addition, she has presented these topics at IDUG and DB2 Technical Conferences.

Trademarks
IBM, DB2, DB2 Universal Database, and Rational are trademarks of IBM Corporation in the United States, other countries, or both. Other company, product, or service names may be trademarks or service marks of others.

Introduction to DB2 development with Rational Application Developer Page 10 of 10 Copyright IBM Corporation 1994, 2005. All rights reserved.

S-ar putea să vă placă și