Figure 6-3 Data Definition view Navigator view In the Navigator view Figure 6-4 you can see the local descriptor files .xmi files that represent the database objects.. Creating a databas
Trang 1Database models are stored within an Application Developer project This can be any type of project; for example, a simple project, a Web project with database access, or an EJB project with entity beans
The Data Definition view is a hierarchical view of the database objects and does not display how these definitions are stored in actual files
Figure 6-3 Data Definition view
Navigator view
In the Navigator view (Figure 6-4) you can see the local descriptor files (.xmi files) that represent the database objects Each of the files has an editor associated with it Double-clicking the file brings up the appropriate editor for the type of object that is described, which could be either a database, a schema, or a table
Trang 2Figure 6-4 Navigator view
Using the DB Servers view
You can use the DB Servers view to connect to existing databases and view their objects The objects can be imported into Application Developer and used in your applications
The DB Servers view allows you to filter the designs that are returned to only show a subset of schemas or tables You can also use the DB Servers view to generate DDL files and XML schemas
Creating a database connection
To view the definition of an existing database, you first have to create a JDBC connection to the database To create a new connection, first make sure you are
in the DB Servers view Then right-click anywhere in the view and select New Connection to display the Database Connection wizard (Figure 6-5).
Important: The DB Servers view is read-only Before you can edit any
database objects, you have to import them into an Application Developer project
Note: The examples in this section assume that you have created and
populated the DB2 tables as described in “Installing the EJBBANK database”
on page 811
Trang 3Figure 6-5 Creating a JDBC connection You have to provide a unique name for the connection (Con1 in our example), a user ID and password if required, and the type of database you are connecting
to Also, you must specify which JDBC driver should be used There are two predefined JDBC drivers for DB2:
IBM DB2 APP DRIVER (COM.ibm.db2.jdbc.app.DB2Driver) for connections
to local databases or remote databases defined locally with the DB2 Client Configuration Assistant
IBM DB2 NET DRIVER (COM.ibm.db2.jdbc.net.DB2Driver) for connections
to remote DB2 databases
See “DB2 JDBC drivers” on page 175 for more details on DB2 JDBC drivers
Trang 4If you do not want to use one of the predefined drivers, you can select Other Driver from the JDBC driver drop-down, and fill in the JDBC driver class field with the driver name
If you want to limit the schemas, tables, stored procedures or user-defined
functions returned, click the Filters button (Figure 6-5 on page 146), and the Connection Filters dialog, shown in Figure 6-6, is displayed
Figure 6-6 Creating a JDBC connection: filter
The dialog has four pages to define filters for schemas, tables, stored
procedures, and user-defined functions By default, one schema filter is
predefined (SCHEMA NOT LIKE SYS%) by the selection Exclude system schemas For our example we are limiting the selection to tables with names starting with
CU To create the filter, select the Table tab and click Add Filter, and the Add Filter dialog shown in Figure 6-7 opens
Note: Application Developer now contains support for additional database
systems, such as Cloudscape™, Oracle, Microsoft® SQL Server, Informix, Sybase, InstantDB, and MySQL
Trang 5Figure 6-7 Adding a new filter Enter CU% in the entry field and click OK You can modify the filter by clicking any cell in the table and changing the value Click OK to close the filter dialog After clicking Finish in the connection window, the connection is created and a new database entry is added to the DB Servers view You can expand the new
node to see the schemas and tables that are now available for use (Figure 6-8)
Figure 6-8 DB Servers view of EJBBANK database with table filter applied Next we create another connection without filters We will use this connection in the next section to import database objects into our workspace
Right-click inside the DB Servers view, and select New Connection from the context menu The new connection wizard opens, already containing the default values for connection name (Con2) and database name (EJBBANK) By default, no filters are entered All you have to do is click Finish to create this connection Figure 6-9 shows the DB Servers view, which now contains the new connection
Trang 6Figure 6-9 Updated DB Servers view with two connections
Importing database objects
In the DB Servers view you can browse the tables and columns, but before you
can actually use them in your application, you have to import them into a folder in
a project
Simple project
We use a simple project to store the database definitions A simple project is a generic project that contains files and folders To create the project select File -> New -> Project Then select Simple and Project from the New Project dialog and
click Next Enter ItsoProGuideDatabase as the project name, and click Finish The Resource perspective opens However, we will continue to work in the Data perspective, so we close the Resource perspective
Import database
We now import the EJBBANK database into the new project:
In the Data perspective, DB Servers view, select the connection Con2 Import
to Folder (context)
In the Import dialog (Figure 6-10) click Browse to locate the ItsoProGuideDatabase project, then click Finish
Trang 7Figure 6-10 Import database objects
In the Data Definition view (Figure 6-11) expand the EJBBANK node The same database objects are shown, but you can now open editors on them to view and modify their definitions
In the Navigator view you will notice that a number of XMI files have been created
for the database objects (XMI is an open information interchange model that allows developers who work with object technology to exchange programming data over the Internet in a standardized way.)
Figure 6-11 Imported database objects in Data Definition and Navigator views
If you double-click one of these files, the appropriate object editor opens If you want to see the XMI source, you can right-click any of the files and select Open
Trang 8Generate DDL and XML schema files
Application Developer allows you to generate DDL files and XML Schemas for
database objects To generate a DDL file, select the database object in the Data Definition or DB Servers view and Generate DDL (context) to open the Generate SQL DDL wizard, shown in Figure 6-12 You can generate DDL for the database, for a schema, or for individual tables
Figure 6-12 Generate DDL for a database object Enter the name of the folder where you want the generated sql file to be stored, select options for the generation and whether you want to open the SQL editor on the generated file If you elect not to open the editor, you will have to switch to the
Navigator view to see the generated file The generated DDL file is shown in
Example 6-1
Example 6-1 Generated EJBBANK.sql file (extract) Generated by Relational Schema Center on Mon Mar 17 01:07:11 PST 2003 CREATE SCHEMA ITSO;
CREATE TABLE ITSO.ACCOUNT (ACCID CHARACTER(8) NOT NULL, BALANCE DECIMAL(8, 2) NOT NULL, INTEREST INTEGER NOT NULL, ACCTYPE VARCHAR(8) NOT NULL, DISCRIMINATOR CHARACTER(1) NOT NULL, OVERDRAFT DECIMAL(8, 2) NOT NULL,
Trang 9MINAMOUNT DECIMAL(8, 2) NOT NULL);
ALTER TABLE ITSO.ACCOUNT ADD CONSTRAINT SQL030221140126480 PRIMARY KEY (ACCID);
CREATE TABLE ITSO.CUSTACCT (CUSTOMERID INTEGER NOT NULL, ACCID CHARACTER(8) NOT NULL);
ALTER TABLE ITSO.CUSTACCT ADD CONSTRAINT SQL030221140126190 PRIMARY KEY (CUSTOMERID, ACCID);
CREATE TABLE ITSO.CUSTOMER (CUSTOMERID INTEGER NOT NULL, TITLE CHARACTER(3) NOT NULL, FIRSTNAME VARCHAR(30) NOT NULL, LASTNAME VARCHAR(30) NOT NULL, USERID CHARACTER(8),
PASSWORD CHARACTER(8), ADDRESS BLOB(2000));
ALTER TABLE ITSO.CUSTOMER ADD CONSTRAINT SQL030221140125890 PRIMARY KEY (CUSTOMERID);
ALTER TABLE ITSO.CUSTACCT ADD CONSTRAINT "CAtoAccount" FOREIGN KEY (ACCID) REFERENCES ITSO.ACCOUNT(ACCID)
ON DELETE RESTRICT
ON UPDATE NO ACTION;
ALTER TABLE ITSO.CUSTACCT ADD CONSTRAINT "CAtoCustomer" FOREIGN KEY (CUSTOMERID) REFERENCES ITSO.CUSTOMER(CUSTOMERID)
ON DELETE RESTRICT
ON UPDATE NO ACTION;
XML schemas can be generated for tables To generate an XML schema for a
table, you must already have imported it into a folder and be in the Data
Definition view
Select the CUSTOMER table and Generate XML Schema from the context menu, and the Create XML Schema dialog opens (Figure 6-13)
Trang 10Figure 6-13 Create XML schema for database table
Click Finish and the schema file (with extension xsd) is created and opened in the XML schema editor The content of the customer XSD file (visible in the Source tab of the editor) is shown in Example 6-2
Example 6-2 Generated XML schema
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.ibm.com/EJBBANK/ITSO"
xmlns:EJBBANKITSO="http://www.ibm.com/EJBBANK/ITSO">
<element name="CUSTOMER_TABLE">
<complexType>
<sequence>
<element ref="EJBBANKITSO:CUSTOMER" minOccurs="0"
maxOccurs="unbounded"/> </sequence>
</complexType>
<key name="CUSTOMER_PRIMARYKEY">
<selector xpath="EJBBANKITSO:CUSTOMER"/>
<field xpath="CUSTOMERID"/>
</key>
</element>
<element name="CUSTOMER">
<complexType>
<sequence>
<element name="CUSTOMERID" type="integer"/>
<element name="TITLE">