IBM WebSphere and VisualAge for Java Database Integration with DB2, Oracle, and SQL Server

An IBM Redbook Publication
IBM Redbook Form Number: SG24-5471-00
ISBN: 0738414190
ISBN: 9780738414195
Publication Date: 16-Sep-1999
Find Similar Download

Related People

Ueli Wahli - Author [+3] [-3]
Tobias Himstedt - Author
Sean Lee - Author
Amir Razmara - Author

Abstract

With the move of Java applications to a servlet runtime environment
on a Web server the access of relational databases on the Web server
becomes one of the most important topics.

IBM WebSphere is a set of software products that help customers
develop and manage high-performance Web sites to ease the transition
from simple Web publishing to advanced e-business applications.

VisualAge for Java Enterprise is the premier Java integrated
development environment that helps customers connect their Java
applications to their enterprise data. VisualAge for Java provides
an integrated WebSphere Test Environment that makes development
and deployment of servlets to WebSphere fast and easy.

In this IBM Redbooks publication we describe many ways of how relational database
systems can be integrated with the VisualAge for Java development
environment and the WebSphere execution environment.

First, we describe the installation of the underlying base products
IBM WebSphere and VisualAge for Java. Next, we describe the
installation and setup of the database systems and JDBC drivers for
the three relational database management systems DB2 Universal
Database, Oracle, and Microsoft SQL Server. Finally, we provide
small programming examples for JDBC, data access beans, the
Persistence Builder, Enterprise JavaBeans, WebSphere Studio,
User Profiles, Security and Directory Services, and SQLJ.

This book is especially suited for Java application designers
that are implementing servlets with relational database access.

Language

English

Table of Content

Part 1. Introduction to IBM WebSphere and VisualAge for Java
Chapter 1. HTTP Server and WebSphere Application Server
Chapter 2. VisualAge for Java Enterprise Version 2
Chapter 3. ITSO Sample Applications

Part 2. Database Setup and JDBC
Chapter 4. Relational Database Installation and Setup
Chapter 5. Java Database Connectivity

Part 3. Database Programming Examples
Chapter 6. JDBC Programming Examples
Chapter 7. IBM Connection Manager
Chapter 8. Data Access Beans: VisualAge for Java
Chapter 9. Persistence Builder: VisualAge for Java
Chapter 10. Enterprise JavaBeans
Chapter 11. WebSphere Studio
Chapter 12. WebSphere User Profile
Chapter 13. WebSphere Security and the LDAP Directory
Chapter 14. Using SQLJ to Access a Database
SG24-5471-00
International Technical Support Organization
www.redbooks.ibm.com
IBMWebSphere and VisualAge for Java
Database Integration with DB2,
Oracle,and SQL Server
Ueli Wahli,Tobias Himstedt,Sean Lee,Amir Razmara


IBM WebSphere and VisualAge for Java
Database Integration with DB2,
Oracle,and SQL Server
September 1999
SG24-5471-00
International Technical Support Organization

© Copyright International Business Machines Corporation 1999.All rights reserved.
Note to U.S Government Users – Documentation related to restricted rights – Use,duplication or disclosure is
subject to restrictions set forth in GSA ADP Schedule Contract with IBM Corp.
First Edition (September 1999)
This edition applies to
• IBM WebSphere Application Server Version 2.0.2
• IBM WebSphere Studio Version 3.0 beta 2
• VisualAge for Java Version 2 with Rollup 2 and Enterprise Update
• DB2 Universal Database Version 5.2
• Oracle Version 8
• Microsoft SQL Server Version 7
for use with the Windows NT Operating System,Service Pack 4.
Sample Code on the Internet:
Comments may be addressed to:
IBMCorporation,International Technical Support Organization
Dept.QXXE Building 80-E2
650 Harry Road
San Jose,California 95120-6099
When you send information to IBM,you grant IBMa non-exclusive right to use or distribute the
information in any way it believes appropriate without incurring any obligation to you.
The sample code for this redbook is available as 5471samp.zip file on the ITSO home page on the
Internet:
ftp://www.redbooks.ibm.com/redbooks/SG245471/
Download the sample code and read Chapter 3,“ITSO Sample Applications” on page 15.
Before using this information and the product it supports,be sure to read the general information in
Appendix B,“Special Notices” on page 269.
Take Note!

© Copyright IBM Corp.1999
iii
Contents
Figures..................................................ix
Tables..................................................xvii
Preface.................................................xix
The Team That Wrote This Redbook...............................xx
Comments Welcome............................................xxi
Part 1.Introduction to IBMWebSphere and VisualAge for Java.1
Chapter 1.HTTP Server and WebSphere Application Server....3
Environment...................................................3
IBMHTTP Server...............................................4
Installation.................................................4
Configuration...............................................4
IBMWebSphere Application Server................................5
WebSphere Installation.......................................5
WebSphere Configuration.....................................6
WebSphere Configuration for JDBC.............................7
Starting and Stopping WebSphere..............................8
Chapter 2.VisualAge for Java Enterprise Version 2............9
VisualAge for Java Installation...................................10
VisualAge for Java Configuration for JDBC.........................10
VisualAge for Java Configuration for WebSphere....................10
WebSphere in VisualAge for Java.................................11
Default Directories for Servlets and JSP files....................11
Compiled JSPs.............................................12
Running Servlets in VisualAge for Java.........................12
Chapter 3.ITSO Sample Applications........................15
Sample Code...............................................16
Repository Export Files......................................17

iv
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Part 2.Database Setup and JDBC.............................19
Chapter 4.Relational Database Installation and Setup........21
DB2 UDB Installation..........................................22
DB2 UDB Database Setup....................................22
DB2 UDB Table Creation.....................................24
Loading the Table Data Into DB2..............................26
DB2 JDBC Drivers..........................................26
Oracle 8 Installation............................................27
Oracle Database Setup.......................................27
Oracle Table Creation.......................................29
Importing the Table Data into Oracle...........................31
Oracle JDBC Drivers........................................32
SQL Server Installation.........................................33
SQL Server Database Setup..................................33
SQL Server Table Creation...................................34
Importing the Table Data into SQL Server.......................35
SQL Server JDBC Drivers....................................38
ODBC Data Source Setup for JDBC............................38
Chapter 5.Java Database Connectivity.......................43
DB2 UDB JDBC Drivers........................................44
DB2 Application Driver......................................44
DB2 Network Driver.........................................45
Oracle JDBC Drivers...........................................46
Oracle Thin Client Driver....................................46
Oracle OCI Driver...........................................46
SQL Server JDBC Drivers.......................................47
Configuring VisualAge for Java for JDBC..........................48
Configuring WebSphere for JDBC.................................49
Part 3.Database Programming Examples.....................51
Chapter 6.JDBC Programming Examples....................53
Simple JDBC Application........................................53
DB2 Universal Database JDBC Code Example......................56
Oracle JDBC Code Example......................................57
SQL Server JDBC Code Example.................................58
Running Applications with JDBC.................................60
Testing the JDBC Applications in VisualAge for Java..............60
Running the JDBC Applications in the Operating System..........60
Simple JDBC Servlet...........................................61

v
Testing the Servlet in VisualAge for Java.......................63
Running the Servlet in WebSphere.............................63
Chapter 7.IBM Connection Manager.........................65
Configuration.................................................67
Connection Pool............................................67
IBMConnection Manager and Pure JDBC..........................69
IBMConnection Manager Examples...............................70
Using the IBMConnection Manager............................70
HTML File for Accessing the Servlets...........................71
Running the Example........................................72
DbaseConnMgr Class........................................73
DB2......................................................77
Oracle....................................................79
Microsoft SQL Server........................................79
Chapter 8.Data Access Beans:VisualAge for Java............81
Loading the Data Access Bean Feature.............................82
Data Access Beans and the Standard SQL Classes...................82
DatabaseConnection and java.sql.Connection....................83
Statement Types............................................85
ResultSet Types............................................85
Building a Servlet using Data Access Beans.........................86
Sample Data Access Beans...................................86
Creating a Servlet Visually using Data Access Beans..............86
Using the Connection Manager and Data Access Beans...............98
Class Definition............................................98
Initialization...............................................99
Form Parameter Access.....................................100
Form Processing...........................................101
Employee Retrieval.........................................102
Employee Update..........................................105
Testing the Servlet.........................................106
Deploying Servlets to WebSphere................................107
Chapter 9.Persistence Builder:VisualAge for Java..........109
What is Persistence?...........................................110
Using Databases to Provide Persistence...........................110
Persistence Builder Tools.......................................110
Vendor Specific Considerations..................................111
Persistence Builder Example:Employees in Department.............112
Import the Schema.........................................112
Generate and Validate the Object Model.......................115
Inspect the Map...........................................116

vi
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Generate the Java Code for the Object Model...................118
Generate the Java Code for the Service Classes..................119
Build the Servlet...........................................121
Changing the Datastore Database................................127
Deploying a Servlet to WebSphere...............................128
Chapter 10.Enterprise JavaBeans..........................131
Enterprise JavaBeans from a Bird’s-Eye Perspective................132
A Closer Look................................................132
Developing a Container Managed Persistence Entity Bean...........133
Setup of VisualAge for Java..................................134
Create a Department Enterprise Bean.........................135
Add the Properties and Methods to the Bean....................137
Map the Bean to the Schema.................................140
Generate the Code and Test..................................143
Deploy the Enterprise Bean to WebSphere.........................149
Access the Department Bean from a Servlet........................153
Class Declaration..........................................153
PerformTask Method.......................................154
Initialization..............................................154
Retrieve a Department Bean.................................155
Create or Find a Department Bean............................157
Update the Department Bean................................157
Test the Servlet in VisualAge for Java.........................158
Test the Servlet in WebSphere...............................159
Access the Department Bean from an Application...................160
Create the GUI Layout......................................160
Create the Connections.....................................161
Test the Applet or Application................................163
Developing a Bean Managed Persistence Entity Bean................164
Create an Employee Enterprise Bean with the SmartGuide........164
Add Properties and Methods.................................165
Provide the Employee Bean with Persistence Functionality........166
The EmployeeStorage Class..................................169
Testing the Employee Bean..................................174
Access the Employee Bean from a Servlet......................174
Deployment of the Employee Bean to WebSphere................177
Chapter 11.WebSphere Studio..............................179
WebSphere Studio Tools........................................180
Installation and Configuration..................................181
Project Setup.................................................181
Create an SQL Statement and Servlet............................183

vii
Create the Servlet and JSPs....................................188
Project View after the Database Wizard........................192
Publish the Project............................................194
Publishing Configurations...................................194
Assembly Stages...........................................196
Publishing Targets.........................................196
Testing the Generated Servlet...................................197
Enhanced Insert Example......................................198
Create an SQL Statement...................................198
Create a Servlet...........................................200
Viewing the Result.........................................200
Determine all Available Department Numbers..................201
Invoke the Servlet froma JSP................................202
Save the Modified HTML Page as a JSP........................207
Modify the Generated Servlet................................207
Test the JSP and the Servlet in WebSphere.....................208
Debugging the Servlets in VisualAge for Java......................210
Hints and Tips for WebSphere Studio.............................211
Synchronization between Development and Publishing View.......211
Checkout.................................................211
Publishing Warnings About Old Class Files.....................211
Mismatch between Servlet and Servlet Configuration.............211
Project View.................................................212
Chapter 12.WebSphere User Profile.........................213
Description..................................................214
Configuration................................................214
Enable Page..............................................214
Database Page............................................215
Connection Manager Page...................................217
User Profile Create,Update and Retrieve..........................218
Create...................................................218
Update...................................................219
Retrieve..................................................219
UserProfile Example...........................................220
Classes and Methods.......................................220
Output Result.............................................228
XML,HTML,and JSP Files..................................230
Testing the User Profile Sample in VisualAge for Java............235
Running the User Profile Sample in WebSphere.................236
Chapter 13.WebSphere Security and the LDAP Directory....237
Introduction..................................................238

viii
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Access Control Lists........................................238
Realms...................................................239
Resources.................................................240
Setup and Configuration.......................................241
Chapter 14.Using SQLJ to Access a Database................247
What is SQLJ?...............................................248
SQLJ and VisualAge for Java...................................248
SQLJ Translators,Input,and Output.............................249
Updating Serialized Profiles....................................249
DB2 Resources for SQLJ.......................................250
Using the SQLJ Translator with DB2.............................250
Employees in Department Example..............................250
Servlet...................................................251
Bean with SQLJ Code Before Translation......................252
SQLJ Code After Translation................................254
Deployment of an SQLJ Servlet to WebSphere.....................258
SQLJ Servlet Output..........................................259
Appendixes.................................................261
Appendix A.Odds and Ends.................................263
Different Mapping of SQL Types.................................264
Correct Deployment of Enterprise Beans..........................266
Data Access Beans............................................266
WebSphere Studio.............................................267
Appendix B.Special Notices................................269
Appendix C.Related Publications...........................273
International Technical Support Organization Publications...........274
Redbooks on CD-ROMs.........................................275
Other Publications............................................275
How to Get ITSO Redbooks...............................277
IBMRedbook Fax Order Form...................................278
List of Abbreviations.....................................279
Index...................................................281
ITSO Redbook Evaluation................................285

© Copyright IBM Corp.1999
ix
Figures
1.WebSphere Administrator Login Panel........................6
2.WebSphere Administration Panel.............................7
3.Services Panel:Starting and Stopping WebSphere...............8
4.Add Feature to VisualAge for Java Environment...............10
5.Feature Selection Panel....................................11
6.Class Path Setup for the WebSphere Test Environment..........13
7.Sample Console Output from SERunner......................14
8.Creating a New Database using the Control Center.............22
9.Create Database SmartGuide...............................23
10.Add User to a Database....................................24
11.DB2 Table Creation.......................................25
12.DB2 Sample Data Extract..................................26
13.Creating a New User for Oracle.............................28
14.Assigning a Role to an Oracle User...........................28
15.Oracle Table Creation.....................................29
16.Oracle Table Creation Output...............................30
17.Import Data For The Employee Table........................31
18.Oracle Sample Data Extract................................32
19.Create a New Database....................................33
20.SQL Server Table Creation.................................35
21.Import Data for Department Table...........................36
22.Choose a Destination......................................37
23.Select the Destination Table................................37
24.Importing Data Was Successful..............................38
25.ODBC Data Source Administrator...........................39
26.Create New Data Source...................................39
27.Create New Data Source to SQL Server.......................40
28.Create New Data Source to SQL Server (Database Setup)........41
29.Category 2 JDBC Driver,Vendor Specific Bridge...............44
30.Category 3 JDBC Driver,Generic Network Protocol.............45
31.Editing VisualAge for Java’s Class Path.......................48
32.JDBC Drivers in WebSphere Class Path......................49
33.Simple Class for JDBC Access...............................54
34.JDBC Example Output....................................55
35.DB2 UDB JDBC Code Example.............................56
36.Oracle JDBC Code Example................................57
37.ODBC DSN Authorization..................................58
38.ODBC Data Source Administrator...........................59

x
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
39.SQL Server JDBC Example Code............................59
40.Simple Servlet doGet Method...............................62
41.Simple Servlet Output in a Web Browser......................64
42.Interaction Flow Between the Connection Manager and a Servlet..66
43.Connection Management on WebSphere......................67
44.Connection Pool Configuration..............................68
45.HTML for the Connection Manager Servlet Examples...........71
46.Launching a Servlet.......................................72
47.Exporting a Servlet.......................................73
48.DbaseConnMgr Class Declaration............................73
49.DbaseConnMgr init Method.................................74
50.DbaseConnMgr initConnection Method.......................74
51.DbaseConnMgr getConnection Method........................74
52.DbaseConnMgr releaseConnection Method....................75
53.DBaseConnMgr executeSelect Method........................75
54.DBaseConnMgr outputInfo Method..........................75
55.doGet Method............................................76
56.UdbConnMgr Class Declaration.............................77
57.UdbConnMgr getDriver Method.............................77
58.UdbConnMgr getPoolName Method..........................77
59.UdbConnMgr getUrl Method................................78
60.Connection Manager Output................................78
61.OracleConnMgr Class Declaration...........................79
62.SqlSrvConnMgr Class Declaration...........................79
63.Data Access Beans Architecture.............................83
64.Internal Connection Management by DatabaseConnection.......84
65.External Connection Management by IBMConnectionManager...84
66.Select Statement Design...................................85
67.Servlet User Interface.....................................87
68.The Servlet Palette........................................88
69.Servlet with Data Access Bean and Select Bean................88
70.UdbEmpInDept Connection Setup...........................89
71.Connection Alias Setup....................................90
72.UdbEmpInDept SQL Setup.................................90
73.Creating New SQL Specification.............................91
74.Selecting a Table for the Query..............................91
75.Setting Query Condition(s).................................92
76.Selecting Columns to Include for a Query.....................93
77.SQL Query Statement.....................................94
78.Resulting SQL Query......................................95
79.UDB Servlet Using Data Access Beans:Visual Composition......96
80.Tailored Method with Short Columns Names..................97
81.Output Result of the Servlet in a Web Browser.................97

xi
82.EmployeeServlet Class Declaration..........................98
83.EmployeeServlet init Method...............................99
84.EmployeeServlet getParameter Method......................100
85.EmployeeServlet doGet and doPost Methods..................101
86.EmployeeServlet performTask Method.......................101
87.EmployeeServlet getEmployee Method (I)....................102
88.EmployeeServlet getEmployee Method (II)...................102
89.getEmployee Method (III).................................103
90.EmployeeServlet getEmployee Method (IV)...................103
91.Output of the getEmployee Method.........................104
92.EmployeeServlet getEmployee Method (V)....................104
93.EmployeeServlet getEmployee Method (VI)...................104
94.EmployeeServlet updateEmployee Method (I).................105
95.EmployeeServlet updateEmployee Method (II)................105
96.EmployeeServlet updateEmployee Method (III)................105
97.EmployeeServlet updateEmployee Method (IV)................106
98.EmployeeServlet updateEmployee Method (IV)................106
99.Properties for the Employee Servlet.........................107
100.Importing a Schema......................................112
101.Import Table Selection....................................113
102.Schema Browser.........................................114
103.Schema Column Editor...................................114
104.Model Browser..........................................115
105.Attribute Editor:Marking the Value Required Field............116
106.Map Browser with a Broken Table Map......................117
107.Model Browser..........................................117
108.Generation Options......................................118
109.Generation Options (Bean Properties).......................119
110.Schema Generation Selection..............................120
111.Database Connection Information...........................121
112.HTML Output Page of a Servlet............................122
113.Visual Composition Editor with Persistence Builder Palette.....123
114.Composition Editor with Connections........................124
115.Column Identifiers.......................................125
116.Servlet Test Output......................................126
117.Establishing Connections to Multiple Database Vendors........127
118.Exporting the Servlet.....................................128
119.WebSphere Application Server:Java Engine Setup............129
120.Available Features.......................................134
121.EJB Development Pane:Adding an EJB Group................135
122.EJB SmartGuide........................................136
123.Adding a Field...........................................137
124.Switching to the Fields Pane...............................138

xii
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
125.Add Methods to the Remote Interface........................139
126.Marking Properties Container Managed.....................140
127.Connection Information for the Schema Import................140
128.Select the Table to Import.................................141
129.Schema Browser after Import..............................141
130.Specifying the Datastore Map for the Enterprise Bean..........142
131.Map Browser............................................142
132.The Property Map Editor..................................143
133.EJB Server Configuration Dialog...........................144
134.EJB Server Properties....................................144
135.EJS Console Output......................................145
136.Test Client..............................................146
137.The Home Interface......................................146
138.Parameter Specification for the DepartmentKey...............147
139.Test Client Presenting the Remote Interface..................148
140.WebSphere General EJB Settings...........................150
141.WebSphere Container EJB Settings.........................151
142.EJB Jar File Pane.......................................151
143.Redeployment Warning...................................152
144.DepartmentEJBServlet Class Declaration....................154
145.DepartmentEJBServlet performTask Method.................154
146.DepartmentEJBServlet init Method.........................155
147.DepartmentEJbServlet getDepartment Method...............156
148.DepartmentEJBServlet createOrFindDepartment Method......157
149.DepartmentEJBServlet updateDepartment Method............158
150.DepartmentEJBServlet in a Browser........................159
151.Department Panel with All Connections.....................160
152.DepartmentPanel createOrFind Method.....................162
153.Department Panel Displaying the Department Bean...........163
154.Adding a BMP Bean......................................164
155.Properties in the Workspace View...........................165
156.EmployeeBean ejbFindByPrimaryKey Method................167
157.EmployeeBean ejbStore Method............................167
158.EmployeeBean Methods...................................168
159.EmployeeStorage Class Declaration.........................169
160.Initialize the Select Statement.............................170
161.EmployeeBean ejbStore Method............................171
162.Find an Employee........................................172
163.Update the Database.....................................173
164.Retrieve and Display the Employee Bean.....................175
165.Set the Properties of the Employee Bean.....................176
166.EmployeeEJBServlet Output...............................176
167.Create New Project.......................................181

xiii
168.Specify Project Parameters................................182
169.WebSphere Studio Project Window..........................182
170.WebSphere Studio Tool Bar................................183
171.Specify a Name for the SQL Statement......................183
172.Specify the Connection Parameters.........................184
173.Select Tables from the Database............................185
174.Specify the Join..........................................185
175.Specify the Columns to Display.............................186
176.Specify the Conditions....................................187
177.Completed SQL Statement................................187
178.Select the SQL Statement.................................188
179.Select the Generated Pages................................189
180.Specify Input Parameters.................................189
181.Specify the Fields for the Result Page........................190
182.Specify to Use the Bean in a Session.........................191
183.Provide Package Name and Class Name Prefix................191
184.Finish the Wizard........................................192
185.Project after Servlet Generation............................192
186.Specify Publishing Options................................194
187.Publishing Server Properties...............................195
188.Publishing Targets.......................................196
189.Select Preview fromthe Context Menu.......................197
190.Generated HTML Input Page..............................197
191.Generated HTML Output Page.............................198
192.Creating an Insert Statement..............................199
193.Select the Input Fields....................................199
194.Generated SQL Insert Statement...........................200
195.Insert a New Employee...................................201
196.Select the DEPNO Column................................202
197.WebSphere Page Designer.................................203
198.Specify the Servlet to be Invoked...........................203
199.Specify Bean Parameters..................................204
200.Resulting Page..........................................205
201.Selecting the Loop Property................................206
202.Generated Code for Dynamic Item Values....................207
203.Enhanced Insert Servlet Input Page with Combobox...........209
204.Complete Project View....................................212
205.Enabling User Profile.....................................215
206.UserProfile Database Setup for DB2.........................216
207.UserProfile Database Setup for Oracle.......................216
208.UserProfile Database Setup for SQL Server...................217
209.UserProfile’s Connection Pool Selection......................217
210.itsowasdb.userprofile Class Listing..........................220

xiv
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
211.UserProfileCartServlet Methods Listing.....................221
212.UserProfileCartServlet Class Declaration....................221
213.UserProfileCartServlet addUserShoppingCart Method.........222
214.UserProfileCartServlet.createUserProfile....................222
215.UserProfileCartServlet doGet Method.......................222
216.UserProfileCartServlet doPost Method.......................223
217.UserProfileCartServlet getParameter Method.................223
218.UserProfileCartServlet itemCreateToOutputPage Method.......224
219.UserProfileCartServlet performTask Method.................225
220.UserProfileCartServlet processLogin Method.................226
221.UserProfileCartServlet retrieveUserProfile Method............227
222.UserProfileCartServlet userCreateToOutputPage Method.......227
223.UserProfile Login Page...................................228
224.UserProfile UserCreate Page...............................228
225.UserProfile Default OutputPage After User Registration........229
226.UserProfile ItemCreate Page...............................229
227.UserProfile Default Output Page After Item Creation..........230
228.UserProfileCartServlet.Servlet Control File...................231
229.UserProfileLogin.html File................................231
230.UserProfileCreate.jsp File.................................232
231.UserProfileCartOutputPage.jsp File.........................233
232.ItemInfoInputPage.jsp File................................234
233.UserProfileCartErrorPage.jsp File..........................234
234.User Profiles Properties File...............................235
235.Relationships Between Realm,ACL,and Resource.............239
236.WebSphere Administration Setup for Directory Management....241
237.WebSphere Access Control List Main Panel...................242
238.Adding a New Access Control List..........................242
239.Add Permission to ACL...................................243
240.WebSphere Access Control List Main Panel with New ACL......244
241.WebSphere Resources Security Setup........................244
242.Protect a Resource using Directory Service through ACL........245
243.Resource Panel after Adding a Resource to be Protected.........245
244.SQLJ Example Servlet (SqljServ.java).......................251
245.SQLJ Example Servlet Bean (SqljServBean.sqlj) Part 1.........252
246.SQLJ Example Servlet Bean (SqljServBean.sqlj) Part 2.........253
247.SQLJ Code after Translation:Iterator Class Definition.........255
248.SQLJ Code after Translation:Simple Select Statement.........256
249.SQLJ Code after Translation:Select Without Result Set........257
250.WebSphere Application Server:Java Engine Setup............258
251.Output of SQLJ Servlet Employees in Department Example.....259
252.Default Mapping for the Employee Table in DB2..............264
253.Default Mapping for the Employee Table in Oracle.............265

xv
254.Default Mapping for the Employee in ODBC/SqlServer.........265
255.WebSphere Studio Database Wizard........................267

xvi
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

© Copyright IBM Corp.1999
xvii
Tables
1.Directories and Files with Sample Code.........................16
2.Models and Packages in the Repository Samples..................17
3.Properties of the Employee Bean.............................165

xviii
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

© Copyright IBM Corp.1999
xix
Preface
With the move of Java applications to a servlet runtime environment on a
Web server the access of relational databases on the Web server becomes one
of the most important topics.
IBMWebSphere is a set of software products that help customers develop
and manage high-performance Web sites to ease the transition from simple
Web publishing to advanced e-business applications.
VisualAge for Java Enterprise is the premier Java integrated development
environment that helps customers connect their Java applications to their
enterprise data.VisualAge for Java provides an integrated WebSphere Test
Environment that makes development and deployment of servlets to
WebSphere fast and easy.
In this redbook we describe many ways of how relational database systems
can be integrated with the VisualAge for Java development environment and
the WebSphere execution environment.
First,we describe the installation of the underlying base products IBM
WebSphere and VisualAge for Java.Next,we describe the installation and
setup of the database systems and JDBC drivers for the three relational
database management systems DB2 Universal Database,Oracle,and
Microsoft SQL Server.Finally,we provide small programming examples for
JDBC,data access beans,the Persistence Builder,Enterprise JavaBeans,
WebSphere Studio,User Profiles,Security and Directory Services,and SQLJ.
This redbook is especially suited for Java application designers that are
implementing servlets with relational database access.

xx
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
The TeamThat Wrote This Redbook
This redbook was produced by a team of specialists fromaround the world
working at the International Technical Support Organization San Jose
Center.
Ueli Wahli is a Consultant ADSpecialist at the IBMInternational Technical
Support Organization in San Jose,California.Before joining the ITSO 14
years ago,Ueli worked in technical support at IBMSwitzerland.He writes
extensively and teaches IBMclasses worldwide on application development,
object technology,VisualAge products,data dictionaries,and library
management.Ueli holds a degree in Mathematics fromthe Swiss Federal
Institute of Technology.His e-mail address is
wahli@us.ibm.com
.
Tobias Himstedt is a Senior Consultant working for SerCon GmbH,an IBM
Global Service Company in Mainz,Germany.Tobias holds a diploma in
Computer Science from the University of Hildesheim,Germany.He is
co-author of a book about Python,a useful scripting language.Before joining
SerCon he worked for GMD,the German National Research Center for
Information Technology.There,he was mainly involved in a European
research project about electronic commerce.His areas of expertise include
Internet,client/server,object-oriented,and component technologies (e-mail:
tobias.himstedt@sercon.de
).
Sean Lee is an IT Specialist currently engaged in customer consulting in
New York.He is a DB2 Technical Specialist representing the DB2
development lab in San Jose,California.Sean has 2 years of experience in
Information Technologies and holds a degree in Computer Science from the
State University of New York in Binghamton.His areas of expertise include
System/390 database management and connectivity.You can contact Sean at
leese@us.ibm.com
.
Amir Razmara is an IT Specialist in Canada.He has 6 years of experience
in the software development field.He holds a degree in Computer Science
from the York University in Toronto,Canada.His areas of expertise include
object-oriented analysis and design,database servers,client/server and
internet solutions for e-business.His e-mail address is
razmara@ca.ibm.com
.

xxi
Thanks to the following people for their invaluable contributions to this
project:
Joaquin Picon
International Technical Support Organization,San Jose Center
Moncrief Rowe-Anderson
IBMSanta Teresa Lab (DB2 JDBC SQLJ)
Virinder Batra
IBMRaleigh
Comments Welcome
Your comments are important to us!
We want our redbooks to be as helpful as possible.Please send us your
comments about this or other redbooks in one of the following ways:

Fax the evaluation formfound in “ITSO Redbook Evaluation” on page 285
to the fax number shown on the form.

Use the online evaluation form found at http://www.redbooks.ibm.com

Send your comments in an internet note to redbook@us.ibm.com

xxii
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

© Copyright IBM Corp.1999
1
Part 1 Introduction to
IBMWebSphere
and VisualAge
for Java
In this part we briefly discuss the installation of the two products,
IBMWebSphere and VisualAge for Java Enterprise.

2
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

© Copyright IBM Corp.1999
3
1 HTTP Server and
WebSphere
Application Server
In this chapter we describe the IBMHTTP Server and the IBMWebSphere
Application Server.The functionality of these products will be briefly
discussed,including a few hints and tips on how to set themup to run your
servlets inside and outside of the VisualAge for Java environment.
Environment
The examples in this book were developed in the following environment:

PCs with Pentium II 450 MHZ processors and 128 MB RAM

MS Windows NT 4.0 service pack 4

Both Netscape Navigator 4.5 and Microsoft Internet Explorer 4.0 were
used interchangeably through the test and usage of different components.

4
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
IBM HTTP Server
IBMHTTP Server is a Web server which shares its base code with the
Apache Server,with additional enhancements such as the Secure Socket
Layer (SSL).
Installation
IBMHTTP Server Version 1.3.3.1 was used to run the sample code provided
in this book.
Be sure to consider the following during the installation:

Make sure there is a user ID dedicated to be used only by the HTTP
server.

The user ID must be a member of the Administrator group.
Once you have created the user ID proceed with the installation.As you step
through the installation pages enter the user ID created prior to the
installation into the Information for Service Setup panel.
Configuration
To configure the HTTP server you should use the httpd.doc file located under
<drive>:\Program Files\IBM HTTP Server\conf
.You can customize the HTTP
Server using the tags in httpd.conf file.Here is a description on some of those
tags:
DocumentRoot:The directory where the HTTP Server looks for the HTML
files.The default set value is
<drive>:/Program Files/IBM HTTP Server/htdocs
.
Alias:To create a specific location for your project you can take advantage of
the Alias tag.This tag can be set to point to any directory where you wish to
place your HTML and JSP files.If you want to access files which reside in a
location other than the DocumentRoot directory,you can use an Alias to do
this.For example,to be able to access test.html froma c:\itsowww directory,
set
Alias /itso/ "C:/itsowww/"
in the httpd.conf file.This way,to access the
test.html file from the browser the following URL should be used:
http://<server name>/itso/test.html
ServerRoot:The directory where the server's configuration,error,and log
files are kept.
ServerName:If you observe any problems with the IBMHTTP Server it
might help to explicitly set the server name to your host name.

Chapter 1.HTTP Server and WebSphere Application Server
5
IBM WebSphere Application Server
The WebSphere Application Server implements a servlet engine that
functions as a plug-in to extend the capabilities of a number of Web servers.
WebSphere is an application server that enables the “write once,use
anywhere” concept for servlets.
WebSphere can work with number of different Web servers,such as:

IBMHTTP Server

Apache Server 1.3.x

Lotus Domino 5.0

Lotus Domino Go Webserver

Netscape Enterprise Server

Netscape FastTrack Server

Microsoft Internet Information Server
All of the samples provided in this book have used the IBMHTTP Server as
their Web server.
With a properly coded URL,client requests to a Web server are passed on to
the Application Server.The Application Server,in turn,passes the client
request information to a Java servlet,which acts on the request information
and prepares a response that is sent back to the client through the
Application Server and the Web server.
A Java servlet is basically an ordinary piece of Java code with added
capabilities provided by APIs in the javax.servlet and javax.servlet.http
packages.These packages are standard Java extensions available from
JavaSoft for JDK 1.1 and built-in to JDK 1.2.The request/response model is
used by general Web browser and server interactions over the Internet,and
it applies to servlet behavior as well.Because HTTP is a stateless protocol,
one request/response knows nothing about another request/response,even
from the same Web browser,unless additional session state features are
added.
WebSphere Installation
The WebSphere Application Server Version 2.02 was used to run the sample
code provided in this book.
One thing to consider prior to installation is to make sure you are using a
specific IP address instead of an IP address provided through a DHCP server
(we did experience problems with a DHCP address).

6
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
The installation process for WebSphere Application Server is fairly simple.
The only thing you need to know is the Web server that is used with
WebSphere.It is a good idea to install the Web server prior to installing
WebSphere.Also make sure you select the right Web server to work with
WebSphere,because there is no easy way to change this after the
installation.
WebSphere Configuration
To configure different components of WebSphere you use the administration
applet of WebSphere.To gain access on your browser input the following
URL:
http://<server-name>:9527 or http://<ip-address>:9527
.This starts the
applet and displays the main administrator login panel (Figure 1).
Figure 1.WebSphere Administrator Login Panel
From the administration panel you configure different components of
WebSphere (Figure 2).

Chapter 1.HTTP Server and WebSphere Application Server
7
Figure 2.WebSphere Administration Panel
Servlet and Java Class Files Location
The servlet and Java class files should reside under:
<drive>:\WebSphere\AppServer\servlets\<package name (if any)> or
<drive>:\WebSphere\AppServer\classes\<package-name>.
Keep in mind that the classes placed under the servlets directory can be
replaced at any time without the need to restart WebSphere.On the other
hand,the classes placed under the classes directory do require WebSphere to
be restarted when they are replaced.Therefore,the classes directory is the
proper place to store any packages that will most likely not change,while the
servlets directory is the best place during development and testing.
Servlets are addressed with the URL:
http://<server-name>/servlet/packagename.ClassName
WebSphere Configuration for JDBC
To run a servlet with JDBC in WebSphere,we have to make the JDBC
drivers available.See “Configuring WebSphere for JDBC” on page 49 for
detailed instructions.

8
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Starting and Stopping WebSphere
When using the IBMHTTP server it is not necessary to start WebSphere
explicitly.This is done automatically when the HTTP Server is started.
To stop WebSphere use the Control Panel->Services (Figure 3) and stop the
WebSphere Servlet Service.Next,stop the IBMHTTP Server.To restart,just
start the IBMHTTP Server again and verify that the WebSphere Servlet
Service is started as well.
Figure 3.Services Panel:Starting and Stopping WebSphere

© Copyright IBM Corp.1999
9
2 VisualAge for Java
Enterprise Version 2
Because VisualAge for Java Version 2 contains some very nice tools to
develop,debug,and run servlets and Enterprise JavaBeans in a test
environment,VisualAge for Java was used to develop the samples.
In this chapter we briefly describe how to install and configure VisualAge for
Java to work properly with the IBMWebSphere Test Environment.

10
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
VisualAge for Java Installation
The installation consists of three steps:
1.Install VisualAge for Java version 2.0 GA release,reboot,start Visual Age
for Java,and exit.
2.Install fixpack Rollup 2 by unzipping the file rollup2_vaj20_win.zip to the
VisualAge for Java directory (
c:\IBMVAJava
),start VisualAge for Java to
complete the installation,and exit.
3.Install the Enterprise Update,start VisualAge for Java,and exit.
During the installation VisualAge for Java asks for the directory for
HTML files;the default path is
<drive>:\www\html
.This is the location
where the IBMWebSphere Test Environment looks for the HTML files.
VisualAge for Java Configuration for JDBC
To run Java programs with JDBC in VisualAge for Java,we have to make the
JDBC drivers available.See “Configuring VisualAge for Java for JDBC” on
page 48 for detailed instructions.
VisualAge for Java Configuration for WebSphere
Once the installation is complete,you should add the IBMWebSphere Test
Environment feature to the Workbench by selecting Workbench -> File ->
Quick Start (Figure 4).
Figure 4.Add Feature to VisualAge for Java Environment

Chapter 2.VisualAge for Java Enterprise Version 2
11
Select Feature->Add Feature and select IBMWebSphere Test Environment
1.1 (Figure 5).
Figure 5.Feature Selection Panel
If the feature has already been added it will not be listed.Adding the feature
will allow you to run servlets within the VisualAge for Java environment.
WebSphere in VisualAge for Java
In this section we describe the necessary steps involved in setting up and
running servlets inside VisualAge for Java.
Default Directories for Servlets and JSP files
The HTML and JSP files should reside under:
<drive>:\www\html (refer to “VisualAge for Java Installation” on page 10)
The XML servlet configuration files (.servlet) should reside under:
<drive>:\IBMVJava\ide\project_resources\<project name>\<[package name]>

12
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Compiled JSPs
When the JSP file is accessed through a servlet it is translated into a Java
file and its class is executed in the VisualAge for Java environment.The Java
version of the JSP file can be found in the JSP Page Compile Generated Code
project in the VisualAge for Java Workbench.
Running Servlets in VisualAge for Java
There are a couple of steps you have to take to run your Websphere
application.From the Workbench select the com.ibm.servlet package in the
IBMWebSphere Test Environment project.Execute the SERunner class to
start the servlet runtime environment.Depending on the processing power of
your machine this will take between 20 to 120 seconds to get started.
Before running SERunner make sure the project in which the servlet code is
residing is included in the class path of SERunner (Figure 6).
One safe option is to select all the projects and add themto the class path of
the SERunner class.
To run JSPs,the JSP Page Compile Generated Code project must also be
added to the class path.This project does not exist originally;it is created
when the first JSP is compiled.Only then can the project be added to the
class path of the SERunner class.

Chapter 2.VisualAge for Java Enterprise Version 2
13
Figure 6.Class Path Setup for the WebSphere Test Environment
When the WebSphere test environment is up and running you can launch
your servlets by selecting the servlet class and Tools -> Servlet Launcher ->
Launch from the context menu.If the SERunner is not running,this process
will launch the SERunner.After SERunner is launched,a series of messages
is displayed in the Console window of VisualAge for Java.At some point a
message “ServerProcess:Server started” gets logged.Despite what the
message indicates,the server does some further processing before it is fully

14
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
operational.Figure 7 shows the final message in the Console window that
indicate the server is truly up and running.
Figure 7.Sample Console Output from SERunner
Checking if server is already started on port: 8080
***Starting WebSphere Test Environment***
ServerProcess: Start services
ServiceManager: Load service adminservice
.
.
.
.
Servlet reloading is disabled
ServiceManager: Autostarting servletservice
ServiceManager: Autostarting httpservice
ServerProcess: Server started.
.
.
.
servletservice: Load (update):
endpoint.main.port=80
ServiceParameters:
servletservice: Load (update):
endpoint.main.port=80

© Copyright IBM Corp.1999
15
3 ITSO Sample
Applications
The sample code developed for this book is available as a ZIP file on the
Internet:
ftp://www.redbooks.ibm.com/redbooks/SG245471/
The 5471samp.zip file expands into a directory structure containing all the
sample code:
\WasDbBk\Code <=== sample files for book chapters
\WasDbBk\Code\Repository <=== VisualAge for Java export files (.dat)

16
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Sample Code
Table 1 describes the sample code subdirectory structure and the files
contained in those subdirectories.
Table 1.Directories and Files with Sample Code
Subdirectory Files Description
Main directory:\WasDbBk\Code
DBase xxx.ddl Database definition DDL for DB2,
Oracle,and SQL Server
xxx.icx,xxx.csv Sample table data load files
Repository xxx.dat VisualAge for Java repository export
file
itso\wasdb\..pkg xxx.class,xxx.java VisualAge for Java exported classes,
matching the package structure
deployableEJBs xxx.jar VisualAge for Java jar files for
deployment of EJBs to WebSphere
Html xxx.html HTML,JSP,and servlet XML files
Studio xxx.war WebSphere Studio archive file
xxx.java,.html,
.jsp,.servlet,.sql
WebSphere Studio generated files

Chapter 3.ITSO Sample Applications
17
Repository Export Files
The repository subdirectory (
\WasDbBk\code\Repository
) contains one file:
5426samp.dat (contains project ITSO SG24 5471)
Table 2 describes the samples that we developed and the repository packages
where the code is stored.
Table 2.Models and Packages in the Repository Samples
To load the packages into the repository,use the import function of VisualAge
for Java.You can import the whole projects or just individual packages.After
importing you have to load the packages into the Workbench,either into your
own project or by using the ITSO SG24 5471 project.
Package Description
itso.wasdb.jdbc JDBC example and servlet example
itso.wasdb.connmgr Connection manager example
itso.wasdb.accessbean Data access bean example
itso.wasdb.connaccess Data access bean with connection manager
itso.wasdb.persist.metadata Persistence Builder model,schema,map
itso.wasdb.persist Persistence Builder domain classes
itso.wasdb.persist.Services Persistence Builder service classes
itso.wasdb.persist.gui Persistence Builder GUI application
itso.wasdb.ejb Enterprise JavaBeans
itso.wasdb.ejb.client Enterprise JavaBeans client example
itso.wasdb.ejb.servlet Enterprise JavaBeans servlet example
itso.wasdb.studio WebSphere Studio example
itso.wasdb.userprofile WebSphere user profile example
itso.wasdb.sqlj DB2 SQLJ example
ITSO_EJBsEJBReserved Enterprise JavaBeans control information

18
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

© Copyright IBM Corp.1999
19
Part 2 Database Setup
and JDBC
In this part we discuss the installation and setup required for all of the
relational database products and the setup required to create a JDBC
connection to your database.
In our project we used three different relational database management
systems,together with the WebSphere Application Server,WebSphere
Studio,and VisualAge for Java

DB2 UDB Version 5.2

Oracle 8i,Version 8.0.4

Microsoft SQL Server,version 7.0

20
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

© Copyright IBM Corp.1999
21
4 Relational Database
Installation and
Setup
In this chapter we describe the installation and setup of the relational
database products used for the examples throughout this book.For the
purpose of the samples provided,all of the databases do share the same name
and table formats.Because the scripts used to create these databases were
slightly different,all three scripts are posted here.
To standardize the data structures accessed in each of the database products,
we created a simple sample database called ITSOWDB to run queries
against.We created a schema under the ID ITSO.Therefore all of the table
names used are ITSO.<TABLENAME>.The sample data definition language
(DDL) or SQL file for every database is listed under the corresponding
installation section.

22
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
DB2 UDB Installation
We installed DB2 Universal Database for Windows NT Version 5.2.To keep
the version current,UDB fixpack WR09074,also known as fixpack 6,was
installed.
To use the SQLJ functionality,you must install fixpack 8 (see Chapter 14,
“Using SQLJ to Access a Database” on page 247).
DB2 UDB Database Setup
After DB2 is installed,you need to create the ITSOWDB database.You can
either use a GUI with the DB2 Control Center or use a DB2 command
window.
If you are using the Control Center,you select Systems -> [ComputerName] ->
Instances -> DB2 -> Databases.Open the context menu (right click) and
select Create->New (Figure 8).
Figure 8.Creating a New Database using the Control Center
A SmartGuide prompts you for the database name (Figure 9).For the
purpose of our sample code,you do not have to use the other pages of the
SmartGuide dialog.Enter the name and click on Done.

Chapter 4.Relational Database Installation and Setup
23
Figure 9.Create Database SmartGuide
If you are using a DB2 Command Window run the command:
db2 create database itsowdb
If you want to find out more about the create database options,enter:
db2 ? create database
This command lists all of the options available for the particular DB2
command.
User ID
By convention we create all tables under the ITSO user ID (with the
password itso).Because of this you need to add the ITSO user ID to the
database user list.Make sure that the user ID ITSO is already created in the
Windows NT operating system.
The simplest way to to add the user ID to the database is through the Control
Center.Select the ITSOWDB database and open User and Group Objects.In
the context menu (right mouse click) select DB Users and select Add.From
the Add User panel select the ITSO user ID and give it all of the Authorities.
Then click on Apply and then on Close (Figure 10).

24
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 10.Add User to a Database
DB2 UDB Table Creation
When the database is created,the next step is to create the tables in the
database.Figure 11 shows the itsowdb.ddl file file used to create the tables.
To execute this file,open a DB2 Command Window and run the command:
db2 -tvf itsowdb.ddl
Use the proper directory (
c:\WasDbBk\dbase\db2)
for the script file.
Two tables are created:

Department

Employee
These tables definitions are similar to the tables of the DB2 SAMPLE
database.We decided not to use the SAMPLE database,so that we can have
the same database and tables in all three database systems.

Chapter 4.Relational Database Installation and Setup
25
Figure 11.DB2 Table Creation
CONNECT TO itsowdb;
DROP TABLE ITSO.EMPLOYEE;
DROP TABLE ITSO.DEPARTMENT;
----------------- DDL Statements for table ITSO.DEPARTMENT
CREATE TABLE ITSO.DEPARTMENT (
DEPTNO CHAR(3) NOT NULL ,
DEPTNAME VARCHAR(29) NOT NULL ,
MGRNO CHAR(6) ,
ADMRDEPT CHAR(3) NOT NULL ,
LOCATION CHAR(16) );
ALTER TABLE ITSO.DEPARTMENT
ADD PRIMARY KEY (DEPTNO);
----------------- DDL Statements for table ITSO.EMPLOYEE
CREATE TABLE ITSOWDB.ITSO.EMPLOYEE (
EMPNO CHAR(6) NOT NULL ,
FIRSTNME VARCHAR(12) NOT NULL ,
MIDINIT CHAR(1) NOT NULL ,
LASTNAME VARCHAR(15) NOT NULL ,
WORKDEPT CHAR(3) ,
PHONENO CHAR(4) ,
HIREDATE DATE ,
JOB CHAR(8) ,
EDLEVEL SMALLINT NOT NULL ,
SEX CHAR(1) ,
BIRTHDATE DATE ,
SALARY DECIMAL(9,2) ,
BONUS DECIMAL(9,2) ,
COMM DECIMAL(9,2) );
ALTER TABLE ITSO.EMPLOYEE
ADD PRIMARY KEY (EMPNO);
----------------- DDL Statement for foreign key relationship
ALTER TABLE ITSO.EMPLOYEE ADD FOREIGN KEY (WORKDEPT)
REFERENCES ITSO.DEPARTMENT (DEPTNO) ON DELETE RESTRICT;
----------------- Load sample data
LOAD FROM DEPARTMENT.CSV OF DEL INSERT INTO ITSO.DEPARTMENT;
LOAD FROM EMPLOYEE.CSV OF DEL INSERT INTO ITSO.EMPLOYEE;
SET CONSTRAINTS FOR ITSO.EMPLOYEE IMMEDIATE CHECKED;
CONNECT RESET;

26
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Loading the Table Data Into DB2
The load statements at the bottomof Figure 11 load the sample data into the
tables from a data file that uses one line of comma-delimited data for each
row of the tables.
An extract of the sample data (.csv files) is shown in Figure 12.
Figure 12.DB2 Sample Data Extract
DB2 JDBC Drivers
The DB2 JDBC drivers are in the
c:\SQLLIB\java\db2java.zip
file.This file
must be available to Java through the class path.
Department.csv
"A00","SPIFFY COMPUTER SERVICE DIV.","000010","A00", "SAN JOSE"
"B01","PLANNING","000020","A00", "SAN JOSE"
"C01","INFORMATION CENTER","000030","A00", "SAN JOSE"
"D01","DEVELOPMENT CENTER",,"A00", "SAN JOSE"
"D11","MANUFACTURING SYSTEMS","000060","D01", "SAN JOSE"
"D21","ADMINISTRATION SYSTEMS","000070","D01", "SAN JOSE"
"E01","SUPPORT SERVICES","000050","A00", "SAN JOSE"
"E11","OPERATIONS","000090","E01", "SAN JOSE"
"E21","SOFTWARE SUPPORT","000100","E01", "SAN JOSE"
Employee.csv
"000010","CHRISTINE","I","HAAS","A00","3978","1965-01-01","PRES",18,"F","1933-08-24",
0052750.00, 0001000.00, 0004220.00
"000020","MICHAEL","L","THOMPSON","B01","3476","1973-10-10","MANAGER",18,"M","1948-02
-02", 0041250.00, 0000800.00, 0003300.00
"000030","SALLY","A","KWAN","C01","4738","1975-04-05","MANAGER",20,"F","1941-05-11",
0038250.00, 0000800.00, 0003060.00
"000050","JOHN","B","GEYER","E01","6789","1949-08-17","MANAGER",16,"M","1925-09-15",
0040175.00, 0000800.00, 0003214.00
"000060","IRVING","F","STERN","D11","6423","1973-09-14","MANAGER",16,"M","1945-07-07"
, 0032250.00, 0000500.00, 0002580.00
"000070","EVA","D","PULASKI","D21","7831","1980-09-30","MANAGER",16,"F","1953-05-26",
0036170.00, 0000700.00, 0002893.00
"000090","EILEEN","W","HENDERSON","E11","5498","1970-08-15","MANAGER",16,"F","1941-05
-15", 0029750.00, 0000600.00, 0002380.00
"000100","THEODORE","Q","SPENSER","E21","0972","1980-06-19","MANAGER",14,"M","1956-12
-18", 0026150.00, 0000500.00, 0002092.00
"000110","VINCENZO","G","LUCCHESSI","A00","3490","1958-05-16","SALESREP",19,"M","1929
-11-05", 0046500.00, 0000900.00, 0003720.00
"000120","SEAN"," ","O'CONNELL","A00","2167","1963-12-05","CLERK",14,"M","1942-10-18",
0029250.00, 0000600.00, 0002340.00
"000130","DOLORES","M","QUINTANA","C01","4578","1971-07-28","ANALYST",16,"F","1925-09
-15", 0023800.00, 0000500.00, 0001904.00
"000140","HEATHER","A","NICHOLLS","C01","1793","1976-12-15","ANALYST",18,"F","1946-01
-19", 0028420.00, 0000600.00, 0002274.00
"000150","BRUCE"," ","ADAMSON","D11","4510","1972-02-02","DESIGNER",16,"M",
"1947-05-17", 0025280.00, 0000500.00, 0002022.00

Chapter 4.Relational Database Installation and Setup
27
Oracle 8 Installation
We have installed a standard developer installation of Oracle Version 8.0.4
under Windows NT.
Oracle Database Setup
We assume that Oracle is installed.We are now going to create the tables of
the itsowdb database,and fill the tables with sample data.
User ID
By convention,all tables are created under the ITSO user ID,so the first
thing to do is to create the ITSO user.To do that,open the Oracle 8
Navigator.In the tree go all the way down to the User node (Figure 13).
In the General Pane create the user ITSO with password itso;in the
Privileges Pane give the new ITSO user additionally the Resource Role
(Figure 14).
In our installation the Oracle installer did not copy the JDBC driver files.
Make sure those driver files are installed if you plan to access an Oracle
database using JDBC.We copied the driver files manually from the
installation CD from
win32/v8/Jdbc
to
c:\orant\Jdbc
,which was the home of
our Oracle8 installation.
Moreover,if you later want to use the OCI Driver (see “Oracle OCI Driver”
on page 46) you either have to put
c:\orant\Jdbc\lib
in the systempath or
place the oci804jdbc.dll somewhere in the path,for example,
c:\WinNT\system32
.
Notice

28
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 13.Creating a New User for Oracle
Figure 14.Assigning a Role to an Oracle User

Chapter 4.Relational Database Installation and Setup
29
Oracle Table Creation
Now that the user is created,the next step is to create the database tables.
You will notice that we have not yet created a database ourselves.During the
installation of Oracle,a database is created that holds all the tables.
Figure 15 lists the SQL statements used to create the ITSOWDB tables.
Figure 15.Oracle Table Creation
CONNECT itso/itso;
DROP TABLE ITSO.EMPLOYEE;
DROP TABLE ITSO.DEPARTMENT;
----------------- DDL Statements for table ITSO.DEPARTMENT
CREATE TABLE ITSO.DEPARTMENT (
DEPTNO CHAR(3) NOT NULL ,
DEPTNAME VARCHAR(29) NOT NULL ,
MGRNO CHAR(6) ,
ADMRDEPT CHAR(3) NOT NULL ,
LOCATION CHAR(16) );
ALTER TABLE ITSO.DEPARTMENT
ADD PRIMARY KEY (DEPTNO);
----------------- DDL Statements for table ITSO.EMPLOYEE
CREATE TABLE ITSO.EMPLOYEE (
EMPNO CHAR(6) NOT NULL ,
FIRSTNME VARCHAR(12) NOT NULL ,
MIDINIT CHAR(1) NOT NULL ,
LASTNAME VARCHAR(15) NOT NULL ,
WORKDEPT CHAR(3) ,
PHONENO CHAR(4) ,
HIREDATE DATE ,
JOB CHAR(8) ,
EDLEVEL SMALLINT NOT NULL ,
SEX CHAR(1) ,
BIRTHDATE DATE ,
SALARY DECIMAL(9,2) ,
BONUS DECIMAL(9,2) ,
COMM DECIMAL(9,2) );
ALTER TABLE ITSO.EMPLOYEE
ADD PRIMARY KEY (EMPNO);
----------------- DDL Statement for foreign key relationship
ALTER TABLE EMPLOYEE ADD FOREIGN KEY (WORKDEPT)
REFERENCES DEPARTMENT (DEPTNO);
COMMIT;

30
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
To create the tables,start the SQL Plus 8.0 tool from the Start menu.
Connect as user ITSO with password itso.You will use the itsowdb.ddl file
from the Oracle directory.Fromthe SQL> prompt,issue the command:
start c:\WasDbBk\dbase\Oracle\itsowdb.ddl
Use the correct path for the SQL file for your installation.Figure 16 displays
the result when you issue the start command to execute the SQL.
Figure 16.Oracle Table Creation Output
Now your tables are set up to import the actual data.To do that,use the
Oracle8 Navigator again.Expand the tree until you see the new tables in the
local databases.You should have at least one table named DEPARTMENT
and one named EMPLOYEE.Because the default installation of Oracle8
installs tables with the same names,make sure you import the data to the
correct tables.Verify this by inspecting that owner of the tables is ITSO.

Chapter 4.Relational Database Installation and Setup
31
Importing the Table Data into Oracle
Import the data by selecting Import From File (Figure 17).Then specify the
data file,which is either employee.csv for the employee table or
department.csv for the department table.Both files reside in the same
directory as the itsowdb.ddl file (
c:\WasDbBk\dbase\Oracle
).
Figure 17.Import Data For The Employee Table
The sample data files are almost identical to the files used for DB2.An
extract of the sample data is shown in Figure 18.
The format of the date columns is different,
01-JAN-1965
instead of
1965-01-01
.
To distinguish results of queries,we use employee numbers starting with the
number 1,that is,
100010
instead of
000010
.

32
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 18.Oracle Sample Data Extract
The database is now ready.You may verify this by selecting one of the tables
(right click) and open it.
Oracle JDBC Drivers
The Oracle JDBC drivers are in the
c:\orant\jdbc\lib\classes111.zip
file.This
file must be available to Java through the class path.
Department.csv
"A00","SPIFFY COMPUTER SERVICE DIV.","100010","A00", "SAN JOSE"
"B01","PLANNING","100020","A00", "SAN JOSE"
"C01","INFORMATION CENTER","100030","A00", "SAN JOSE"
"D01","DEVELOPMENT CENTER",,"A00", "SAN JOSE"
"D11","MANUFACTURING SYSTEMS","100060","D01", "SAN JOSE"
"D21","ADMINISTRATION SYSTEMS","100070","D01", "SAN JOSE"
"E01","SUPPORT SERVICES","100050","A00", "SAN JOSE"
"E11","OPERATIONS","100090","E01", "SAN JOSE"
"E21","SOFTWARE SUPPORT","100100","E01", "SAN JOSE"
Employee.csv
"100010","CHRISTINE","I","HAAS","A00","3978","01-JAN-1965","PRES",18,"F","24-AUG-1933
", 0052750.00, 0001000.00, 0004220.00
"100020","MICHAEL","L","THOMPSON","B01","3476","10-OCT-1973","MANAGER",18,"M","02-FEB
-1948", 0041250.00, 0000800.00, 0003300.00
"100030","SALLY","A","KWAN","C01","4738","05-APR-1975","MANAGER",20,"F","11-MAY-1941"
, 0038250.00, 0000800.00, 0003060.00
"100050","JOHN","B","GEYER","E01","6789","17-AUG-1949","MANAGER",16,"M","15-SEP-1925"
, 0040175.00, 0000800.00, 0003214.00
"100060","IRVING","F","STERN","D11","6423","14-SEP-1973","MANAGER",16,"M","07-JUL-194
5", 0032250.00, 0000500.00, 0002580.00
"100070","EVA","D","PULASKI","D21","7831","30-SEP-1980","MANAGER",16,"F","26-MAY-1953
", 0036170.00, 0000700.00, 0002893.00
"100090","EILEEN","W","HENDERSON","E11","5498","15-AUG-1970","MANAGER",16,"F","15-MAY
-1941", 0029750.00, 0000600.00, 0002380.00
"100100","THEODORE","Q","SPENSER","E21","0972","19-JUN-1980","MANAGER",14,"M","18-DEC
-1956", 0026150.00, 0000500.00, 0002092.00
"100110","VINCENZO","G","LUCCHESSI","A00","3490","16-MAY-1958","SALESREP",19,"M","05-
NOV-1929", 0046500.00, 0000900.00, 0003720.00
"100120","SEAN"," ","O'CONNELL","A00","2167","05-DEC-1963","CLERK",14,"M",
"18-OCT-1942", 0029250.00, 0000600.00, 0002340.00
"100130","DOLORES","M","QUINTANA","C01","4578","28-JUL-1971","ANALYST",16,"F","15-SEP
-1925", 0023800.00, 0000500.00, 0001904.00
"100140","HEATHER","A","NICHOLLS","C01","1793","15-DEC-1976","ANALYST",18,"F","19-JAN
-1946", 0028420.00, 0000600.00, 0002274.00
"100150","BRUCE"," ","ADAMSON","D11","4510","12-FEB-1972","DESIGNER",16,"M",
"17-MAY-1947", 0025280.00, 0000500.00, 0002022.00

Chapter 4.Relational Database Installation and Setup
33
SQL Server Installation
We installed SQL Server Version 7.0 Desktop.Before you proceed with the
installation you should go to the Installation Pre-requisite section.Check and
make sure the Internet Explorer version you have is equal or greater than the
version mentioned in the pre-requisite.If you have an older version,proceed
with the prerequisite installation and then go and install the SQL server.
SQL Server Database Setup
There are a fewsteps you must followto enable access to SQL Server through
WebSphere.Because SQL Server does not provide a JDBC driver,we use the
Sun JDBC/ODBC bridge to get a connection to the database.
Create ITSOWDB Database
The first step is to create our database:

Open the SQL Server Enterprise Manager.

Expand ConsoleRoot -> Microsoft SQL Server -> SQL Server Group ->
[Computer-Name] -> Databases and select New Database fromthe context
menu (Figure 19).
Figure 19.Create a New Database

34
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

Now enter the database name ITSOWDB in the Name:entry field and
click OK.
Setup User ID as Owner
To create the database tables with the ITSO schema you have to add the
ITSO user ID into the list of Logins as follows:

FromEnterprise Manager expand ConsoleRoot -> Microsoft SQL Server ->
SQL Server Group -> [Computer-Name] and open Security.

Right mouse click on Logins and select New Login.

Input ITSO as the Name and select your Domain under Authentication.

Under Defaults for Database select ITSOWDB.

Select Server Roles tab and check System Administrators.

Select Database Access,check ITSOWDB and check public and db_owner
as database roles.Now click on the OK button.
SQL Server Table Creation
To create the tables for SQL Server we run the DDL commands in the
c:\WasDbBk\dbase\SQLServer\itsowdb.ddl
file.To run this file you have to run the
Query Analyzer.This is a separate SQL Server tool found from Windows
Start -> Programs -> SQL Server,or fromthe SQL Server Enterprise manager
select Tools -> SQL Query Analyzer.Open the file and run it (Figure 20).
Make sure to select the ITSOWDB database fromthe list of databases on the
top menu bar.

Chapter 4.Relational Database Installation and Setup
35
Figure 20.SQL Server Table Creation
Importing the Table Data into SQL Server
We now have to fill our tables with content.The easiest way to do this is to
use the import/export wizard.From the Microsoft SQL Server 7.0 program
group start Import and Export Data.Skip the entry panel with the
introduction text and proceed to the second panel.
DROP TABLE ITSO.EMPLOYEE;
DROP TABLE ITSO.DEPARTMENT;
----------------- DDL Statements for table ITSO.DEPARTMENT
CREATE TABLE ITSO.DEPARTMENT (
DEPTNO CHAR(3) NOT NULL ,
DEPTNAME VARCHAR(29) NOT NULL ,
MGRNO CHAR(6) ,
ADMRDEPT CHAR(3) NOT NULL ,
LOCATION CHAR(16) );
ALTER TABLE ITSO.DEPARTMENT
ADD PRIMARY KEY (DEPTNO);
----------------- DDL Statements for table ITSO.EMPLOYEE
CREATE TABLE ITSOWDB.ITSO.EMPLOYEE (
EMPNO CHAR(6) NOT NULL ,
FIRSTNME VARCHAR(12) NOT NULL ,
MIDINIT CHAR(1) NOT NULL ,
LASTNAME VARCHAR(15) NOT NULL ,
WORKDEPT CHAR(3) ,
PHONENO CHAR(4) ,
HIREDATE DATETIME ,
JOB CHAR(8) ,
EDLEVEL SMALLINT NOT NULL ,
SEX CHAR(1) ,
BIRTHDATE DATETIME ,
SALARY DECIMAL(9,2) ,
BONUS DECIMAL(9,2) ,
COMM DECIMAL(9,2) );
ALTER TABLE ITSO.EMPLOYEE
ADD PRIMARY KEY (EMPNO);
----------------- DDL Statement for foreign key relationship
ALTER TABLE ITSO.EMPLOYEE ADD FOREIGN KEY (WORKDEPT)
REFERENCES ITSO.DEPARTMENT (DEPTNO);

36
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

Make sure you select Text File as the source.Then specify the path
(
c:\WasDbBk\dbase\SQLServer
) where the csv files are located (Figure 21).
Figure 21.Import Data for Department Table

In the next panel you may specify the file format of the text file that is to
be imported.It should not be necessary to make any changes.

Then you may specify the column separator.Again the default should be
sufficient.

Figure 22 shows the next step.Be sure to select the correct database as
the import destination.

Chapter 4.Relational Database Installation and Setup
37
Figure 22.Choose a Destination

Now the destination table has to be specified.In this case we import the
department data into the department table as destination (Figure 23).
Figure 23.Select the Destination Table

In the next step you may specify additional options,for example,if this
import process should be stored.We skip this step and click Next.

The wizard shows its last panel,informing us that the import process is
now specified and ready to run.Click Finish.If everything runs fine,SQL
Server will informyou with a message box (Figure 24).

38
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 24.Importing Data Was Successful
The same import has to be performed for the employee table using the
employee.csv file.
The sample data files are identical to the files used for Oracle (Figure 18 on
page 32),with the exception that we used employee numbers starting with
the number 2,that is,
200010
instead of
100010
.
SQL Server JDBC Drivers
SQL Server does not provide JDBC drivers.We use the JDBC/ODBC bridge
drivers provided by Sun in the JDK.To use the bridge we have to set up an
ODBC data source.
ODBC Data Source Setup for JDBC
To enable the ITSOWDB database for ODBC or for the JDBC/ODBC bridge,
you have to set up an ODBC data source:

Go to the Control Panel and open ODBC Data Sources.

In the ODBC Data Source Administrator select the System DSN tab
(Figure 25).

Chapter 4.Relational Database Installation and Setup
39
Figure 25.ODBC Data Source Administrator

Select Add to add a new DSN (Figure 26).
Figure 26.Create New Data Source

40
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

Select SQLServer and click on Finish.

Enter the name,description,and server for the Data Source Name,then
click Next (Figure 27).
Figure 27.Create New Data Source to SQL Server

Now specify what kind of authorization you want the database to use and
then click on Next.

Next you select the ITSOWDB database for the ODBC connection.Click
on Next (Figure 28).

Chapter 4.Relational Database Installation and Setup
41
Figure 28.Create New Data Source to SQL Server (Database Setup)

On the ODBC Microsoft SQL Server Setup Panel click on Test Data Source
to confirmthat your selections were correct and that the connection can be
established.Then click on Finish.
You must make sure to set up a System DSN and not a User DSN.If you
set up a User DSN you will not be able to establish a connection to SQL
server through WebSphere,because it is using a default system ID to gain
access.
Note

42
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

© Copyright IBM Corp.1999
43
5 Java Database
Connectivity
Sun developed the Java Database Connectivity (JDBC) data access layer to
keep the Java API for accessing relational databases standard across all
vendors.Similar to Microsoft’s ODBC,this layer provides Java programmers
a generic way to communicate with relational databases regardless of the
database vendor.
Most vendors provide JDBC drivers for their products,which enables you to
access the vendor’s database using the JDBC API.For more detailed
information on JDBC drivers and categories of drivers,refer to the IBM
redbook Application Development with VisualAge for Java Enterprise,
SG24-5081.

44
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
DB2 UDB JDBC Drivers
IBMprovides two JDBC drivers for standard access to DB2.Each was
developed by utilizing previously written software,so each implements the
connection differently.The drivers are shipped with DB2 UDB and are
located in the file:
c:\SQLLIB\java\db2java.zip
When testing your JDBC applications,this file must be accessible in the Java
class path,or your application will not be able to find the JDBC drivers.
DB2 Application Driver
The DB2 application driver is in the COM.ibm.db2.jdbc.app.DB2Driver
class,located in the db2java.zip class collection file.This driver uses
the URL format of
jdbc:db2:<database name> (jdbc:db2:itsowdb)
Known as a category (or type) 2 JDBC driver,this driver takes advantage of a
previously written database client software.It contains a JDBC bridge that
closes the gap between the newJava coded driver and the legacy client access
software.The bridge takes JDBC commands and translates theminto the
traditional commands used in the client software.In DB2’s case,the DB2
Client Application Enabler (CAE) is used (Figure 29).The JDBC drivers
must have CAE code libraries available to access the server.
Figure 29.Category 2 JDBC Driver,Vendor Specific Bridge
When the JDBC bridge accesses CAE code,the client is accessing non-Java
code libraries.This driver is not used for communication between client and
CLIENT
SERVER
Application
JDBC Driver Manager
JDBC Vendor Specific
Data Access Bridge
Client Application
Enabler (CAE)
DBMS Server

Chapter 5.Java Database Connectivity
45
server across a network because of its use of platformdependent code.This is
because you cannot package the non-Java,platform specific code,into a JAR
file to send to a client when the client requests direct access to an
independent database server.
DB2 Network Driver
The network driver is in the COM.ibm.db2.jdbc.net.DB2Driver class,also
located in the db2java.zip class collection.It has a URL of the format:
jdbc:db2://<hostname>:<port>/<database> (jdbc:db2://sonoma:6789/itsowdb)
This driver is known as a category 3 JDBC driver,which splits the driver
code between the client and the server.The client contains the pure Java
code and the server contains the platformspecific code.When a database call
is issued,the client translates the call into a generic network protocol.The
server then translates the call back into a JDBC call and processes the call on
the server.Because the client is pure Java,this is a good implementation for
network use (Figure 30).
Figure 30.Category 3 JDBC Driver,Generic Network Protocol
These drivers require software on the server,also known as middleware,to
fulfill the database request and return the result to the client;for DB2 this is
the DB2 JDBC applet server.You must start this service on the server from
the Services menu in the Control Panel,it uses the default port number of
6789.You can also start it from a DOS command prompt by issuing the
command:
db2jstrt <port number> (db2jstrt 6789)
JDBC Driver Manager
JDBC Generic Network Protocol Driver
Middleware Middleware
DBMS (DB2) DBMS (other)
Application

46
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Oracle JDBC Drivers
Oracle provides two categories of JDBC,a thin client driver which is pure
Java and a second driver known as the Oracle Call Interface (OCI) driver.
The OCI driver is not pure Java and uses platform-specific native methods to
access the database.
Both drivers are implemented in the oracle.jdbc.driver.OracleDriver class.
Oracle Thin Client Driver
The thin client driver is a type 4 client driver which requires only sockets to
connect to an Oracle database.The driver is completely implemented in Java,
so it is platformneutral and does not require any Oracle software to be
installed on the client.
A type 4,according to the definition of JavaSoft,is a native-protocol fully
Java technology-enabled driver.
The URL to access a database through the thin client is:
jdbc:oracle:thin:@<servername>:<portnumber>:<databasename>
In our examples we use the default database instance orcl and the default
port number 1521,therefore the resulting URL is:
jdbc:oracle:thin:@localhost:1521:orcl
Oracle OCI Driver
The OCI driver is a type 2 driver.By definition a category 2,or type 2,driver
is a native-API partly Java technology-enabled driver (as shown in Figure 29
on page 44).It provides an implementation of the JDBC based on the native
OCI to access the database.
Since native methods are used to interact with the database,the OCI driver
is platform specific.Although this driver is not suitable for applets,the OCI
driver has the advantage of better performance.This makes it ideal for being
used by servlets on the server.As we tested,both drivers work with
WebSphere,provided that the Application Server class path includes the
Oracle drivers,and that for the OCI driver the oci804jdbc.dll file is in the
systempath (see “Oracle 8 Installation” on page 27).

Chapter 5.Java Database Connectivity
47
The URL to access a database using the OCI driver is somewhat longer:
jdbc:oracle:oci8:@(description=(address=(host=<servername>)
(protocol=tcp)(port=<portnumber>))(connect_data= (sid=<databasename>)))
In our examples we use the default database instance orcl and the default
port number 1521,therefore the resulting URL is:
jdbc:oracle:oci8:@(description=(address=(host=localhost)
(protocol=tcp)(port=1521))(connect_data=(sid=orcl)))
SQL Server JDBC Drivers
Microsoft does not provide a JDBC driver for SQL Server.JavaSoft does
package a JDBC/ODBC bridge in the sun.jdbc.odbc.JdbcOdbcDriver class.It
uses a URL format of
jdbc:odbc:<ODBC Data Source Name>
.Sun’s driver does have
a few shortcomings;it is a type 2 driver,which means it cannot be used in
applets.Also it can only connect to local ODBC Data Source Names.
An ODBC Data Source is a system defined way for a driver to connect to an
ODBC compliant data provider.To use ODBC drivers (or a JDBC/ODBC
bridge),you must define an ODBC Data Source Name (DSN) for the data
provider (for example,SQL Server) so that your driver knows how to retrieve
information from the data provider.In Windows,you can define an ODBC
Data Source Name (DSN) by using the ODBC Data Source Administrator,
located in the Windows Control Panel.It is possible to define a DSN on your
machine for a remote server,but access must be granted on the server for
your specific machine name.
Many vendors offer drivers which can connect directly to remote servers,but
most require some middleware to translate the calls fromJDBC to ODBC.A
company named i-net software (
www.inetsoftware.de
) does offers a category 4
driver for SQL Server.This driver has made a positive impression on the
Java community,but we have neither tested nor implemented this product.A
category 4 driver is a pure Java driver that connects directly to the server
and does not require any ODBC setup or use.Category 4 drivers are the most
preferred drivers.

48
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Configuring VisualAge for Java for JDBC
To use the databases fromwithin the development environment of VisualAge
for Java,the drivers have to be made visible in VisualAge for Java.There are
two ways to achieve this.
The first possibility is to include the zip files containing the JDBC drivers in
the class path of VisualAge for Java:

The DB2 zip file is
c:\sqllib\java\db2java.zip

The Oracle zip file is
c:\orant\Jdbc\lib\classes111.zip

The JDBC/ODBC bridge for SQL Server is part of the standard Sun Java
classes.
To add the zip files to the class path,open the Options dialog fromthe
Window menu in VisualAge for Java.Click on Resources on the left side and
edit the Workspace class path on the right side,as in Figure 31.
Figure 31.Editing VisualAge for Java’s Class Path
C:\sqllib\db2java.zip;c:\orant\Jdbc\lib\classes111.zip;

Chapter 5.Java Database Connectivity
49
The second possibility is to import the zip files into the repository and
Workbench of VisualAge for Java.This has the advantage that,if later on an
application or an applet is packed together with the required JDBC drivers,
VisualAge for Java will be able to include the drivers in the resulting JAR
files.
To import the zip file use the import feature.Select File -> Import and select
Jar-file as input source,browse for the jar file (either db2java.zip or
classes111.zip) and import it,for example under the new project Database
JDBC Drivers.
Configuring WebSphere for JDBC
To use the DB2 and Oracle JDBC drivers for servlets within WebSphere it is
necessary to include the driver classes in the WebSphere class path.Open a
Web browser with the WebSphere administration page (
http://<server-name or
ip-address>:9527
),go to Setup -> Java Engine,and add
c:\sqllib\java\db2java.zip (DB2)
c:\orant\Jdbc\lib\classes111.zip (Oracle)
to the Application Server Classpath Entry (Figure 32).
Figure 32.JDBC Drivers in WebSphere Class Path

50
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

© Copyright IBM Corp.1999
51
Part 3 Database
Programming
Examples
This part describes all of the code implementations of integrating Websphere,
VisualAge for Java,and database managers.These examples are simplified
to facilitate your understanding of the concepts.After reviewing these
examples you should have developed the basic knowledge necessary to
integrate Websphere Java servlets and database management systems in
more complex environments.
For all of the development and tests that are not automated by WebSphere
Studio,we used IBM’s VisualAge for Java.We recommend the use of this
development suite for its power,ease of use,and its tight integration with
WebSphere.This tight integration provides seamless portability of your code
between the two products.

52
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

© Copyright IBM Corp.1999
53
6 JDBC Programming
Examples
This chapter briefly describes,through examples,the basics of connecting
Java applications and servlets to databases using JDBC.These examples
should merely be used as building blocks and to understand the concepts.
Simple JDBC Application
We implemented a simple class to handle the common interaction with each
vendor’s databases.This class,shown in Figure 33,was implemented using
VisualAge for Java and created under the itso.wasdb.jdbc package.
The programlogic uses the following methods:

makeConnection:creates a connection to the database,prints a message
to the console saying the connection was successful,and then returns the
connection to the caller.

54
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 33.Simple Class for JDBC Access
package itso.wasdb.jdbc;
import java.sql.*;
public class SimpleSQL {
// Instance variable for the URL property
private String url = null;
// Instance variable for the userID property
private String userID = null;
// Instance variable for the password property
private String password = null;
// Instance variable for Connection
private Connection con = null;
public void makeConnection() {
try {
con = DriverManager.getConnection(url, userID, password);
System.out.println("Connected to Database: " + url+"\n");
} catch (SQLException se) {
System.out.print(se);
} catch (Exception e) {
System.out.println(e);
}
return;
}
public void setPassword(String aPassword) {
password = aPassword;
}
public void setURL(String aUrl) {
url = aUrl;
}
public void setUserID(String aUserID) {
userID = aUserID;
}
public void executeSQL() throws SQLException {
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("Select empno, lastname, deptname, mgrno
from itso.employee A, itso.department B where A.workdept = B.deptno");
System.out.println("EMPNO LASTNAME DEPT MGRNO\n");
while (rs.next()) {
String s1 = rs.getString("EMPNO");
String s2 = rs.getString("LASTNAME").concat(" ").substring(0,14);
String s3 = rs.getString("DEPTNAME").concat(" ").substring(0,29);
String s4 = rs.getString("MGRNO");
System.out.println(s1 + " " + s2 + " " + s3 + " " + s4);}
}
}

Chapter 6.JDBC Programming Examples
55

setXXX:methods to set the database connection parameters.

executeSQL:executes an SQL statement and displays the result set to the
console.This SQL statement performs an inner join of the two tables
based on the employee’s department.It displays the employee number,
last name,work department,and the department manager’s employee
number,as shown in the output (Figure 34).
Note that for this simplified example,we are not catching all possible
exceptions.
Figure 34.JDBC Example Output
Connected to Database: jdbc:db2:itsowdb
EMPNO LASTNAME DEPT MGRNO
000010 HAAS SPIFFY COMPUTER SERVICE DIV. 000010
000120 O'CONNELL SPIFFY COMPUTER SERVICE DIV. 000010
000110 LUCCHESSI SPIFFY COMPUTER SERVICE DIV. 000010
000020 THOMPSON PLANNING 000020
000030 KWAN INFORMATION CENTER 000030
000140 NICHOLLS INFORMATION CENTER 000030
000130 QUINTANA INFORMATION CENTER 000030
000060 STERN MANUFACTURING SYSTEMS 000060
000220 LUTZ MANUFACTURING SYSTEMS 000060
000210 JONES MANUFACTURING SYSTEMS 000060
000200 BROWN MANUFACTURING SYSTEMS 000060
000190 WALKER MANUFACTURING SYSTEMS 000060
000180 SCOUTTEN MANUFACTURING SYSTEMS 000060
000170 YOSHIMURA MANUFACTURING SYSTEMS 000060
000160 PIANKA MANUFACTURING SYSTEMS 000060
000150 ADAMSON MANUFACTURING SYSTEMS 000060
000070 PULASKI ADMINISTRATION SYSTEMS 000070
000270 PEREZ ADMINISTRATION SYSTEMS 000070
000260 JOHNSON ADMINISTRATION SYSTEMS 000070
000250 SMITH ADMINISTRATION SYSTEMS 000070
000240 MARINO ADMINISTRATION SYSTEMS 000070
000230 JEFFERSON ADMINISTRATION SYSTEMS 000070
000050 GEYER SUPPORT SERVICES 000050
000090 HENDERSON OPERATIONS 000090
000310 SETRIGHT OPERATIONS 000090
000300 SMITH OPERATIONS 000090
000290 PARKER OPERATIONS 000090
000280 SCHNEIDER OPERATIONS 000090
000100 SPENSER SOFTWARE SUPPORT 000100
000340 GOUNOT SOFTWARE SUPPORT 000100
000330 LEE SOFTWARE SUPPORT 000100
000320 MEHTA SOFTWARE SUPPORT 000100

56
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
DB2 Universal Database JDBC Code Example
We use both the application driver (com.ibm.db2.jdbc.app.DB2Driver) and
the network driver (com.ibm.db2.jdbc.net.DB2Driver) in Figure 35 to
illustrate the coding of each driver.
For the network driver,you must remember to start the JDBC applet server
on the server machine.This network driver uses a generic network protocol
to send its request to the server,then platform specific code on the server
translates the call to the database manager.See “DB2 Network Driver” on
page 45 for further details.
The port specified in the network driver’s URL must correspond to the port
defined when the middleware on the server (DB2 JDBC applet server) is
started.
Figure 35.DB2 UDB JDBC Code Example
package itso.wasdb.jdbc;
public class Db2SQL {
public static void main(java.lang.String[] args) {
SimpleSQL udbSQL = new SimpleSQL();
SimpleSQL udbSQLnet = new SimpleSQL();
String drv = "COM.ibm.db2.jdbc.app.DB2Driver";
String drvnet = "COM.ibm.db2.jdbc.net.DB2Driver";
try {
Class.forName(drv).newInstance();
udbSQL.setUserID("itso");
udbSQL.setPassword("itso");
udbSQL.setURL("jdbc:db2:itsowdb");
udbSQL.makeConnection();
udbSQL.executeSQL();
// You must start the JDBC Applet Server on the server machine with port 2222.
// Issue db2jstrt 2222 from command prompt
Class.forName(drvnet).newInstance();
udbSQLnet.setUserID("itso");
udbSQLnet.setPassword("itso");
udbSQLnet.setURL("jdbc:db2://SONOMA:2222/itsowdb");
udbSQLnet.makeConnection();
udbSQLnet.executeSQL();
} catch (Exception e) {
System.out.println(e);
}
}

Chapter 6.JDBC Programming Examples
57
Oracle JDBC Code Example
The Oracle example is straightforward and differs only in the usage of the
drivers.We used both Oracle JDBC drivers (Figure 36).
We assume a standard installation of Oracle with the default settings:

The Oracle server is located on the same machine where the example is
running.If Oracle is running on another machine,localhost must be
replaced by the name of the appropriate machine.

The database instance is the default instance which is created during
installation of Oracle,namely orcl.If other database instances are used,
this is to be replaced by the appropriate names.

The default port number 1521 is used for the Oracle server listener.This
can be changed in the Oracle Net8 Assistant tool.
Figure 36.Oracle JDBC Code Example
package itso.wasdb.jdbc;
public class OracleSQL {
public static void main(String args[]) {
SimpleSQL orclThinSql = new SimpleSQL();
SimpleSQL orclOciSql = new SimpleSQL();
String drv = "oracle.jdbc.driver.OracleDriver";
String urlThin = "jdbc:oracle:thin:@localhost:1521:orcl";
String urlOci = "jdbc:oracle:oci8:@(description=(address=(host=localhost)
(protocol=tcp)(port=1521))(connect_data=(sid=orcl)))";
try {
Class.forName(drv).newInstance();
// First try the thin driver
orclThinSql.setUserID("itso");
orclThinSql.setPassword("itso");
orclThinSql.setURL(urlThin);
orclThinSql.makeConnection();
orclThinSql.executeSQL();
// Now try the OCI driver
orclOciSql.setUserID("itso");
orclOciSql.setPassword("itso");
orclOciSql.setURL(urlOci);
orclOciSql.makeConnection();
orclOciSql.executeSQL();
} catch (Exception e) { System.out.println(e); }
}
}

58
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
SQL Server JDBC Code Example
We decided to use Sun’s JDBC/ODBC bridge driver for our example.It
provides limited functionality but is sufficient for our testing.When using a
JDBC/ODBC Bridge,it is important to create an ODBC Data Source Name
(DSN) for your data server.In Windows NT,go to the Control Panel and
select ODBC Data Sources.
Because we are using Sun’s driver,we must use a user DSN to access our
database.In the Data Sources panel,select the Add button to add a DSN for
SQL Server.A prompt for the data source will appear,and if SQL Server is
installed,it will be listed as a possible source.You must specify a name,
description,and a server.You can enter a remote server by entering the
remote server computer name.This is the only way to connect to a remote
server if you are using Sun’s driver.The next screen will prompt you on how
to check the authority of a login ID.For remote server authentication you
should select SQL Server for authorization as in Figure 37.You should also
add this ID to the SQL Server running on the remote computer.
Figure 37.ODBC DSN Authorization
The rest of the setup is self-explanatory;we chose the default settings for all
other panels.When you are done,you should have a new Data Source Name.
In Figure 38 we have already added a local and remote SQL Server.

Chapter 6.JDBC Programming Examples
59
Figure 38.ODBC Data Source Administrator
Figure 39 contains the class we used to connect to SQL Server.We instantiate
the driver and then set our parameters.Notice that our URL uses the
JDBC/ODBC bridge standard naming convention:
jdbc:odbc:<server name>:<Data Source Name>
Figure 39.SQL Server JDBC Example Code
package itso.wasdb.jdbc;
public class SqlSrvSQL {
public static void main(java.lang.String[] args) {
SimpleSQL sqlsrvsql = new SimpleSQL();
String drv = "sun.jdbc.odbc.JdbcOdbcDriver";
try {
Class.forName(drv).newInstance();
sqlsrvsql.setUserID("itso");
sqlsrvsql.setPassword("itso");
sqlsrvsql.setURL("jdbc:odbc:SQLSERVER");
sqlsrvsql.makeConnection();
sqlsrvsql.executeSQL();
} catch (Exception e) { System.out.println(e); }
}
}

60
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Running Applications with JDBC
The sample JDBC applications can be tested in VisualAge for Java,and they
can be run outside of VisualAge for Java in the operating system.
Testing the JDBC Applications in VisualAge for Java
To execute the sample applications in VisualAge for Java,make sure that the
JDBC drivers are in the class path.See “Configuring VisualAge for Java for
JDBC” on page 48 for detailed instructions.
Select the Java class of the application and click on the Run icon in the tool
bar.You can also select Run -> Run main from the context menu.The output
of the application is displayed in the VisualAge for Java Console window.
Note:The Oracle OCI driver does not work in VisualAge for Java.It seems
that the required DLL cannot be loaded.
Running the JDBC Applications in the Operating System
A Java JDK must be installed in the operating system.To run the sample
applications,export the classes fromVisualAge for Java into a directory that
is in the Java class path of the operating system.
We exported the classes into the
c:\WasDbBk\code
directory.This operation
creates subdirectories matching the package name of the classes,that is,
c:\WasDbBk\code\itso\wasdb\jdbc
.
Class Path for Java Applications
The class path must include the JDBC drivers and the exported code.We
changed the operating systemCLASSPATH environment variable from the
Control Panel -> System icon to include:
c:\sqllib\java\db2java.zip
c:\orant\jdbc\lib\classes111.zip
c:\WasDbBk\code
Run the JDBC Application
To execute the JDBC applications,open a command window and enter:
java itso.wasdb.jdbc.Db2SQL
java itso.wasdb.jdbc.OracleSQL
java itso.wasdb.jdbc.SqlSrvSQL

Chapter 6.JDBC Programming Examples
61
Simple JDBC Servlet
We implemented a simple JDBC servlet to gain experience with the
WebSphere Test Environment of VisualAge for Java and with deployment of
the code to the WebSphere Application Server.
Our servlet consists of two classes:

DbServletBean:a bean that contains all the JDBC code to list the
employees of the employee table.

DbServlet:a servlet that accepts a parameter to select the database
management systemand then calls the bean to execute the JDBC code.
The servlet (DbServlet) contains a doGet method that is called from the Web
server’s servlet engine (Figure 40).
The servlet sets up the bean with the JDBC driver,URL,user ID,and
password,before calling the execute method to run the JDBC code.A
parameter named database is passed to the servlet to select the database
management system(DB2,Oracle,or SQLServer).
The JDBC bean (DbServletBean) contains the following methods:

setURL:to set the value of the database URL

setUserID:to set the user ID for the connection

setPassword:to set the password for the connection

makeConnection:to connect to the database using the URL,user ID,and
password

executeSQL:to execute the select statement:
select empno, lastname, deptname, mgrno
from itso.employee A, itso.department B
where A.workdept = B.deptno

outputInfo:to create the HTML table with the result of the select
We do not show the code of the JDBC bean here because it is very similar to
the simple JDBC application.The only difference is that the output is routed
back to the Web server instead of being written to the console.

62
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 40.Simple Servlet doGet Method
public class DbServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res){
String drv = " ", url = " ";
String database = req.getParameterValues("database")[0];
if (database.equals("DB2")) {
drv = "COM.ibm.db2.jdbc.app.DB2Driver";
url = "jdbc:db2:itsowdb";
}
else if (database.equals("Oracle")) {
drv = "oracle.jdbc.driver.OracleDriver";
url = "jdbc:oracle:thin:@localhost:1521:orcl";
}
else if (database.equals("SQLServer")) {
drv = "sun.jdbc.odbc.JdbcOdbcDriver";
url = "jdbc:odbc:SQLSERVER";
}
PrintWriter out = null;
DbServletBean dbBean = new DbServletBean();
try {
out = res.getWriter();
out.println("<HTML><BODY>");
Class.forName(drv).newInstance();
dbBean.setUserID("itso");
dbBean.setPassword("itso");
dbBean.setURL(url);
out.println("<h1>--------- Attempting to Connect -------</h1><p>");
out.println("URL: "+ url + "<br>Driver: "+drv);
dbBean.makeConnection();
out.println("<h1>--------- Connection was successful -------</h1><p>");
dbBean.executeSQL(out);
}
catch(SQLException sqlExcept) {
out.println("<h1>------ EXCEPTION IN SAMPLE CODE ------</h1><p>");
out.println("Error:" + sqlExcept.getErrorCode());
out.println("State:" + sqlExcept.getSQLState());
sqlExcept.printStackTrace(out);
}
catch (Exception except) {
out.println("<h1>------ EXCEPTION IN SAMPLE CODE ------</h1><p>");
out.println(except);
except.printStackTrace(out);
}
out.println("<HTML><BODY>");
}
}

Chapter 6.JDBC Programming Examples
63
Testing the Servlet in VisualAge for Java
Start the WebSphere Test Environment (review “Running Servlets in
VisualAge for Java” on page 12).
To run the servlet you open a Netscape browser and enter the URL:
http://localhost:8080/servlet/itso.wasdb.jdbc.DbServlet?database=DB2
http://localhost:8080/servlet/itso.wasdb.jdbc.DbServlet?database=Oracle
http://localhost:8080/servlet/itso.wasdb.jdbc.DbServlet?database=SQLServer
Alternatively you can set up the servlet database parameter through the
servlet launcher properties dialog in VisualAge for Java (select the servlet
class and Tools -> Servlet Launcher -> Properties).Enter the parameter name
(database) and the value (DB2,Oracle,or SQLServer).Then launch the
servlet by selecting Tools -> Servlet Launcher -> Launch.This starts a Web
browser with the correct URL.
The output of the servlet is shown in Figure 41.
Running the Servlet in WebSphere
To deploy the servlet to WebSphere export the classes to the WebSphere
servlets directory (
c:\WebSphere\AppServer\servlets
).
Make sure that the JDBC drivers are in the WebSphere class path (review
“WebSphere Configuration for JDBC” on page 7).
Open a Web browser and enter the URL:
http://<hostname>/servlet/itso.wasdb.jdbc.DbServlet?database=DB2
http://<hostname>/servlet/itso.wasdb.jdbc.DbServlet?database=Oracle
http://<hostname>/servlet/itso.wasdb.jdbc.DbServlet?database=SQLServer
The output of the servlet is shown in Figure 41.

64
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 41.Simple Servlet Output in a Web Browser

© Copyright IBM Corp.1999
65
7 IBMConnection
Manager
The introductory text in this chapter is reprinted from the December 1998
issue of the IBMDeveloper Connection Technical Magazine.
The connection manager provides the means to manage pool(s) of connections
to one or more database(s).It classes are made of wrapper classes around the
classes over java.sql classes.Having these wrapper classes,JDBC
connections are passed to applications requesting connection.
The goal of the connection manager is to reduce the percentage of resources
used by the nonproductive connect/disconnect overhead,so that these
resources are available to handle more traffic.The connection manager also
provides a way to allocate relational database resources so that during peak
request volumes the more critical servlets have greater access to the
database resources than less critical servlets.
Performance and scalability are two of the most advantages of the connection
manager.A servlet needing a connection to a relational database will get an
existing connection from the pool,through the connection manager,rather
than having to create a new connection on its own.This is the key to the
connection manager's role:connections are kept open and are reused,so the

66
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
connect/disconnect overhead is spread over many,entirely separate user
requests coming in over the Internet.WebSphere makes the connection pool
management scalable by allowing the connection pool parameters to be
modified dynamically.Figure 42 shows the interaction between the
connection manager and a servlet.
Figure 42.Interaction Flow Between the Connection Manager and a Servlet
Servlets which are using the connection manager can run inside VisualAge
for Java using the WebSphere Test Environment or outside using WebSphere
itself.

Chapter 7.IBM Connection Manager
67
Configuration
The connection pools are configurable through the WebSphere Application
Server Administrator panel (Figure 43).For information on how to get to the
Administrator panel please refer to “WebSphere Configuration” on page 6.
Figure 43.Connection Management on WebSphere
Connection Pool
There is a set of pre-defined connection pools provided as default for database
servers such as:

IBMDB2

Oracle

Informix

Microsoft SQL Server

Sybase
You can customize your pool by adding a new one to the list or edit an
existing one (Figure 44).
It is also possible to add multiple entries differing in the pool name for
different databases of the same vendor.This is even more likely.For example,
one could add a pool entry specifying that the database(s) addressed by this

68
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
pool have longer lasting time out than other databases,addressed by other
pool entries,because the user interaction in a transaction is longer.
Figure 44.Connection Pool Configuration
These are the parameters that can be set for the connection pool:
Pool Type:The type of data server used by this pool of connections.JDBC
indicates a JDBC-compliant database.A data server is a product that helps
you manage and access data.Usually,it is a relational DBMS such as DB2,
Oracle,Informix,or Sybase.
Pool Name:The unique name for this pool of connections.Servlet
programmers need to knowthis name for their servlets to use this connection
pool.
MaximumConnections:Maximumnumber of connections that can be in
the pool.Consider setting it to the maximumnumber of users permitted by
your DBMS product license agreement.
MinimumConnections:The minimum number of connections that can
remain in the pool as a result of the reap process.The connection manager
periodically removes connections that become idle or orphaned.Use this
setting to keep from removing too many connections,erasing resource usage
performance gains.
Connection Time Out:The length of time (in milliseconds) the connection
manager will wait for a connection to become free when all connections in the
pool are currently in use and the number of connections has reached the
maximum (meaning no new connections can be created to fulfill the need).

Chapter 7.IBM Connection Manager
69

A value of 0 allows the connection manager to wait forever.

A value of -1 disables the wait (an exception is immediately thrown if a
connection is not available).

A value of 1000 to 2000 (1 to 2 seconds) is suggested.

Servlet programmers must know the value of the connection time out to
effectively set the waitRetry parameter in their servlets.
MaximumAge:The maximum number of seconds a connection can be idle
before the reap process releases the connection from the servlet that owns it.

A value of -1 disables this function;the reap process will not release any
connections from idle servlets.

A value of 900 to 1800 (15 to 30 minutes) is suggested.
MaximumIdle Time:The maximumnumber of seconds an unassigned
connection can remain in the pool.

A value of -1 disables this function;the reap process will not remove any
connection fromthe pool and disconnect it from the data server.

A value of 900 to 1800 (15 to 30 minutes) is suggested.
Reap Time:The interval (in seconds) at which the connection manager
performs the reap process.

A value of -1 disables this function;the reap will not be performed,
regardless of the values specified in MaximumAge or MaximumIdle Time
parameters.

A value of 1800 to 3600 (30 to 60 minutes) is suggested.
IBM Connection Manager and Pure JDBC
When using standard JDBC for database connection,you are responsible for
creating a new connecting to the database and then closing it.Once the
connection is closed,the next transaction will have to create a new
connection to access the database.
When using the IBMconnection manager you get a connection froma
connection pool.If a connection is available the connection manager will
return a IBMJdbcConnection from which you can obtain its underlying
JDBC connection.When you are done using the connection,you do not close
the connection;you merely release it.This way it can be used by the next
party requesting a connection to a database.The connection obtained
through connection manager should not be closed,that is,do not call
Connection.close.

70
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
IBM Connection Manager Examples
In our programming examples we have made connections through the
connection manager using servlets.A select query is run against the
employee table and the result is displayed on a page using HTML.The
connections have been made to three different databases:

IBMDB2

Oracle

SQL Server
Every database example has its own class.All of the classes related to the
connection manager example are part of the itso.wasdb.connmgr package.
The example for all three databases performs the same task.As a result,all
three classes do inherit their common function froma super class.The super
class name is DbaseConnMgr.
When describing the code,only the segments that are relevant to the
connection manager usage are described.The rest is left to the reader’s
ability to read the code and understand its purpose.
Using the IBM Connection Manager
The package in which the connection manager classes reside is
ibm.servlet.connmgr.
There are several steps you must follow to use the connection manager.

Create a connection specification using IBMJdbcConnSpec.

Get a reference to the connection manager object using the static method
IBMConnMgrUtil.getIBMConnMgr.

Get an IBMJdbcConn object,that is,a wrapper around a JDBC
connection,using the connection manager’s getIBMConnection method.

Get a java.sql.Connection Object using getJdbcConnection of the class
IBMJdbcConn.
Now you have a connection to the database for your transactions.
Once you are done with the transaction,you have to release the connection to
be returned into the pool.You do this by calling the releaseIBMConnection
method of the IBMJdbcConnection class.

Chapter 7.IBM Connection Manager
71
HTML File for Accessing the Servlets
A simple HTML file is used to provide access to launch the servlets through
WebSphere (Figure 45).You have to use this file only when you are trying to
launch the servlets outside of VisualAge for Java.
Figure 45.HTML for the Connection Manager Servlet Examples
<!DOCTYPE HTML PUBLIC "-//SQ//DTD HTML 2.0 + all extensions//EN" "hmpro3.dtd">
<HTML>
<HEAD> <TITLE> JDBCServlet </TITLE> </HEAD>
<BODY BGCOLOR="#FFFFFF">
<p>
<table width=580 border=0 cellpadding=20>
<tr>
<td valign=top>
<H2><center>IBM Connection Manager</center></H2>
<P>The following provides an access to three different databases using
IBM Connection Manager .
</td>
</tr>
<tr>
<td valign=bottom>
What would you like to do?
<br><br>&nbsp;&nbsp;
<A HREF="/servlet/itso.wasdb.connmgr.UdbConnMgr" TARGET="_blank">Run IBM DB2 UDB
Sample</A>
<br>&nbsp;&nbsp;
<A HREF="/servlet/itso.wasdb.connmgr.SqlSrvConnMgr" TARGET="_blank">Run MS SqlServer
Sample</A>
<br>&nbsp;&nbsp;
<A HREF="/servlet/itso.wasdb.connmgr.OrclConnMgr" TARGET="_blank">Run Oracle
Sample</A>
<br>&nbsp;&nbsp;
</td>
</tr></table>
</BODY>
</HTML>
Keep in mind that the connections provided through the connection
manager are subject to time-outs,depending on the maximum age
parameter.If the difference in the time when you have obtained the
connection and the time when you are going to use it is rather long,
compare it to maximumage.It is recommended to use
verifyIBMConnection prior to using the connection.If the servlet still owns
the connection,the method returns true.Otherwise false will be returned
and you have to obtain a new connection object.
Notice

72
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Running the Example
There are two ways in which you can run this sample:
1.Using the WebSphere Test Environment in VisualAge for Java
Select the servlet class,for example itso.wasdb.connmgr.UdbConnMgr,
and launch it fromthe context menu using Tools -> Servlet Launcher ->
Launch (Figure 46).
Figure 46.Launching a Servlet
2.Using WebSphere and the HTTP Server
Place the ConnMgrTest.html and the UdbConnMgr.class file under the
proper directories.The HTML file has to be placed in the path where the
Web server can find it.Using the default installation the HTML file
should be placed under
c:\Program Files\IBM HTTP Server\htdocs
.
The class file can easily exported with VisualAge for Java.Fromthe
context menu of the servlet class select Export....In the following dialog
select Directory for the export destination,click Next and specify the
export path.If you use the default installation of WebSphere this is
c:\WebSphere\AppServer\servlets
.Make sure you select at least the class file
to be exported (Figure 47).

Chapter 7.IBM Connection Manager
73
For more information about the proper directory location refer to
“Configuration” on page 4 and “WebSphere Configuration” on page 6.
Figure 47.Exporting a Servlet
Next go to
http://<server-name>/ConnMgrTest.html
fromthe HTML page and
select the database sample you wish to run (you have to export all servlet
examples first).
DbaseConnMgr Class
This class takes care of all function directly involved with the connection
manager.The following is the class declaration for the DbaseConnMgr class
(Figure 48).We then used subclasses for the three different DBMSs.
Figure 48.DbaseConnMgr Class Declaration
The init method initializez the servlet when it is first loaded (Figure 49).
package itso.wasdb.connmgr
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.ibm.servlet.connmgr.*;
public abstract class DbaseConnMgr extends HttpServlet {
private static final String SELECT = "Select empno, lastname, deptname, mgrno
from itso.employee A,itso.department B where A.workdept = B.deptno
ORDER BY A.EMPNO ,B.DEPTNO";
protected IBMJdbcConnSpec connSpec = null;
protected IBMConnMgr connMgr = null;
private IBMJdbcConn ibmConn = null; //Connection used for Select statement
private static final String USER = "itso";
private static final String PASSWORD = "itso";
}

74
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 49.DbaseConnMgr init Method
The initConnection method creates a connection manager specification and
obtains a static reference to the connection manager object (Figure 50).
Figure 50.DbaseConnMgr initConnection Method
The getConnection method gets an IBMJdbcConnection object fromthe
connection pool and returns a java.sql.Connection.This connection is used for
the actual SQL statement (Figure 51).
Figure 51.DbaseConnMgr getConnection Method
The releaseConnection method releases the IBMJdbcConnection object into
the connection pool (Figure 52).
public void init(ServletConfig config) throws ServletException {
super.init(config);
initConnection(getPoolName(), getDriver(), getUrl());
}
private void initConnection(String poolName,
String driverName,
String url ){
connSpec = new IBMJdbcConnSpec(poolName ,
true ,
driverName,
url ,
USER ,
PASSWORD );
connMgr = IBMConnMgrUtil.getIBMConnMgr();
}
public Connection getConnection() throws SQLException, IBMConnMgrException {
Connection conn = null;
ibmConn = (IBMJdbcConn)connMgr.getIBMConnection(connSpec);
conn = ibmConn.getJdbcConnection();
return conn;
}

Chapter 7.IBM Connection Manager
75
Figure 52.DbaseConnMgr releaseConnection Method
The executeSelect method creates an SQL statement using the connection
obtained from the connection manager and executes it.The result of the
execution is passed on to an output formatting routine (Figure 53).
Figure 53.DBaseConnMgr executeSelect Method
The outputInfo method reads the ResultSet produced by the executeSelect
method and places it inside a table on the HTML page (Figure 54).
Figure 54.DBaseConnMgr outputInfo Method
The doGet method is called from the Web server when the servlet is started
from the HTML page (Figure 55).
public void releaseSelectConnection() throws IBMConnMgrException{
ibmConn.releaseIBMConnection();
}
public void executeSelect(PrintWriter out) throws SQLException {
Statement stmt= selConn.createStatement();
ResultSet rs = stmt.executeQuery(DB2_SELECT);
outputInfo(rs , out);
}
public void outputInfo(ResultSet rs , PrintWriter out) throws SQLException{
out.println("<TABLE BORDER=3 FRAME=ALL CELLPADDING=1 CELLSPACING=1 COLS=4>");
out.println("<CAPTION ALIGN=TOP><b>UDB - Employee Info</b></CAPTION>");
out.println("<TR>");
out.println("<TH ALIGN=CENTER WIDTH=20%>Emp Number ");
out.println("<TH ALIGN=CENTER WIDTH=20%>Last Name ");
out.println("<TH ALIGN=CENTER WIDTH=20%>Dept Name ");
out.println("<TH ALIGN=CENTER WIDTH=20%>Mgr Number ");
while(rs.next()) {
out.print("<TR><TD ALIGN=CENTER>" + rs.getString("EMPNO"));
out.print("<TD ALIGN=CENTER>" + rs.getString("LASTNAME"));
out.print("<TD ALIGN=CENTER>" + rs.getString("DEPTNAME"));
out.print("<TD ALIGN=CENTER>" + rs.getString("MGRNO"));
}
out.println("</TABLE>");
}

76
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 55.doGet Method
public void doGet(HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
try {
out.println("<HTML><HEAD><TITLE>ConnMgr Test</TITLE>");
out.println("</HEAD><BODY>");
executeSelect(out);
releaseConnection();
} catch (SQLException sqlExcept) {
out.println("<h1>---------- EXCEPTION IN SAMPLE CODE ----------</h1> <p>");
out.println("Error:" + sqlExcept.getErrorCode());
out.println("State:" + sqlExcept.getSQLState());
sqlExcept.printStackTrace(out);
out.println("<HTML><BODY>");
} catch (IBMConnMgrException mgrExcept) {
if (out != null) {
out.println("<h1>--------- EXCEPTION IN SAMPLE CODE ----------</h1> <p>");
out.println("<b>" + mgrExcept.getReason() + "</b>");
out.println(mgrExcept);
mgrExcept.printStackTrace(out);
} else {
System.out.println("---------- EXCEPTION IN SAMPLE CODE ----------");
System.out.println(mgrExcept);
mgrExcept.printStackTrace();
}
out.println("</BODY></HTML>");
}
}
The code for the three databases mentioned below is the same,with the
exception of the constants used to describe their database,connection pool,
and driver name.To avoid repetition we will review all the methods for
DB2 and will go through specifics of the other two databases.
Note

Chapter 7.IBM Connection Manager
77
DB2
Before you start make sure your database is set up properly,see “DB2 UDB
Installation” on page 22 for more information.
The package in which the connection manager classes reside is
ibm.servlet.connmgr.The following is the class declaration for the
UdbConnMgr class.It is here where the constants for the specific database
and connection pool are specified (Figure 56).
Figure 56.UdbConnMgr Class Declaration
The getDriver method returns the JDBC driver name (Figure 57).
Figure 57.UdbConnMgr getDriver Method
The getPoolName method returns the pool name (Figure 58).
Figure 58.UdbConnMgr getPoolName Method
package itso.wasdb.connmgr
/**
* This type was created in VisualAge.
*/
public class UdbConnMgr extends DbaseConnMgr{
private static final String DB2_POOLNAME="JdbcDb2";
private static final String DB2_URL = "jdbc:db2:itsowdb";
private static final String DB2_DRIVERNAME="COM.ibm.db2.jdbc.app.DB2Driver";
}
public String getDriver() {
return DB2_DRIVERNAME;
}
public String getPoolName() {
return DB2_POOLNAME;
}

78
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
The getUrl method returns the URL (Figure 59).
Figure 59.UdbConnMgr getUrl Method
Sample Output
The result of the run of this servlet is shown in Figure 60.
Figure 60.Connection Manager Output
public String getUrl() {
return DB2_URL;
}

Chapter 7.IBM Connection Manager
79
Oracle
Before you start make sure your database is set up properly (see “Oracle 8
Installation” on page 27).
The following is the class declaration for the OracleConnMgr class.Here is
where the constants for the specific database and connection pool are
specified (Figure 61).
Figure 61.OracleConnMgr Class Declaration
The rest of the code is the same as that described in “DB2” on page 77.The
only difference is that,inside the code,the DB2_ constants are replaced with
the ORACLE_ set of constants.
Microsoft SQL Server
Before you start,make sure your database is set up properly (see “SQL
Server Installation” on page 33).
The following is the class declaration for the SqlSrvConnMgr class.Here is
where the constants for the specific database and connection pool are
specified (Figure 62).
Figure 62.SqlSrvConnMgr Class Declaration
The rest of the code is the same as that described in “DB2” on page 77.The
only difference is that,inside the code,the DB2_ constants are replaced with
SQLSRV_ set of constants.
/**
* This type was created in VisualAge.
*/
public class OracleConnMgr extends DbaseConnMgr {
private static final String ORACLE_POOLNAME = "JdbcOracle";
private static final String ORACLE_URL = "jdbc:oracle:thin:@localhost:1521:orcl";
private static final String ORACLE_DRIVERNAME = "oracle.jdbc.driver.OracleDriver";
}
/**
* This type was created in VisualAge.
*/
public class SqlSrvConnMgr extends DbaseConnMgr {
private static final String SQLSRV_POOLNAME = "JdbcSqlserver";
private static final String SQLSRV_URL = "jdbc:odbc:SqlServer";
private static final String SQLSRV_DRIVERNAME = "sun.jdbc.odbc.JdbcOdbcDriver";
}

80
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

© Copyright IBM Corp.1999
81
8 Data Access Beans:
VisualAge for Java
In this chapter we describe the IBMdata access beans and their usage
through WebSphere.For more detailed reading,refer to VisualAge for Java
Enterprise Version 2:Data Access Beans - Servlets - CICS Connector,
SG24-5265.
Data access beans provide access to relational data.Using data access beans
is the fastest,non-programming way of building SQL queries accessing
existing databases.Just open the Visual Composition Editor,place a Select
bean on the free-formsurface,specify the database connection and data you
require,and you are ready.
Most functionality is predefined.For example,your application can add,
update,and delete rows,commit or rollback database transactions,handle
multiple connections,lock rows,make the access read only,and specify how
many rows are fetched into memory (cache).
An SQL Assist SmartGuide helps you to visually specify the data you need.
You can select one or more tables,join tables,define search conditions,
restrict the number of columns,sort the result set,and change the mapping
between the SQL types in the database and the Java types in the application.

82
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
In addition,an SQL Editor lets you enter SQL statements manually.Use this
method when you need to compose very special or very sophisticated
database queries.
1
After you have defined your database access using a Select bean,you can
place a DBNavigator bean into your visual application.The DBNavigator
bean incorporates buttons that navigate the result set of a query and perform
various relational database operations.
The Select bean fits into the JTable model of the new Java Foundation
Classes (JFC,called Swing).This relationship between retrieved SQL data
and the Swing table model makes it very easy to develop attractive user
interfaces for Java applets and applications,based on standard Java classes.
With data access beans you have direct access to any database for which a
JDK 1.1 compliant JDBC driver exists,for example,DB2 Universal Database
(UDB) Version 5.
2
Alternatively,you can use an ODBC driver together with
the JDBC ODBC bridge that comes with the Sun JDK.
Loading the Data Access Bean Feature
Before you can use data access beans you have to add the data access bean
feature to the Workbench.Use the Quick Start menu (F2),select Features ->
Add feature,and select IBMData Access Beans 1.0 in the dialog that is
displayed.This action loads the project into the Workbench and adds the
data access beans to the beans palette of the Visual Composition Editor.
Data Access Beans and the Standard SQL Classes
When using java.sql.* set of classes,you are required to write all of the SQL
code which sometimes can be a headache.Data access beans require less use
of SQL and more Java.The following shows the similarities between
java.sql.* classes and data access beans (com.ibm.db.*) classes (Figure 63).
1
SQL Editor generated code,in contrast to SQL Assistant generated code,cannot handle SQL parameters (host
variables) because the statement is not parsed.The developer or the application code can add the parameter
definitions manually to the generated methods to enable passing of values into the host variables.
2
You can also use DB2 V2.12,plus the latest CSD.

Chapter 8.Data Access Beans:VisualAge for Java
83
Figure 63.Data Access Beans Architecture
DatabaseConnection and java.sql.Connection
DatabaseConnection is a replacement for java.sql.Connection.It provides the
same methods as its java.sql.Connection counter part.The connection can be
managed internally or externally depending on which constructor is used.
Internal Connection
The DatabaseConnection default constructor provides a wrapping object
around the JDBC connection for internal connection management.The
internal management is rather simple.The connection that is created is
shared by any application that makes a request to create a connection of the
same name;it can connect and disconnect fromthe database at any time.
With every request a counter gets incremented internally,and as objects
disconnect,the counter is decremented.The object is released once the
counter is dropped back to 0 (Figure 64).This way multiple applications
share the same connection.Having this internal connection management is
ideal for query based applications.
If you are using an internal connection for update,you should be aware of all
the applications with access to the connection,because any application with
the connection can execute a commit or a rollback at any time.If you have
several beans with access to same connection you have to ensure the
sequence of access to the connection to prevent any loss or duplication of
data.
Data Access Beans Architecture
Connection
Statement
PreparedStatement
DatabaseConnection
ResultSet
SelectResult
SelectStatement
com.ibm.db.*java.sql.*

84
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 64.Internal Connection Management by DatabaseConnection
External Connection
When using the DatabaseConnection (java.sql.Connection) constructor,the
connection is considered to be externally managed.You can get this
connection object from some other externally managed connection pool,such
as IBM’s connection manager.You cannot issue the connect or disconnect
methods against an externally managed connection (Figure 65).
Figure 65.External Connection Management by IBMConnectionManager
DatabaseConnection
java.sql.Connection
conn
Application
Application
Application
BA C
Once all three applications
disconnect() the object conn
is closed.
DatabaseConnection
java.sql.Connection
connA
DatabaseConnection
java.sql.Connection
connB
DatabaseConnection
java.sql.Connection
connC
JdbcDb2 JdbcOracle
ConnectionManager
DB2
Oracle

Chapter 8.Data Access Beans:VisualAge for Java
85
Statement Types
The class com.ibm.db.SelectStatement represents an SQL select statement,
which is roughly equivalent to both java.sql.Statement and
java.sql.PreparedStatement classes.The SelectStatement always works with
a helper object;an instance of com.ibm.db.StatementMetaData.
Figure 66 shows the way in which these objects are constructed.A
SelectStatement holds a StatementMetaData.A StatementMetaData is
composed of SQL query text and column information.Note that multiple
SelectStatements can share the same StatementMetaData,the information is
read only from that perspective.
Figure 66.Select Statement Design
ResultSet Types
There are certain drawbacks in using JDBC directly;the java.sql.ResultSet
can only scroll forward,not backward.The com.ibm.db.SelectResult class of
the data access beans is traversable both backward and forwards,and it also
allows jumping to a particular index.The SelectResult is already positioned
at the first row.
The SelectResult reads the content of ResultSet into its structure.The
SelectResult has the performance hit the first time it does that.The
advantage of the SelectResult becomes apparent when there is a need to
scroll up and down the ResultSet several times.
SelectStatement
StatementMetaData
SQL Query Text
Column Typing Information

86
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Building a Servlet using Data Access Beans
We chose to use the Servlet Builder in VisualAge for Java to create our
samples.There are three samples in which each of them accesses a different
DBMS:DB2,Oracle and SQL Server.
When describing the examples we avoided one-by-one steps involved in
creating the servlet because there is a book dedicated to that topic.Please
refer to VisualAge for Java Enterprise Version 2:Data Access Beans - Servlets
- CICS Connector,SG24-5265,for details about visual servlet construction.
Sample Data Access Beans
In this sample the servlet takes a department number,and using a Select
bean,it executes a query against the employee table and returns the list of
employees into a list box.It also displays the number of employees within the
department.For detailed information on the database tables structure,
please refer to “Relational Database Installation and Setup” on page 21.
Every database systemhas its own servlet class and Select bean.The servlet
contains basic information on the parameters of the servlet,such as the
department number entry field.The Select bean contains all of the
information on the type of database,DB2,Oracle,or SQL Server,the tables
involved,and the type of the query.No line of code is written to create this
servlet.All of the code is generated by VisualAge for Java.All of the
information on the tables and the beans is stored through a sequence of
panels and dialogs,as part of the visual composition.
Creating a Servlet Visually using Data Access Beans
To create a servlet visually using data access beans,we create the visual
servlet,tailor a Select bean for database access,and then construct the logic
through connections in the visual composition.
Visual Servlet
To create the visual servlet follow these steps:

Create the new servlet class UdbDabServlet in the itso.wasdb.accessbean
package using Quick Start from the menu (F2).Use the servlet palette in
the visual composition editor to design the HTML output page of the
servlet.In our example we use the same HTML page for input and output
(Figure 67).

Chapter 8.Data Access Beans:VisualAge for Java
87
Figure 67.Servlet User Interface

Notice the naming of these beans:
• The HtmlEntryField is named deptnum
• The HtmlText...deptnum...is named deptresult
• The HtmlText...number...is named numemp
• The Retrieve push button is named retrieveButton

Save the servlet.This generates the underlying Java code.In particular
this creates a JavaBean (UdbDabServletFormData) that holds all the
data of the data entry form,that is,the department number (deptnum)
and the Retrieve push button.

Fromthe Servlet palette (Figure 68) select the FormData bean and choose
the UdbDabServletFormData as the class name in the dialog.This class
was generated in the step before.Place it somewhere outside the user
interface of the servlet.This bean represents the actual data input which
can be done in the user interface.In our example,this bean contains as a
property named deptnumthe department value entered in the
HtmlEntryField.
HtmlText
HtmlForm
HtmlButton
HtmlTable
HtmlResultTable
HtmlForm
HtmlEntryField
(deptnum)

88
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 68.The Servlet Palette

From the Database palette drag and drop a Select bean and name it
UdbEmpInDept (Figure 69).
Figure 69.Servlet with Data Access Bean and Select Bean
Select Bean
To tailor the database access you open the Select bean to specify the database
connection and SQL statement.

Double click on the UdbEmpInDept bean.Select the query field and click
on the grey box.Now you can set up a database connection and an SQL
statement (Figure 70).

Chapter 8.Data Access Beans:VisualAge for Java
89
Figure 70.UdbEmpInDept Connection Setup

Click on the New button to create a new database access class
(UdbDabAccess) that will hold the connection information and the SQL
statement to be executed.

Click on the Add button to add a connection to a database (Figure 71).
Specify the URL,the driver,the user ID,and the password.The name of
the connection (connect) then becomes a method name in the database
access class.
By defining multiple connections it is possible to access different
databases,or the same database with different user IDs (and different
rights) just by choosing another connection method.

90
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 71.Connection Alias Setup

Test the connection (the DBMS must be up and running) and only proceed
if you get a successful answer.

Click OK to generate the connect method into the database access class.

Select the SQL tab to set up an SQL query (Figure 72).
Figure 72.UdbEmpInDept SQL Setup

Chapter 8.Data Access Beans:VisualAge for Java
91

Click on the Add button and input the SQL Name in the SQL
Specification panel (Figure 73).This name becomes the name of the
method that is generated into the database access class.
Figure 73.Creating New SQL Specification
In most cases,you will select the Use SQL Assist SmartGuide radio
button.The SQL Assist SmartGuide enables you to build an SQL
statement without much knowledge of SQL.You go through a set of pages
to specify the tables,columns,and conditions of your SQL access.

On the Tables page select the table(s) (Figure 74).
Figure 74.Selecting a Table for the Query

92
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

On the Condition 1 page you can create a WHERE clause with column
specifications.To pass a variable as a value enter the name of a host
variable (:DEPTNUM) that will be set at execution time (Figure 75).You
will recognize that if you enter a host variable beginning with a colon the
name is printed in uppercase bold letters.
Figure 75.Setting Query Condition(s)

On the Columns page select the columns to be retrieved (Figure 76).

Chapter 8.Data Access Beans:VisualAge for Java
93
Figure 76.Selecting Columns to Include for a Query

On the Sort page optionally select the columns used to sort the resulting
rows.This creates an ORDER BY clause in the SQL statement.

On the Mapping page you can performsimple conversions of SQL data
types for your Java program,for example,converting numeric data to
appropriate Java data types.

The SQL page displays the resulting query statement fromthe choices
made in previous pages (Figure 77).

94
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 77.SQL Query Statement

You can test the SQL statement by clicking on the Run SQL button.You
are prompted for the host variable value,the SQL statement is executed,
and the results are displayed in a table.

Click on Finish to generate the code.The SQL statement is added to the
query (Figure 78).

Chapter 8.Data Access Beans:VisualAge for Java
95
Figure 78.Resulting SQL Query
Up to this point,the setup for the servlet and the Select bean have been
described.
DBMS Selection
This example was developed using the DB2 DBMS.To develop a servlet that
uses another database,the same steps apply,except for the connection
parameters.A different connection alias,appropriate for that database
system,has to be provided (Figure 71 on page 90).
We developed the same sample also for Oracle (OrclDabServlet) and SQL
Server (SqlSrvDabServlet).
Logic Connections
Figure 79 shows the final servlet after all the proper connections have been
established between the objects and the user interface.
Follow these steps to create the connections:
1.Connect the retrieveButtonPressed event of the UdbDabServletFormData
bean to the execute method of the UdbEmpInData bean.

96
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
2.Connect the deptnumString property of UdbDabServletFormData1 to the
Parm_DEPTNUM_String property of UdbEmpInDept.
3.Connect the deptnumString property of UdbDabServletFormData1 to the
string property of the...deptnum...HtmlText bean.
4.Connect the this property of the UdbEmpInDept Select bean to the
tableModel property of the HtmlResultTable.
5.Connect the executed event of the UdbEmpInDept to the string property of
the...number...HtmlText.The result line is still dashed,meaning that a
parameter is missing.
6.Connect the value parameter of the previous connection to the numRows
property of UdbEmpInDept.
Figure 79.UDB Servlet Using Data Access Beans:Visual Composition
At this point you can generate the code and run it.
Tailoring the Output Result Table
By default the HTMLResultTable bean displays the long columns names,
which,depending on the database,consist of the schema name,the table
name,and the column name.
We modified the empindept method of the UdbDabAccess class to specify
short column names (Figure 80).
1
2
3
4
5
6

Chapter 8.Data Access Beans:VisualAge for Java
97
Figure 80.Tailored Method with Short Columns Names
Running the Servlet
Figure 81 shows the result of the servlet in a Web browser.Start the
WebSphere Test Environment (SERunner class),then enter the URL:
http://127.0.0.1:8080/servlet/itso.wasdb.accessbean.UdbDabServlet
Figure 81.Output Result of the Servlet in a Web Browser
Before:

aSpec.addColumn("EMPLOYEE.EMPNO", 1,1);
aSpec.addColumn("EMPLOYEE.FIRSTNME", 12,12);
aSpec.addColumn("EMPLOYEE.LASTNAME", 12,12);
aSpec.addColumn("EMPLOYEE.JOB", 1,1);
aSpec.addColumn("EMPLOYEE.SEX", 1,1);
aSpec.addColumn("EMPLOYEE.BIRTHDATE", 91,91);
After:

aSpec.addColumn("EMPNO", 1,1);
aSpec.addColumn("FIRSTNME", 12,12);
aSpec.addColumn("LASTNAME", 12,12);
aSpec.addColumn("JOB", 1,1);
aSpec.addColumn("SEX", 1,1);
aSpec.addColumn("BIRTHDATE", 91,91);

98
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Using the Connection Manager and Data Access Beans
In this section we develop an example servlet that uses both the connection
manager to establish and manage the connections to the database system
and a non-visual data access bean.
The servlet function is to search for a record in the employee table,and
display and update the record.
Class Definition
The first step is the class definition in a new itso.wasdb.connaccess package
(Figure 82).We coded the database access for all three DBMSs,however,two
are in comments.
Figure 82.EmployeeServlet Class Declaration
package itso.wasdb.connaccess;
import com.ibm.servlet.connmgr.*;
import com.ibm.db.*;
import java.sql.*;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
public class EmployeeServlet extends javax.servlet.http.HttpServlet {
com.ibm.servlet.connmgr.IBMConnSpec connSpec = null;
static com.ibm.servlet.connmgr.IBMConnMgr connMgr = null;
final static String POOLNAME = "JdbcDb2";
final static String URL = "jdbc:db2:itsowdb";
final static String DRIVERNAME = "COM.ibm.db2.jdbc.app.DB2Driver";
/*
final static String POOLNAME = "JdbcOracle";
final static String URL = "jdbc:oracle:thin:@localhost:1521:orcl";
final static String DRIVERNAME = "oracle.jdbc.driver.OracleDriver";
final static String POOLNAME = "JdbcSqlserver";
final static String URL = "jdbc:odbc:SqlServer";
final static String DRIVERNAME = "sun.jdbc.odbc.JdbcOdbcDriver";
*/
final static String USER = "itso";
final static String PASSWORD = "itso";
final static String SELECT =
"SELECT EMPNO, FIRSTNME, LASTNAME, JOB, SEX FROM ITSO.EMPLOYEE
WHERE LASTNAME = :LNAME";

StatementMetaData metaData = null;

Chapter 8.Data Access Beans:VisualAge for Java
99
The class declaration has nothing really fancy in it.As most servlets it is
derived from HttpServlet,it defines the connection parameters,the user ID
and the password.This can be easily changed,or even better,the servlet
might read this from a property file.In the last line a StatementMetaData
Object is declared.This object will contain all information for the query and
in the same turn also for the update of the database.
Initialization
The next part is the init method.The main purpose of this method is to
initialize the connection manager and to create the StatementMetaData
object (Figure 83).
Figure 83.EmployeeServlet init Method
public void init(ServletConfig config) throws ServletException {
super.init(config);
// Specify the connection, now for simplicity this is hardcoded
// It would be nicer to read this from a property file
connSpec = new IBMJdbcConnSpec(POOLNAME,
true,
DRIVERNAME,
URL,
USER,
PASSWORD);
// Now get the static instance of the Connection Manager
// from the Utility Class if not already done
if (connMgr == null)
connMgr = IBMConnMgrUtil.getIBMConnMgr();
// Next step is to prepare the Statement which will be run
// agains the database. Therfor create the MetaData Object
metaData = new StatementMetaData();
// Spefify the select statement for the metaData's SQL statment
metaData.setSQL(SELECT);
try{
// Define the input parameter with the type of class we use
// for input (String) and the the SQL source type (CHAR)
metaData.addParameter("LNAME", String.class, Types.VARCHAR);
// Next define all ouput parameter in the same way
metaData.addColumn("EMPNO", String.class, Types.CHAR);
metaData.addColumn("FIRSTNME", String.class, Types.VARCHAR);
metaData.addColumn("LASTNAME", String.class, Types.VARCHAR);
metaData.addColumn("JOB", String.class, Types.CHAR);
metaData.addColumn("SEX", String.class, Types.CHAR);
// Define the table name which is used in the query
metaData.addTable("ITSO.EMPLOYEE");
} catch (DataException e) {
System.out.println("Error: " + e.toString());
throw new ServletException(e.toString());
}
}

100
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
In the first part a new database connection is specified.The connection
consists of the constants we saw already in Figure 82.Afterwards a new
static reference to the connection manager is created,if not already there.
In the second part a new StatementMetaData object is created.The SQL
select statement (see Figure 82) is then passed to this object.Notice the last
part of the SELECT statement:
".... WHERE LASTNAME = :LNAME"
This variable LNAME will be later replaced by an actual value.To do this,
LNAME has to be specified as an input parameter,which is done in the next
line.Moreover,it also specifies the Java class that will be used to specify this
parameter,and what the resulting SQL type is.If you observe any problems
here refer to “Data Access Beans” on page 266.
The next lines are quite similar except that now the output columns are
specified.The arguments are the SQL column name,the Java class that is
used to represent this value,and the according SQL type.We will see later
that it is useful to provide this information to allowa convenient access to the
result set and,moreover,it is a nice way to update a row.
The last line specifies the SQL table that contains all the given columns.
FormParameter Access
The getParameter method is a helper method (Figure 84).
Figure 84.EmployeeServlet getParameter Method
This method is handy to retrieve the parameter given to a servlet in the
request.It just returns the first value for a key from the request object given
to the servlet.For example,if the HTTP request is:
http://myserver/Servlet?firstName=Tobias&lastName=Himstedt&firstName=Amir
then getParameter(request,"firstName") would return"Tobias".
public String getParameter(HttpServletRequest request, String key) {
String values[] = request.getParameterValues(key);
if (values != null)
return values[0];
return null;
}

Chapter 8.Data Access Beans:VisualAge for Java
101
FormProcessing
The next thing to is to define doGet and doPost.Because they should do the
same,we call the same performTask method so that we are free to use either
HTTP-POST or HTTP-GET.The servlet works with both options (Figure 85).
Figure 85.EmployeeServlet doGet and doPost Methods
Both methods call the performTask method,which itself is also very simple.
Depending on the request,it either calls the method to retrieve the data for
an employee or to update the data (Figure 86).
Figure 86.EmployeeServlet performTask Method
Based on the value for the command parameter in the HttpServletRequest
object,performTask either calls getEmployee to fetch a specific row fromthe
employee table or updateEmployee to update an already fetched row.
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, java.io.IOException {
performTask(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, java.io.IOException {
performTask(request, response);
}
public void performTask(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
String command = getParameter(request, "command");
if ( command == null || command.equals("getEmployee") )
getEmployee(request, response);
else if (command.equals("updateEmployee"))
updateEmployee(request, response);
}

102
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Employee Retrieval
The getEmployee method processes the retrieval of an employee (Figure 87).
Figure 87.EmployeeServlet getEmployee Method (I)
What is happening so far is not very exciting.Basically,the response is
specified,the caching is disabled,the output PrintWriter is created from the
response object,and the parameter for the database query is detected.Next
the beginning of the result page is printed to the PrintWriter.
The next part retrieves a connection to the database based on the
specification made in the init method (see Figure 83) fromthe connection
manager.Fromthis connection a JDBC connection is created.This is used to
create a DatabaseConnection used for the data access beans (Figure 88).
Figure 88.EmployeeServlet getEmployee Method (II)
The next part is about how to use the DatabaseConnection object to create a
SelectStatement based on the SelectMetaData initialized in the init method
(Figure 89).
public void getEmployee(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
IBMJdbcConn conn = null;
Connection jdbcConn = null;
SelectResult resultSet = null;
response.setContentType("text/html");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// Get the output writer
PrintWriter out = response.getWriter();
// Get the lastName paramenter for query
String queryName = getParameter(request, "lastName");
out.println("<HTML><HEAD><TITLE>ConnMgr / DataAccess</TITLE></HEAD><BODY>");
try {
// Get the connection from the connection managers pool
conn = (IBMJdbcConn) connMgr.getIBMConnection(connSpec);
// Get the JDBC connection object
jdbcConn = conn.getJdbcConnection();
// Now use this JDBC connection for the creating
// a connection usable by the data access beans
DatabaseConnection dataConnection = new DatabaseConnection(jdbcConn);

Chapter 8.Data Access Beans:VisualAge for Java
103
To execute the query against the database the query has to be completed with
the actual query value.The value is retrieved from the servlet request
(Figure 87 on page 102) and is then passed as parameter LNAME to the
query.Now the query is ready to execute.After execution the resultSet is
retrieved.
Figure 89.getEmployee Method (III)
In Figure 90 the values for the columns are retrieved.Based on the column
values the HTML output is generated as a table with a form in it.This
enables the user to enter values for all the fields except for the primary key.
Figure 90.EmployeeServlet getEmployee Method (IV)
SelectStatement select = new SelectStatement();
select.setConnection(dataConnection);
select.setMetaData(metaData);
select.setParameter("LNAME", queryName);
select.execute();
resultSet = select.getResult();
String empNo = resultSet.getColumnValueToString("EMPNO");
String firstName = resultSet.getColumnValueToString("FIRSTNME");
String lastName = resultSet.getColumnValueToString("LASTNAME");
String job = resultSet.getColumnValueToString("JOB");
String sex = resultSet.getColumnValueToString("SEX");
out.println("<TABLE>" +
"<FORM ACTION = \"/servlet/itso.wasdb.connaccess.EmployeeServlet\">");
out.println("<TR>");
out.println("<TH>Emp Number");
out.println("<TH>First Name");
out.println("<TH>Last Name");
out.println("<TH>Job");
out.println("<TH>Sex");
out.println("<TR><TD>" + empNo);
out.println(" <TD><INPUT TYPE=TEXT NAME=firstName VALUE=\"" +
firstName.trim() + "\">");
out.println(" <TD><INPUT TYPE=TEXT NAME=lastName VALUE=\"" +
lastName.trim() + "\">");
out.println(" <TD><INPUT TYPE=TEXT NAME=job VALUE=\"" +
job.trim() + "\">");
out.println(" <TD><INPUT TYPE=TEXT SIZE=1 NAME=sex VALUE=\"" +
sex.trim() + "\">");
out.println("<TR><TD><INPUT TYPE=HIDDEN NAME=command VALUE=updateEmployee>");
out.println("<TD><INPUT VALUE=Update TYPE=SUBMIT>");
out.println("</FORM></TABLE>");

104
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Notice the action attribute of the FORMtag.It points back to the same
servlet.In the source code this might look a bit wild,but as HTML output in
the browser,it is straightforward and is shown in Figure 91.
Figure 91.Output of the getEmployee Method
Continuing with the servlet code we have one more important thing to do.We
store the resultSet in a session object associated with the servlet request.
This enables us later to use this resultSet to update the row (Figure 92).
Figure 92.EmployeeServlet getEmployee Method (V)
The last part contains the error handling statements (Figure 93).
Figure 93.EmployeeServlet getEmployee Method (VI)
HttpSession session = request.getSession(true);
session.putValue("itso.wasdb.connaccess.Employee.resultSet",
resultSet);
} catch (IBMConnMgrException e) {
System.err.println("Error: " + e.toString());
e.printStackTrace(out);
} catch (DataException e) {
System.err.println("Error: " + e.toString());
out.println("<H2>Error: " + e.toString() + "</H2>");
e.printStackTrace(out);
}
out.println("</BODY></HTML>");
}

Chapter 8.Data Access Beans:VisualAge for Java
105
Employee Update
After retrieving an employee,the user can update the column values.The
updateEmployee method performs the processing.The method begins with
some administrative code (Figure 94).
Figure 94.EmployeeServlet updateEmployee Method (I)
In Figure 95 the session we created (see Figure 92) is now retrieved fromthe
servlet request.In this session we have stored the resultSet of the query.
Figure 95.EmployeeServlet updateEmployee Method (II)
Next we retrieve the values from the input fields (Figure 96).
Figure 96.EmployeeServlet updateEmployee Method (III)
Now we are ready to performthe update (Figure 97).If the values were
given,they are passed to the resultSet of the query retrieved by the
getEmployee Method.Finally the row is updated and closed;also the
connection manager is informed that the connection is released.
SelectResult resultSet = null;
response.setContentType("text/html");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// Get the output writer
PrintWriter out = response.getWriter();
out.println("<HTML><HEAD><TITLE>Result</TITLE></HEAD><BODY>");
HttpSession session = request.getSession(false);
resultSet = (SelectResult)
session.getValue("itso.wasdb.connaccess.Employee.resultSet");
String lastName = getParameter(request, "lastName");
String firstName = getParameter(request, "firstName");
String job = getParameter(request, "job");
String sex = getParameter(request, "sex");

106
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 97.EmployeeServlet updateEmployee Method (IV)
The last part contains the error handling statements (Figure 98).
Figure 98.EmployeeServlet updateEmployee Method (IV)
Testing the Servlet
To test the servlet you may want to use to WebSphere Test Environment
provided by VisualAge for Java (start the SERunner class).
Open a browser and enter the URL:
http://127.0.0.1:8080/servlet/itso.wasdb.connaccess.EmployeeServlet?lastName=LEE
Alternatively you can launch the servlet fromVisualAge for Java.Before
launching the servlet,you can setup the query parameter (host variable) in
the servlet properties so that an employee is found in the table.
From the context menu of EmployeeServlet select Tools -> Servlet Launcher
-> Properties to display the servlet launcher properties dialog and enter the
values shown in Figure 99.
try {
if (lastName != null)
resultSet.setColumnValue("LASTNAME", lastName);
if (firstName != null)
resultSet.setColumnValue("FIRSTNME", firstName);
if (job != null)
resultSet.setColumnValue("JOB", job);
if (sex != null)
resultSet.setColumnValue("SEX", sex);
resultSet.updateRow();
resultSet.close();
conn.releaseIBMConnection();
} catch (DataException e) {
System.err.println("Error: " + e.toString());
out.println("<H2>Error: " + e.toString() + "</H2>");
e.printStackTrace(out);
}
out.println("<H1>Update successfull</H2>");
out.println("</BODY></HTML>");
}

Chapter 8.Data Access Beans:VisualAge for Java
107
Figure 99.Properties for the Employee Servlet
After launching the servlet the browser should appear as shown in Figure 91
on page 104.You may then enter the text fields to update the database.If the
update was successful,the servlet will let you know with a message.
Deploying Servlets to WebSphere
For detailed instructions on deploying servlets to WebSphere,see chapter 20
of the IBMredbook VisualAge for Java Enterprise Version 2:Persistence
Builder with GUIs,Servlets,and Java Servlet Pages,SG24-5426.
An example of deployment of a servlet to WebSphere is described in
“Deploying a Servlet to WebSphere” on page 128.Make sure that the
c:\IBMVJava\eab\runtime20\ivjdab.jar
file is part of the WebSphere class path.

108
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

© Copyright IBM Corp.1999
109
9 Persistence Builder:
VisualAge for Java
In this chapter we discuss the use of relational databases to provide
persistence for Java objects.We will use the VisualAge for Java Persistence
Builder tool to create persistent classes based upon existing database tables.
We will use our ITSOWDB database as described in Chapter 4,“Relational
Database Installation and Setup” on page 21.
For these examples,the Persistence Builder tool must be added to your
workspace.You can add the feature from File -> Quick Start -> Features ->
Add Feature -> Enterprise Access Builder for Persistence.
If you have already added the WebSphere Test Environment feature,the
Persistence Builder feature will report an error when the feature is added.
As the error message indicates you must remove the Websphere Test
environment feature,then add the Enterprise Access Builder for
Persistence feature again.This will also install the WebSphere Test
Environment for you.
Notice

110
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
What is Persistence?
In Java,as with all programming languages,the state of an object is lost
when the object finishes execution,unless the state of the object is
externalized before execution is complete.This is true for any information
which a program utilizes.In Java,saving the state,or any information,
before programcompletion and restoring the state,or retrieving information,
when the object is instantiated again,is known as persistence.
Java persistence can be achieved by serializing an object.That means
converting the object’s state into a sequence of bytes,then storing the bytes,
which can later be read to resurrect the original object.This way an object
does not need to know how to access a file to retrieve data itself;instead,
whoever calls the object resurrects the entire object,including any data and
the object state.
Using Databases to Provide Persistence
Databases can also be used to provide persistence for Java objects.The
object’s state can be stored in a traditional SQL database and then
resurrected by querying the database.The Persistence Builder is a VisualAge
for Java tool which guides you through the creation of classes that provide
persistence functionality.
Although the tool can be used to create persistent objects fromscratch,one of
Persistence Builder’s most powerful abilities is to take an existing database
and create a persistent Java application using the existing design of that
database.The tool includes all of the relations between tables and database
rules in its design.It creates object definitions based on your tables,which
can be used in your Java applications.
Persistence Builder Tools
For detailed instructions on using the Persistence Builder,review the
redbook VisualAge for Java Enterprise 2:Persistence Builder with GUIs,
Servlets,and Java Server Pages,SG24-5426.
The tool has three major components used to define how the tool will create
your Java code for implementing object persistence.The Schema,Model,and
Mapping Browsers enable you to define the table model,the object model,
and the mapping between the two models.

Chapter 9.Persistence Builder:VisualAge for Java
111
Schema Browser
The Schema Browser is used to import,create,view,and modify the
relationships between the actual tables in your database.This browser is
where the Persistence Builder gathers the necessary information (for
example,column definitions,foreign key relationships) fromyour database to
create objects based on the data stored within.Using your newly created
schema,a model can be generated based on the schema.
Model Browser
The Model Browser creates the actual persistent classes whose data will be
externalized.It adds to these objects attributes and associations fromthe
schema.It is important that the schema truly represents your physical
database to ensure that the objects can interact correctly with the actual
database.In our example the objects will be employee and department.
Map Browser
The Map Browser is used to control mappings between the tables and the
model classes.It is used to map the object attributes and associations to the
actual database tables.
Vendor Specific Considerations
As this book was written,the released version of VisualAge for Java
Persistence Builder only supported the importing of DB2 databases to create
a persistent application’s schema.
A fix for importing Oracle and SQL Server databases to create a schema was
written as this book went to publishing.The fix requires a package to be
imported and a file to be placed in your VisualAge for Java directory.With
this fix,you can import the Oracle or SQL Server database and create the
proper schema.
If your version of VisualAge for Java fails to import these schemas,contact
VisualAge for Java support for the proper fixes.

112
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Persistence Builder Example:Employees in Department
We will attempt to take an existing database from DB2,Oracle,and SQL
Server to create a simple persistent object model.Then we will create a
servlet that uses this model.The servlet takes a department number as input
and returns a list of employees within that department.The first step is to
create a schema for your objects based on your table.
Import the Schema
From your Workbench,open the Schema Browser by selecting Workspace ->
Tools -> Persistence Builder Tools -> Browse Schema.Since we want to create
our schema from an existing database,we use the import function.This type
of design is also known as a bottom-up design because we are starting with a
database and building our application around it.Select Schema -> Import/
Export Schema -> Import Schema from Database.Now enter the name you
wish to call your schema;in our example,we call the schema ITSO.
You must now enter the JDBC connection information (Figure 100).This
connection information is dependent on the database vendor you are using as
a datastore.The connection and driver specifications used here should relate
to the information used to create a connection in Chapter 5,“Java Database
Connectivity” on page 43.
Figure 100.Importing a Schema

Chapter 9.Persistence Builder:VisualAge for Java
113
Once the connection is established,you select the tables you want to import
from.Each table you want to import must be highlighted (use the shift key
with the mouse for multiple selections) as in Figure 101.
Figure 101.Import Table Selection
The Persistence Builder now generates the schema from the database and
displays it in the Schema Browser (Figure 102).
The schema is a representation of your actual database.You should look at
each column’s attribute by double clicking on the attribute and confirm that
it is the correct type (Figure 103).
It is also a good idea to inspect the foreign key relationships.It is important
that all foreign keys fromthe table are displayed in the schema.If a
relationship is not shown,create one based on the physical table in the
database,otherwise the correct classes will not be created later on.
We renamed the foreign key relationship to depts (Figure 102).When the
schema is generated fromyour database,if the foreign key relationship is not
given a name in your SQL statements,the schema browser uses the internal
restraint name used by your database.To rename a relationship,use the
relationship’s context menu and select Rename Foreign Key Relationship.

114
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 102.Schema Browser
Figure 103.Schema Column Editor

Chapter 9.Persistence Builder:VisualAge for Java
115
Generate and Validate the Object Model
The next step is to generate the model from the schema.Select Schemas ->
Generate Model from Schema.The Persistence Builder builds a model of the
objects based on the database schema.You should now open the Model
Browser.You can open it either from the tools menu from the Workbench as
described earlier,or fromthe schema window,select Persistence Builder Tools
-> Model Browser.
The Model Browser (Figure 104) is a view of the object model;instead of
tables and columns it has classes and attributes.
Figure 104.Model Browser
Not all of the column specification from the schema transfers to the object
model.It is important to inspect the classes,attributes,and keys as well.
Notice that the NOT NULL option of columns in the tables does not cause the
Value required checkbox to be marked.You should open all of the attributes
that map to columns that are not null (by double clicking on the class
attribute to edit) and checking the Value required checkbox (Figure 105).

116
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 105.Attribute Editor:Marking the Value Required Field
Inspect the Map
Open the Map Browser (under Persistence Tools) to inspect the mapping
fromthe schema to the model.If the model objects do not map properly to the
physical database,the map browser will inform you.The map was created
when you generated the model fromthe schema.It is important that the
mapping is correct before you attempt to generate the Java code,or some of
the generated Java methods will not function properly.In the Map Browser
select the generated DataStore Map,in this example ITSOItso.
In Figure 106 we see a broken table mapping.In this particular example we
see an error converting the type of the table column to the class attribute.If
you open the table in the Schema Browser,we see that Employee.hiredate
has a type TIMESTAMP.If you then look in the Model Browser,the hiredate
attribute has a type DATE.

Chapter 9.Persistence Builder:VisualAge for Java
117
Figure 106.Map Browser with a Broken Table Map
We have two ways of fixing this mapping problem:we can change the
mapping function by double clicking on the attribute in error,or change the
type of one of the attributes in conflict.We chose to change the type of the
hiredate attribute in the Model Browser to a type of java.sql.Timestamp.The
error displayed in the Map Browser is then cleared as in Figure 107.
Figure 107.Model Browser
If you have found any errors or made any changes in the Schema Browser,
remember to regenerate the model based on the new schema.

118
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Generate the Java Code for the Object Model
Now that the three browsers correctly define how you want your objects to
behave,it is time to generate the actual code.Fromyour Model Browser,
select Models -> Generate.Three options are presented in the SmartGuide
(Figure 108).
Figure 108.Generation Options
The first step is to generate the Java classes and interfaces of the object
model,which is the first radio button.The next panel prompts you for the
project and package names.For our example,we used the itso.wasdb.persist
package.
As shown in Figure 109,you should mark the Generate Bound Beans
Properties checkbox.Model classes with bound properties fire
propertyChange events,which is useful when creating applications using
visual construction.

Chapter 9.Persistence Builder:VisualAge for Java
119
Figure 109.Generation Options (Bean Properties)
The next panel asks you which model elements you would like to generate;
for our example,all of the elements.Click on the Finish button and the
classes are generated.
Generate the Java Code for the Service Classes
Now that the object classes have been defined,it is necessary to generate the
service classes that will handle the persistent aspect of your objects.The
service classes are responsible for externalizing all of the information in your
objects and later resurrecting those objects.
Fromthe Model Browser,select Models -> Generate.This time you will select
the second radio button displayed in Figure 108 on page 118,Data Service
Classes and Interfaces.The next panel prompts you for the type of schema to
generate.We are using the existing relational database to store the
information,therefore we select the third radio button,Relational SQL

120
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
(Figure 110).Enter the package name for the service classes,in our case
itso.wasdb.persist.Services.
Figure 110.Schema Generation Selection
After completing this panel,select Next to display the database connection
information prompt.Enter the correct information to connect to the database
(same as in Figure 100 on page 112) and then select the Generate queries
using parm marker bindings checkbox (Figure 111).

Chapter 9.Persistence Builder:VisualAge for Java
121
Figure 111.Database Connection Information
Next you will be prompted for the model elements to include,in our case all
elements and then click on Finish.If you look in the Workbench,you will see
that your packages have been successfully created.
You are now ready to build a persistent application,but first,save the model,
schema,and map definitions in the itso.wasdb.persist.metadata package (use
the File menu in each browser).
Build the Servlet
Now that all the classes are in place,you can create a visual servlet to
interact with the persistent objects.Fromthe Workbench,select File -> Quick
Start -> Servlets -> Visual Servlet.You are prompted for the project,package,
and name of servlet.Use a naming convention you feel comfortable with.We
used the itso.wasdb.persist.gui package and we named the servlet
EmpsInDept and selected a simple model to work with.

122
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
The next panel asks you the name of the servlet to use for the Action form;we
used the same servlet name.Click on Finish and the SmartGuide creates the
classes and opens the visual composition editor.We will not go into depth
concerning the use of the visual composition editor,instead we display some
of the steps and the resulting design.
Visual Layout of the HTML Output Page
We first have to create the HTML output page itself.In Figure 112,we have
created the form to enter the department name.There are a few HtmlText
beans,an HtmlEntryField that we named DeptEntryField,and a
HtmlPushButton named DeptSubmitButton.The prompt,entry field,and
button are within a three column,single row table.At the bottomwe use
another form with an HtmlResultTable named EmpListResult.Save the
design so that the FormData bean is generated.
The composition of the servlet is very similar to the one given in “Creating a
Servlet Visually using Data Access Beans” on page 86.
Figure 112.HTML Output Page of a Servlet

Chapter 9.Persistence Builder:VisualAge for Java
123
Beans for Data Retrieval
Next we placed the beans required for the logic.We select the FormData bean
from the palette and select the EmpsInDeptFomdata that was generated
when saving the visual layout.This bean represents the HTML formand has
all the information and actions that can take place on the visual form.
Because we are entering a department number,we have to retrieve the
actual department object for that number.If we want to find all employees in
a department,we start with the department object and follow the
relationship to get the employees.To retrieve the department object we use a
and enter the DeptHomeBean class from the itso.wasdb.persist package.
Once that department object is found,it needs a place to be stored,therefore
we place a department object on the page as well.We use a transacted
variable of type DepartmentHomeImpl to represent the department retrieved
through the home class.
We now switch to the Persistence Builder palette to add the objects we will
need to enact the persistence services.We select a read-only transaction bean
because we are only reading information without any updates.We also use a
VapDefaultRelationshipTableModel bean to convert the list of employees into
a displayable form.Both of these icons are in the palette (Figure 113).
Figure 113.Visual Composition Editor with Persistence Builder Palette

124
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Connections for the Logic
Now that we have all the necessary beans,we must add the connections
between the objects (Figure 114).
Figure 114.Composition Editor with Connections
Connections:
1.Retrieve the department when the formis submitted:
EmpsInDeptFormData(Event DeptSubmitButtonPressed) to
DepartmentHome(find(java.lang.String) method)
2.Pass the department number entered to the find method:
EmpsInDeptformData(property deptEntryFieldString) to parameter of
find(deptno)
3.Store the result of the find method as a department object:
NormalResult to DepartmentHomeImpl(this)
4.Set the current transaction for the department:
ReadOnlyTransaction(this) to DepartmentHomeImpl(transaction)
5.Set the current transaction for the employee result:
ReadOnlyTransaction(this) to
VapDefaultRelationshipTableModel(transaction)
6.Retrieve the employees of the department:
DepartmentHomeImpl(property employee) to
VapDefaultRelationshipTableModel(relationship)
6
4
5
7
2
1
3

Chapter 9.Persistence Builder:VisualAge for Java
125
7.Display the employees of the department:
VapDefaultRelationshipTableModel(this) to
EmpListResultTable(tableModel)
Tailor the Result Table
After all of the connections are established,the properties of the
VapDefaultRelationshipTableModel have to be set.The bean needs to know
which columns of data you want to display in the result table.Double click on
the object to access its properties,then alter the columnIdentifiers row (click
on the little push button that appears in the row).Figure 115 shows the
dialog used to set the object class and the properties (columns of the table)
that we want to show in the output.
Figure 115.Column Identifiers
You must enter the full name of the class (itso.wasdb.persist.Employee) and
then its properties are displayed automatically in the middle pane.Select the
desired properties and click on Add to set the columns of the result table.

126
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Test the Servlet
Once you have completed all of these steps,your servlet should be ready to
run.Press Ctrl-S to save your work and then click on the Run icon on the tool
bar.This starts the WebSphere Test Environment (servlet runner) and
displays the HTML page with the default browser (Figure 116).
Figure 116.Servlet Test Output
The servlet does not activate the datastore you have created with the
Persistence Builder tool.For the servlet to work,you must activate the
datastore before doing any processing.The best place to add the datastore
activation is the initialize method of the servlet:
itso.wasdb.persist.Services.ITSOItsoDataStore.singleton().activate();
The singleton method creates one instance of the datastore (independent of
how many times it is called),and the activate method starts the datastore.
Place the statement after the comment line
// user code begin (1)
.This
code is preserved even if the method is regenerated.
Notice

Chapter 9.Persistence Builder:VisualAge for Java
127
Changing the Datastore Database
Once you have created the persistent servlet,it is possible to change which
database manager product is used for persistent storage.You must duplicate
the complete database in the new database manager product.The details of
completing this task is beyond the scope of this book.
To change the datastore,simply regenerate the services class of the
persistent servlet fromthe Model Browser.This time specify the connection
information for the new database manager product.
Another way to change which database product is used as a datastore is to
make a small manual change in the datastore services class.The database
connection information is stored in the getConnectionSpec method of the
ITSOItsoDataStore class in the itso.wasdb.persist.Services package.
This code controls the driver and connection information that your object
uses to communicate with the database.By changing the driver and
connection information,we can change the database product,as long as
identical tables exist in the different database managers.Figure 117 shows
the correct settings for our example to interact with DB2,Oracle,and SQL
Server.Note that this code is removed if you regenerate the service classes!
Figure 117.Establishing Connections to Multiple Database Vendors
//* WARNING: THIS METHOD WILL BE REGENERATED. */
public com.ibm.ivj.db.base.DatabaseConnectionSpec getConnectionSpec() {
DatabaseConnectionSpec aConnectionSpec;
DatabaseConnectionSpec forDB2, forOracle, forSQLServer;
// DB2 UDB app driver
forDB2 = new DatabaseConnectionSpec
("VA1","COM.ibm.db2.jdbc.app.DB2Driver", "jdbc:db2:itsowdb");
// Oracle thin driver
forOracle = new DatabaseConnectionSpec
("VA1","oracle.jdbc.driver.OracleDriver",
"jdbc:oracle:thin:@localhost:1521:orcl");
// SQL Server jdbc/odbc bridge
forSQLServer = new DatabaseConnectionSpec
("VA1","sun.jdbc.odbc.JdbcOdbcDriver", "jdbc:odbc:SQLServer");
aConnectionSpec = forDB2;
// aConnectionSpec = forOracle;
// aConnectionSpec = forSQLServer;
aConnectionSpec.setLogonSpec(new DatabaseLogonSpec ("itso", "itso"));
return aConnectionSpec;
}

128
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Deploying a Servlet to WebSphere
Your new servlet is ready to run on the WebSphere server,but you must
make sure that all the proper resources are available on the Web server.For
detailed instructions on deploying persistent servlets to WebSphere,see
chapter 20 of the IBMredbook VisualAge for Java Enterprise Version 2:
Persistence Builder with GUIs,Servlets,and Java Servlet Pages,SG24-5426.
The first thing to move to WebSphere is the datastore.Be sure that the
database tables and files are available on the WebSphere server.It is
important that the database is identical to the database used in the test
environment.
Next export all of the servlet code and copy it to WebSphere.In our example,
we created three packages which must be exported (Figure 118).Place all of
these classes in the WebSphere servlet directory.
Figure 118.Exporting the Servlet

Chapter 9.Persistence Builder:VisualAge for Java
129
With the code moved to WebSphere,it is imperative that all of the classes
that your persistent application utilizes are available in the WebSphere
environment.
We do not go through all of the WebSphere administration pages.For now,
the most important setting to get VisualAge for Java servlets to work is the
class path setting,and that is on the Setup -> Java Engine page (Figure 119).
Figure 119.WebSphere Application Server:Java Engine Setup
We used the following class path:
D:\jdk1.1.6\lib\classes.zip;<=== JDK
D:\WebSphere\AppServer\classes;<=== export directory
D:\WebSphere\AppServer\web\classes;
D:\SQLLIB\java\db2java.zip;<=== DB2 JDBC Drivers
D:\orant\Jdbc\lib\classes111.zip;<=== Oracle JDBC Drivers
D:\IBMVJava\eab\runtime20\ivjdab.jar;<=== data access beans
D:\IBMVJava\eab\runtime20\ivjsb21.jar;<=== servlet builder
D:\IBMVJava\eab\runtime20\ivjpb20.jar;<=== persistence builder
D:\IBMVJava\eab\runtime20\ivjpb20e.jar;<=== pers. builder extras
(see below)
D:\IBMVJava\hpj\lib\swingall.jar;<=== swing

130
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
To simplify the class path,we copied the JAR files into the directory
D:\WebSphere\AppServer\lib.
Jar files in the lib subdirectory are automatically
added to the WebSphere class path and do not have to be specified in the
administration setup.
Three of these JAR files are special cases:

The ivjpb20e.jar file is not provided by VisualAge for Java.We built this
file ourselves by exporting the VisualAge Persistence Extras project from
VisualAge for Java.This is the project with the additional classes to
interact with AWT and Swing fromPersistence Builder home and
relationship collections.

The ivjdab.jar file contains the same classes as the databean.jar file
provided by WebSphere,plus extra GUI classes.To make sure that the
ivjdab.jar file is loaded before the databean.jar file,we added it to the
class path.

The ivjpb20.jar file contains the same classes as the ejs.jar file provided
by WebSphere,plus extra classes.To make sure that the ivjpb20.jar file is
loaded before the ejs.jar file,we added it to the class path.
Your persistent servlet should successfully run in WebSphere with this class
path setting.

© Copyright IBM Corp.1999
131
10 Enterprise
JavaBeans
In this chapter we will briefly discuss what Enterprise JavaBeans are and
how they are used in conjunction with WebSphere,VisualAge for Java,and
databases.
For further information refer to
http://java.sun.com/products/ejb/docs.html
or
to the redbook Enterprise JavaBean Development with VisualAge for Java,
SG24-5429.
Note that Enterprise JavaBeans is the name of the architecture as specified
by Sun;what we develop we will simply call enterprise beans.

132
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Enterprise JavaBeans froma Bird’s-Eye Perspective
An Enterprise JavaBean is a nonvisual component which runs on the server
side of a distributed application.The enterprise bean itself lives in an
environment called an EJB container.The EJB container in turn runs in an
EJB server or Enterprise Java Server (EJS).The goals of the EJB
architecture include:

Making life easier for the developer:The developer does not have to
struggle with low-level entities such as persistence,threads,and
transactions;in that the EJB architecture enables the developer to
concentrate on the business logic.

Making server applications portable:The model ensures that Java
platform server components can be developed once and deployed
anywhere.

Making the server application independent:The Enterprise JavaBeans
architecture is completely independent from any specific platform,
protocol,or middleware infrastructure.Applications that are developed
for one platformcan be picked up,moved,and redeployed to another
platform.
A Closer Look
So far we have seen the abstract term of enterprise bean,EJB containers,
and EJB servers.We will now take a closer look at those terms.
There are two types of enterprise beans:session beans and entity beans.

Session beans can be stateful or stateless.Stateful session beans exist
for the duration of a single client/server session.Stateless session beans
are pooled by the container to handle multiple requests from multiple
clients.

Entity beans contain persistent data that can be saved in various
persistent data stores.Each entity bean carries its own identity;that
means entity beans do have primary keys.Entity beans that manage their
own persistence are called bean managed persistence (BMP) entity beans.
Entity beans that delegate their persistence to the EJB container are
called container managed persistence (CMP) entity beans.
The home interface specifies the available methods for locating,creating,
and removing instances of enterprise bean classes.This is normally provided
by the developer.

Chapter 10.Enterprise JavaBeans
133
The remote interface specifies the business methods of the enterprise bean.
Typically,the developer of the enterprise bean also defines the remote
interface.
An EJB container is a runtime environment that manages one or more
enterprise beans.Specifically,containers manage the life cycles of enterprise
bean objects,coordinate distributed transactions,and implement object
security.The deployment descriptor,a serialized object that contains
run-time settings for an enterprise bean,tells the EJB container how to
manage and control an enterprise bean.Generally,each EJB container
contains a set of enterprise beans.EJB containers are typically provided by
EJB servers.
An EJB server is a high-level process or application that provides a
run-time environment to support the execution of server applications that
use enterprise beans.An EJB server provides a JNDI-accessible naming
service,manages and coordinates the allocation of resources to client
applications,provides access to system resources,and provides a transaction
service.An EJB server could be,for example,a database or application
server.
Developing a Container Managed Persistence Entity
Bean
In this section we develop a simple enterprise bean.Because this book is
about WebSphere and database integration,we develop this entity bean
based on the employee/department sample that we are using throughout this
book.For the development process we utilize VisualAge for Java.Moreover
we show howto deploy the enterprise bean to use it within WebSphere froma
servlet,and we also show how to use this enterprise bean from an applet or
application.
In our version of WebSphere Application Server (Version 2.0.2),and also in
the WebSphere Test Environment of VisualAge for Java,container
managed persistence (CMP) only works with DB2 and not with Oracle and
SQL Server.Therefore,this first example will only work with DB2.
The next example uses bean managed persistence (BMP) and works with
all JDBC compliant databases.
Notice

134
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Setup of VisualAge for Java
Follow these steps to set up VisualAge for Java:

Make sure you added the IBMEJB Development Environment 1.1 feature
to VisualAge for Java.To see whether you have installed this feature,go
to File -> Quick Start,select Features on the left side and Delete Feature
on the right side.This will show you all installed features (Figure 120).
Figure 120.Available Features

Add the feature if it has not been added to the Workbench.

In your project (we used ITSO SG24 5471 as the project name) create a
new package called itso.wasdb.ejb.This will contain the enterprise bean
we develop.

Next go to the EJB pane in the Workbench (Figure 121).

Now create a new EJB group in the EJB pane.From the EJBs menu
select Add -> EJB Group.Select the project (ITSO SG24 5471) and give
the EJB group the name ITSO_EJBs.

Chapter 10.Enterprise JavaBeans
135
Figure 121.EJB Development Pane:Adding an EJB Group
Create a Department Enterprise Bean
To create an enterprise bean:

Fromthe context menu of the new EJB group select Add -> EJB.Our first
EJB will be related to the department table already used throughout the
book,so name the bean Department.As we want the container to manage
the persistence service,select Entity Bean with container managed
persistence fields (CMP) as the bean type.Select the project (ITSO SG24
5471) and the itso.wasdb.ejb package (Figure 122).

136
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 122.EJB SmartGuide
In the next dialog of the SmartGuide you do not have to make any
changes,so click on Finish.

The SmartGuide has now created a very basic framework for the EJB.It
should have created three interfaces and two classes:
• Department interface:This currently offers no methods to be
implemented.Later on,this will contain the abstract method
declarations of all methods that will be implemented by the
DepartmentBean class,which represents the business logic.
• DepartmentBean class:This class will contain the actual business
logic.The SmartGuide has already created several methods for this
class,including a method that creates the EJB.Because this is an
entity bean,the SmartGuide also created a primaryKey property.

Chapter 10.Enterprise JavaBeans
137
• DepartmentBeanFinderHelper interface:This should be empty at the
current state.It might later contain specialized SQL search strings
that allow a certain order within the various EJB instances.
• DepartmentHome interface:This contains a method to find a specific
bean by its primary key or create a new bean with a new primary key.
• DepartmentKey class:This class represents the primary key for the
entity bean.For the Department table,this will be mapped to the
DEPTNO column.
Add the Properties and Methods to the Bean
Now we add the actual properties and business logic to the skeleton classes.

The properties are basically nothing but the columns fromthe department
table.The business logic in this example is limited to get and set methods
for the properties.The department table consists of the following columns:
DEPTNO,DEPTNAME,MGRNO,ADMRDEPT,and LOCATION.
Therefore,the DepartmentBean contains the matching properties and also
the get and set methods for the properties,except for the primary key
property that should not be alterable.
As described in the previous step,the SmartGuide already created a
primary key property.Because it should match the department table,the
first action is to add the deptNo property and make it the primary key
field.Highlight the DepartmentBean class in the Types pane and use the
Create Field SmartGuide to add a public field named deptNo of type
String.Let the SmartGuide to create a get method for this property,but a
set property is not appropriate for a primary key (Figure 123).
Figure 123.Adding a Field

138
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

Now make the deptNo field the primary key field.Click on the fields icon
in the Type pane (Figure 124).The Types pane is now changed to a Fields
pane.
Here you see all the fields (properties) of the bean.It contains now three
properties,the primaryKey property,an entityContext property,and the
newly created deptNo property.The primaryKey property is currently the
key field (it has the key icon on the right side) and it is a container
managed field,identified by the cup-like icon on the right side.
Figure 124.Switching to the Fields Pane
To make the deptNo property the primary key,right-click on that deptNo
entry and select Key Field.It should now show both icons,the cup and the
key.

Chapter 10.Enterprise JavaBeans
139
Now we delete the primaryKey property generated by the SmartGuide by
removing it fromthe bottomsource pane.After deleting the primaryKey
property,the ejbCreate method in the Methods pane reports a problem
that we have to repair.In the method body of ejbCreate,replace
primaryKey by deptNo (
deptNo = key.deptNo;
).

Everything is now ready to add the rest of the properties.Switch back to
the Types pane and select the DepartmentBean class.Use the Create Field
SmartGuide to add the deptName,location,mgrNo,and admrDept
properties (all public,of type String,with getter and setter methods).

By now your Methods pane should be a bit more filled.Next,make all the
getter and setter methods available to the EJB Remote Interface.To do
this,right click each getXXX and setXXX method,and select Add To ->
EJB Remote Interface (Figure 125).After this,each method contains a
little unidentifiable icon on the right,and the department interface offers
all the methods you have just added to the remote interface.
Figure 125.Add Methods to the Remote Interface

140
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

Next,mark all the properties we have defined to be persistent.Switch
back to the Fields pane,right click on each field and mark it Container
Managed (Figure 126).
Figure 126.Marking Properties Container Managed
Map the Bean to the Schema
The bean is persistent in the relational database.We import the database
schema and map the properties of the bean to the columns of the department
table.We describe this only briefly because we covered schema mapping in
detail in Chapter 9,“Persistence Builder:VisualAge for Java” on page 109.

Start the Persistence Builder Schema Browser from the menu EJBs ->
Open To -> Database Schemas.

From the menu select Schemas -> Import/Export Schema -> Import
Schema from Database and name the new schema ITSO_Schema.

Specify driver,URL,user ID,and password (Figure 127).
Figure 127.Connection Information for the Schema Import

Chapter 10.Enterprise JavaBeans
141

Build the table list based on the ITSO qualifier and select the
DEPARTMENT table (Figure 128).
Figure 128.Select the Table to Import

We do not have to modify the imported schema (Figure 129).Close the
Schema Browser.
Figure 129.Schema Browser after Import

142
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

After importing the schema we performthe mapping.Open the Map
Browser by selecting EJBs -> Open To -> Map Schema.Fromthe
Datastore_Maps menu select New EJB Group Map.Name the map
ITSO_Map,select the EJB group and the schema (Figure 130).
Figure 130.Specifying the Datastore Map for the Enterprise Bean

The next step is to associate the properties of the EJB to the columns of
the department table.Select Table_Maps -> New Table Map -> Add Table
Map with No Inheritance and select the DEPARTMENT table (Figure
131).
Figure 131.Map Browser

Chapter 10.Enterprise JavaBeans
143

We associate each property to an attribute of the database table.Open the
Property Map Editor fromthe context menu of the DEPARTMENT table
map.Click on the Map Type in each column to select the Simple mapping
and go on to select the matching column from the database table (Figure
132).When done,close the property map editor and the Map Browser.
Figure 132.The Property Map Editor
Generate the Code and Test
Now everything is prepared to generate the code:

Right-click on the EJB group ITSO_EJBs and select Generate -> Deployed
Code.

Right-click on the EJB group ITSO_EJBs and select Generate -> Test
Client.
As a result the Types pane now shows more classes.
To test an enterprise bean VisualAge for Java contains a basic EJB server.To
add the enterprise bean to the EJB server,right-click on the ITSO_EJBs EJB
group and select Add To -> Server Configuration (Figure 133).

144
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 133.EJB Server Configuration Dialog
Starting the EJB Server in the Test Environment
Now we have to start each of the services:

Start the Location Service Daemon.Right-click on it and select Start
Server.

Next start the Persistent Name Server the same way.

Before you start the EJB server itself,ensure that the properties are
correct.Right-click on the EJB Server and select Properties.Make sure
the database URL (jdbc:db2:itsowdb),user ID (ITSO) and password (itso)
are correct (Figure 134).
Figure 134.EJB Server Properties

Chapter 10.Enterprise JavaBeans
145

Finally start the EJB server.The console should display all the servers as
shown in Figure 135.The server is now ready and the enterprise bean is
deployed in the test environment.
Figure 135.EJS Console Output
Wait for all the servers to be ready before continuing with testing.
Using the Test Client
Now we can test the enterprise bean with the test client we generated a few
steps earlier.

Select the Department EJB in the EJB Server Configuration Window and
click on the Run Test Client button (the yellow running man icon in Figure
133) to open the test client window.
The test client window is the same for all enterprise beans (Figure 136).

146
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 136.Test Client

Click the Connect Button and the test client presents the home interface
of our department bean (Figure 137).This is only used to find or create an
instance of the department enterprise bean.
Figure 137.The Home Interface

Chapter 10.Enterprise JavaBeans
147

Now we retrieve a department with a given primary key:
• Select the findByPrimaryKey entry in the list of methods on the left
side.
• Click on the New button in the parameters list on the right side.
• Select new DepartmentKey(String) in the constructors pane and enter
a department number (for example,E21) as the parameter in the
DepartmentKey dialog (Figure 138).
• Click on the Send button and then on the Done button.
Figure 138.Parameter Specification for the DepartmentKey

Back in the Home Interface Dialog (Figure 137) click the Send button and
the Home Interface should change to the Remote Interface,which
represents the remote interface for the actual business logic.
• You may now execute the methods of the enterprise bean;for example,
to retrieve the department name,select the getDeptName method and
click the Send button (Figure 139).
• You can use the setXxxxx methods to change the values in the
department bean retrieved.Each Send of a set method changes the
column value in the table.(You can open a DB2 command window to
verify the changes in the table.)

148
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 139.Test Client Presenting the Remote Interface

You may now play around with the test client.For instance,you might
create a new DepartmentBean using the home interface,and use the
setXxxx methods to set the properties and therefore the column values in
the department table.
Note that the Send key for the create(DepartmentKey) method returns
with an error message from DB2:
com.ibm.ejs.container.ContainerException:
com.ibm.ejs.persistence.EJSPersistenceException:
COM.ibm.db2.jdbc.app.DB2Exception: [IBM][CLI Driver][DB2/NT]
SQL0407N Assignment of a NULL value to a NOT NULL column "" is not allowed.
SQLSTATE=23502
The columns DEPTNAME and ADMRDEPT were defined as not null.We
have to provide initial values.We can change the create(DepartmentKey)
method in the EJB Workbench pane to set default values for the required
fields:
public void ejbCreate(DepartmentKey key) {
// All CMP fields should be intialized here.
deptNo = key.deptNo;
deptName = "Unknown";
admrDept = " ";
}
With this change the creation of a new department works.The
department name and the other fields (columns) can be changed
afterwards using the set methods.

Chapter 10.Enterprise JavaBeans
149
Deploy the Enterprise Bean to WebSphere
The next step is to extract the enterprise bean and take it into a real
environment,which is the WebSphere Application Server.WebSphere
provides an infrastructure to run a container-managed persistence entity
bean coded according the Sun’s EJB specification.
To deploy a bean within the WebSphere Application Server,performthe
following steps:

First,export a JAR file containing the bean.You have two choices here:
• One way is to export an already deployed version of the bean.
VisualAge for Java can do this for you.The advantage of this method is
that WebSphere keeps the mapping fromthe bean properties to the
table attributes.
To export the JAR file,right-click on the department EJB in the EJBs
pane and select Export -> EJS Jar.You should place the resulting JAR
file in the deployableEJBs directory of WebSphere (for example,
c:\WebSphere\AppServer\deployableEJBs\DepartmentEJS.jar
).
• The other way is to export just the bean code and let WebSphere do the
deployment process.But this has the effect that the mapping which we
have just done will be lost.During the deployment process,WebSphere
will try to create a new schema and a new table in the schema,which
reflects the properties of the bean.If we had started from scratch,this
method might be fine,but because we already have some data in our
database,this is not appropriate.
Export the JAR file (Export -> Export EJB Jar) and WebSphere will do
the main part of the deployment process.

The next step is to generate a new EJB container in WebSphere.This
container holds the database specification:URL,user ID,and password.If
WebSphere supports other database systems for container-managed
beans this might be the place where you must also specify the driver.But
because WebSphere currently only supports DB2,we do not have to
provide a driver.
To create a new container,open the WebSphere administration applet.Go
to the Enterprise Java Services -> Global Settings.Make sure EJS is
enabled and the host name matches your machine name (Figure 140).If
you use localhost,later on you will not be able to access the EJB remotely.
Apparently the naming service on the machine running the EJB server
differentiates between localhost and servername.

150
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 140.WebSphere General EJB Settings

In the Containers section add a new container with the name
ITSOContainer.Select the container and provide the specification:
• Because we have written an entity bean,we will use the
com.ibm.ejs.container.EJSEntityContainer class.This is one of two
container classes which WebSphere provides.The other container
class,which is for session beans,is accordingly named
com.ibm.ejs.container.EJSSessionContainer.
• Next we have to specify the JDBC URL:jdbc:db2:itsowdb.
• The directory in which to put the deployed beans is deployedEJBs.
• Finally,specify the user ID and password (ITSO and itso).
At the bottomof this pane,you will notice a list box that shows the beans
that are already deployed in the selected container.Because we have just
created a new container,no beans are listed here.
Click on Save to preserve the changes (Figure 141).

Chapter 10.Enterprise JavaBeans
151
Figure 141.WebSphere Container EJB Settings

Now we are ready to deploy the bean.In the EJB Jar Files pane (Figure
142),select the DepartmentEJS.jar on the left and click the Deploy button.
Figure 142.EJB Jar File Pane

In the Deploy Jar File dialog,select the ITSOContainer from the
combobox and click on Deploy (this dialog is not shown).

152
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

WebSphere should now present a dialog box,warning you that the
deployment was already done (Figure 143).Do not regenerate the jar file,
only redeploy it (click on Redeploy Existing).If you regenerate,WebSphere
will loose the mapping fromthe bean properties to the database table and
will create a new schema and table,which is not what we want to happen.
Figure 143.Redeployment Warning

You should receive a message that the Department bean was deployed
successfully.

To make the bean available,it is necessary to restart WebSphere.Stop the
WebSphere Servlet Service and the IBMHTTP Server and restart the
HTTP Server (which in turn should start up WebSphere as well).

You can test the deployed bean by using the test client fromVisualAge for
Java.After starting the test client,connect to the bean using the URL:
iiop://hostname:9019
WebSphere will not start the EJB services properly if there are any entity
containers that are invalid in terms of using a non-existent or
non-accessible database.For example,if you did not reate the sample
database from DB2,then the defaultEntityContainer will not be able to
start because the database it uses is not there.In case of such a failure,
WebSphere apparently does not start up the services at all.
The simplest and safest solution is to remove all unused
containers.Do this by inspecting all containers in respect to the
deployed beans they contain.
Notice

Chapter 10.Enterprise JavaBeans
153
Access the Department Bean froma Servlet
Nowthat our bean is deployed,how can we access it?In our first example,we
access the enterprise bean through a servlet.This servlet is very simple and
contains nothing exciting except for the methods to create or find our
enterprise bean.The servlet enables the user to retrieve a bean and to show
and update its properties.
The servlet (DepartmentEJBServlet class in itso.wasdb.ejb.servlet package)
contains the following methods:

doPost and doGet:Both methods just call the performTask method.This
makes the servlet independent of the selected HTTP method to transfer
the form data.We will not list these methods here,instead refer to Figure
85 on page 101.

performTask:Based on the request,this method directs the flow either to
the getDepartment method or the updateDepartment method.

getParameter:This is a simple helper method to retrieve a parameter from
an HTTP request object.See Figure 84 on page 100.

init:This initializes the servlet.This method contains the code to start a
connection to the EJB server.

createOrtFindDepartment:Based on a given key,this method tries to find
a department;and if that department does not exist,it creates a new one.

getDepartment:This method retrieves a department bean and present its
contents in an HTML formthat enables the user to update the bean.It
also attaches the bean to the session for usage in the next interaction.

updateDepartment:This method performs the update of the contents of
the bean.Based on the form data,it uses the setter methods of the bean
that was retrieved in the getDepartment method.
Class Declaration
The DepartmentEJBServlet’s class declaration is shown in Figure 144.It
contains two class variables:

The departmentHome Variable represents the home interface of the
DepartmentBean

The initContext represents the initial context of the naming service.This
makes it possible to retrieve the home interface of the department bean.

154
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 144.DepartmentEJBServlet Class Declaration
PerformTask Method
The performTask method is simple.Depending on the request,it calls either
the getDepartment or the updateDepartment method (Figure 145).
Figure 145.DepartmentEJBServlet performTask Method
Initialization
The init method (Figure 146) initializes the servlet.It retrieves the initial
context,which provides a starting point for naming and directory operations.
Once we have this initial context,we can use it to look up other contexts and
objects.This is done in the next few lines of code.
The initial context is used to look up an object that is registered under the
name Department.Using a static helper method from the generated code,
this object is then cast to an object of type DepartmentHome,which is the
home interface of the department bean and serves for creating and finding of
a department enterprise bean.The resulting object is stored in the
departmentHome class variable.
package itso.wasdb.ejb.servlet;
import javax.servlet.http.*;
import javax.servlet.*;
import itso.wasdb.ejb.*;
public class DepartmentEJBServlet extends HttpServlet {
DepartmentHome departmentHome = null;
InitialContext initContext = null;
public void performTask(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
String command = getParameter(request, "command");
if ( command == null || command.equals("getDepartment") )
getDepartment(request, response);
else if (command.equals("updateDepartment"))
updateDepartment(request, response);
// else print some error message ...
}

Chapter 10.Enterprise JavaBeans
155
Figure 146.DepartmentEJBServlet init Method
Retrieve a Department Bean
The next method to look at is the getDepartment method that uses the home
interface to either create a new enterprise bean or find an already existing
bean based on the primary key given in the HTTP request (Figure 147).
We do not describe the method in detail,but will concentrate on the
interesting parts that are marked in bold face.
public void init(ServletConfig config) throws ServletException {
super.init(config);
System.out.println("Retrieving initial context...");
java.util.Hashtable properties = new java.util.Hashtable(2);
// local name server
// comment next line to run within VA Java
properties.put(javax.naming.Context.PROVIDER_URL, "iiop://sonoma:9019");
// IBM name services
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"com.ibm.jndi.CosNaming.CNInitialContextFactory");
try {
initContext = new InitialContext(properties);
Object obj;
// lookup the home interface using the JNDI name
System.out.println("Retrieving the home interface...");
Object o = initContext.lookup("Department"); // this is the JNDI name
if (o instanceof org.omg.CORBA.Object)
departmentHome = DepartmentHomeHelper.narrow((org.omg.CORBA.Object) o);
System.out.println("Creating DepartmentHome...");
} catch (NamingException e) {
throw new ServletException(e.toString());
}
}

156
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 147.DepartmentEJbServlet getDepartment Method
public void getDepartment(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
response.setContentType("text/html");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// Get the output writer
java.io.PrintWriter out = response.getWriter();
// Get the deptNo to look for
String deptNo = getParameter(request, "deptNo");
out.println("<HTML><HEAD><TITLE>DepartmentEJBServlet </TITLE></HEAD><BODY>");
try {
Department dep = createOrFindDepartment(deptNo);
out.println("<TABLE> <FORM ACTION" +
"= \"/servlet/itso.wasdb.ejb.servlet.DepartmentEJBServlet\">");
out.println("<TR>");
out.println("<TH>Department Number");
out.println("<TH>Department Name");
out.println("<TH>Department Location");
out.println("<TH>Admr. Department");
out.println("<TH>Manager Number");
out.println("<TR><TD>" + deptNo);
out.println(" <TD><INPUT TYPE=TEXT NAME=deptName VALUE=\"" +
dep.getDeptName() + "\">");
out.println(" <TD><INPUT TYPE=TEXT NAME=location VALUE=\"" +
dep.getLocation() + "\">");
out.println(" <TD><INPUT TYPE=TEXT NAME=admrDept VALUE=\"" +
dep.getAdmrDept() + "\">");
out.println(" <TD><INPUT TYPE=TEXT NAME=mgrNo VALUE=\"" +
dep.getMgrNo() + "\">");
out.println("<TR><TD><INPUT TYPE=HIDDEN NAME=command VALUE=updateDepartment>");
out.println("<TD><INPUT VALUE=Update TYPE=SUBMIT>");
out.println("</FORM></TABLE>");
HttpSession session = request.getSession(true);
session.putValue("itso.wasdb.ejb.servlet.DepartmentEJBServlet.department",
dep);
} catch (Exception e) {
out.println("<h2>Error: " + e + "</h2>");
e.printStackTrace(out);
}
out.println("</BODY></HTML>");
}

Chapter 10.Enterprise JavaBeans
157
The first point of interest is where the actual enterprise bean is obtained.
This is done by the createOrFindDepartment method based on a primary key
given in the HTTP request.The contents of this bean are then presented in
an HTML form.
This leads to the next interesting point:The bean itself is attached to the
current HTTP session for later usage in the updateDepartment method.
Create or Find a Department Bean
Now let us inspect the createOrFindDepartment method (Figure 148).This
method tries to find an enterprise bean,and if it cannot find one,it creates a
new enterprise bean.Both operations utilize the home interface of the
department bean.In any case,it will return the bean to the caller.
Figure 148.DepartmentEJBServlet createOrFindDepartment Method
Update the Department Bean
The last method that is important for our servlet is the updateDepartment
method (Figure 149).The interesting parts are again shown in bold face.
Basically,the session that was created in the getDepartment method is used
to retrieve the attached department bean.Then,based on the formdata,it
follows a simple sequence of setXxxx invocations of the department bean.
That is the update!
public Department createOrFindDepartment(String key) throws NamingException,
ObjectNotFoundException, RemoteException, CreateException {
Department dep = null;
try {
dep = departmentHome.findByPrimaryKey(new DepartmentKey(key));
} catch (javax.ejb.FinderException e) {
System.out.println("Department not found! Create new one");
dep = departmentHome.create(new DepartmentKey(key));
}
return dep;
}

158
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 149.DepartmentEJBServlet updateDepartment Method
Test the Servlet in VisualAge for Java
Start the WebSphere Test Environment (SERunner class) and make sure
that the EJB server is running (see “Generate the Code and Test” on
page 143).
Comment one line in the init method (Figure 146 on page 155):
// properties.put(javax.naming.Context.PROVIDER_URL, "iiop://sonoma:9019");
Invoke the servlet with the URL:
http://127.0.0.1:8080/servlet/itso.wasdb.ejb.servlet.DepartmentEJBServlet?
deptNo=E21
Executing the servlet results in the browser output shown in Figure 150.
public void updateDepartment(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
response.setContentType("text/html");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
java.io.PrintWriter out = response.getWriter();
out.println("<HTML><HEAD><TITLE>Result</TITLE></HEAD><BODY>");
HttpSession session = request.getSession(false);
Department dep = (Department)
session.getValue("itso.wasdb.ejb.servlet.DepartmentEJBServlet.department");
String location = getParameter(request, "location");
String admrDept = getParameter(request, "admrDept");
String mgrNo = getParameter(request, "mgrNo");
String deptName = getParameter(request, "deptName");
dep.setLocation(location);
dep.setAdmrDept(admrDept);
dep.setMgrNo(mgrNo);
dep.setDeptName(deptName);
out.println("<H1>Update ok</H1>");
out.println("</BODY></HTML>");
}

Chapter 10.Enterprise JavaBeans
159
Test the Servlet in WebSphere
Now let us try to invoke the servlet with the deployed enterprise bean under
WebSphere.Export the class file of the servlet fromVisualAge for Java to the
WebSphere servlets directory:
c:\WebSphere\AppServer\servlets\itso\wasdb\ejb\servlet
Invoke the servlet with the URL:
http://hostname/servlet/itso.wasdb.ejb.servlet.DepartmentEJBServlet?
command=getDepartment&deptNo=E21
Executing the servlet results in the browser output shown in Figure 150.
Figure 150.DepartmentEJBServlet in a Browser
You may now change the contents of the bean and click on Update.Keep in
mind that you should not exceed the lengths of the fields;otherwise,the
update will not succeed.For example,the Admr.Department may not be
longer than three characters.
If the update was successful,the servlet will let you know.Click on Back to
retrieve the enterprise bean again and verify that the bean has changed.

160
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Access the Department Bean froman Application
In the same way that we have used a servlet to access our enterprise bean,
we might want to use an application or an applet.We will develop a simple
panel that works the same way our servlet does.The user can enter a
department number;if the department exists the applet will show its
content,if not it will create a new department bean.This panel might be
added to an application or to an applet.
Create the GUI Layout
We use VisualAge for Java to develop the applet.

Create a new package called itso.wasdb.ejb.client.

In this package create a new class called DepartmentPanel.Derive this
class fromthe Swing JPanel class and mark the Compose the class
visually checkbox in the SmartGuide.

In the Visual Composition Editor,design the panel.
The final panel with connections is shown in Figure 151.
Figure 151.Department Panel with All Connections
1
2
2
3
4
5
6
6
6

Chapter 10.Enterprise JavaBeans
161
Create the Connections
1.First,we have to create a connection to the server to retrieve the initial
context and the home interface of the department bean.This requires a
method to initialize the context and the home interface based on the
server name.We will use a new method called createOrFind.
For the Create/Find Department button,select Event to Code fromthe
context menu and enter the createOrFind method code (Figure 152).
Notice that the code is almost the same as for the servlet (Figure 148 on
page 157),and again the error handling is only very rudimentary.
2.For the parameters of the method,select the text field next to the
Servername label and the text field next to the Create/Find Department
button.
3.The createForFind method provides our department bean as a result.To
be more precise,it provides an implementation of the Department
interface.To store this object we use a variable named department and we
use the context menu to change its type to Department.Therefore,we
connect the normalResult of the createOrFind method to our department
variable (this property).
4.Now that department is a valid object,we connect each property of
department (admrDept,location,mgrNo,and deptName) to the matching
text properties of the text fields.
5.For the Update button we connect actionPerformed to the setLocation
method of department.Because the setLocation method requires a
parameter,we provide it with the text property of the department name
text field.
6.Next,we do the same for the other properties:admrDept,mgrNo,and
deptName.

162
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 152.DepartmentPanel createOrFind Method
public itso.wasdb.ejb.Department createOrFind(String key, String adress) {
// Get the initial context
if (initContext == null) {
try {
System.out.println("Retrieving initial context...");
java.util.Hashtable properties = new java.util.Hashtable(2);
// local name server
// comment the next line to run inside VA Java
properties.put(javax.naming.Context.PROVIDER_URL, "iiop://" + adress +
":9019");
// IBM name services
properties.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY,
"com.ibm.jndi.CosNaming.CNInitialContextFactory");
initContext = new javax.naming.InitialContext(properties);
} catch (javax.naming.NamingException e) {
System.out.println("Error retrieving the initial context: " + e);
} // endtry
}
Object obj;
if (departmentHome == null) {
// lookup the home interface using the JNDI name
try {
System.out.println("Retrieving the home interface...");
Object o = initContext.lookup("Department"); // this is the JNDI name
if (o instanceof org.omg.CORBA.Object)
departmentHome = DepartmentHomeHelper.narrow((org.omg.CORBA.Object) o);
else
System.out.println("Type mismatch");
} catch (javax.naming.NamingException e) {
System.out.println("Error retrieving the home interface: " +
e.getMessage());
} // endtry
}
System.out.println("Creating Department...");
Department dep = null;
try {
dep = departmentHome.findByPrimaryKey(new DepartmentKey(key));
System.out.println("Department found!");
}
catch (javax.ejb.ObjectNotFoundException e) {
System.out.println("Department not not, create a new one");
try {
dep = departmentHome.create(new DepartmentKey(key));
} catch (Exception excp) {
excp.printStackTrace(System.out);
System.exit(1);
}
} catch (Exception e) {
e.printStackTrace(System.out);
System.exit(1);
}
return dep;
}

Chapter 10.Enterprise JavaBeans
163
Test the Applet or Application
Because VisualAge for Java automatically creates a main method for testing
purposes,we can already test the panel.The normal way is to imbed the
DepartmentPanel in an applet or application.We do not show how this
process is done because it is just a trivial step.However,the sample
repository provides an applet and an application that use the
DepartmentPanel.
To test the panel,execute it,enter your server name and a valid department
number,and click Create/Find Department button (Figure 153).
Figure 153.Department Panel Displaying the Department Bean
We were not able to access the department bean from an applet running
inside a browser,although it was possible to run the applet inside a remote
applet viewer.
Presumably,this has something to do with the security restrictions under
which the applet has to run inside a browser.
Notice

164
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Developing a Bean Managed Persistence Entity Bean
In this section we will develop an entity bean that manages its persistence
itself.For this example,we will take the second table of our sample database
and develop a bean that stores its state to the employee table of the database.
We will again start with the SmartGuide of VisualAge for Java to create the
bean.
Create an Employee Enterprise Bean with the SmartGuide
Similar to the process of adding a container managed bean,we now have to
add another bean to our ITSO_EJBs group (select Add To -> EJB).
Create a new enterprise bean,but this time select Entity Bean with Bean-
managed Persistence (BMP) for the bean type.Name it Employee and add it
to the itso.wasdb.ejb package (Figure 154).
Figure 154.Adding a BMP Bean

Chapter 10.Enterprise JavaBeans
165
Add Properties and Methods
Now we add the properties to the bean.This is similar to the steps we have
done for the CMP bean,except that it is not possible to mark a property to be
container-managed,and there is also no way to mark a property to be the
primary key field (Figure 155).
For each column of the employee table,create a matching property with
getters and setters (no setter for the employee number).Add all getters and
setter to the remote interface (Add To -> EJB Remote Interface).
Figure 155.Properties in the Workspace View
The properties are listed in Table 3.
Table 3.Properties of the Employee Bean
Type Name Type Name
String empNo String firstName
String midInit String lastName
String workDept String phoneNo
java.util.Date hireDate String job
String sex java.util.Date birthDate
double salary double bonus
double comm short edLevel
itso.wasdb.ejb.
EmployeeStorage
storage (private,
no getter/setter)

166
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Provide the Employee Bean with Persistence Functionality
The only thing,apart fromadditional business functionality,is to provide the
bean with the functionality that it can persist itself to some database system.
To implement the persistence,we have to understand which methods of the
bean are invoked by the EJB container and what the purpose of each method
is.VisualAge for Java by default creates the following methods:

public void ejbLoad():This method is invoked by the container to
synchronize the bean according to the state represented in the database.
This means that the enterprise bean has to be able to refresh itself from
the table.

public void ejbStore():This is the counterpart of the ejbLoad method.
It synchronizes the database according to the state of the enterprise bean.
The bean has to able to write its state into the database.

public EmployeeKey ejbFindByPrimaryKey(EmployeeKey key):This
method retrieves an object with a given key from the database and
returns the key.

public EmployeeKey ejbCreate(EmployeeKey key):This method is
invoked when the client requests a new enterprise bean using the home
interface method create(...).This method is usually the place to validate
the argument given from the client and to insert a new row in the
database table that represents the contents of the enterprise bean.

public void ejbRemove():The container invokes this method to end the
life of this enterprise bean.This is the place to delete the record that
represents this bean fromthe database.

public void ejbActivate():This method is invoked when the container
picks an instance fromthe pool and assigns it to a specific enterprise bean
object.According to the EJB specification,this is not the place to read the
state of the bean froma database;this should be done in the ejbLoad
method.The ejbActivate method gives the bean the chance to use,
initialize,or acquire additional resources.This method is invoked by the
container prior to the invocation of ejbLoad.

public void ejbPassivate():This is the counterpart of the ejbActivate
method.It gives the enterprise bean the chance to release any resources
that were acquired during the ejbActivate method.This method is called
directly prior to the invocation of the ejbStore method.
There are some more methods,but at the moment those are of no interest.
We have to implement the ejbLoad,ejbStore,ejbFindByPrimaryKey,
ejbCreate,and ejbRemove methods.Because we do not use any additional
resources,we can skip the ejbActivate and ejbPassivate methods.

Chapter 10.Enterprise JavaBeans
167
To make the bean persistent,we use the IBMdata access beans.The usage is
capsulated in the EmployeeStorage class.This class is responsible to create a
new row,to retrieve a row,to update a row,or to delete a row from the
employee table of our database.Therefore,among the attributes that
represent the actual employee object,the EmployeeBean holds a private
instance of an EmployeeStorage class,named storage.
In the following code snippets,we only describe how to find and update an
entity bean and the respective database record.The actions to remove or
create a new entity bean are very similar.
Methods of the Entity Bean
The code for the ejbFindByPrimaryKey method (and for most methods of the
entity bean) is very short and simple.The main work is done in the
EmployeeStorage class.
In this method we create a new instance of the EmployeeStorage (if not
already there) and invoke the findEmployee method with the instance of the
enterprise bean and the primary key (Figure 156).
Figure 156.EmployeeBean ejbFindByPrimaryKey Method
The ejbStore method to store the entity bean and synchronize its contents
with the database is also rather simple.The only thing done here is to invoke
the update method of the storage class (Figure 157).
Figure 157.EmployeeBean ejbStore Method
public EmployeeKey ejbFindByPrimaryKey (EmployeeKey key)
throws javax.ejb.FinderException {
try {
if (storage == null)
storage = new itso.wasdb.ejb.EmployeeStorage();
this.empNo = key.primaryKey;
storage.findEmployee(this, key.primaryKey);
} catch(Exception e) {
throw new FinderException(e.toString());
}
return key;
}
public void ejbStore () throws java.rmi.RemoteException {
try {
storage.updateEmployee(this);
} catch (com.ibm.db.DataException e) {
throw new RemoteException(e.toString());
}
}

168
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 158 shows some of the other methods.
Figure 158.EmployeeBean Methods
public void ejbActivate() throws java.rmi.RemoteException {
try {
storage = new itso.wasdb.ejb.EmployeeStorage();
} catch (Exception e) {
throw new RemoteException(e.toString());
}
}
public void ejbPassivate() throws java.rmi.RemoteException {
try {
storage.close();
} catch (com.ibm.db.DataException e) {
throw new java.rmi.RemoteException(e.toString());
}
storage = null;
}
public void ejbLoad() throws java.rmi.RemoteException {
if (storage == null)
throw new RemoteException("Storage not initialized");
try {
storage.findEmployee(this, empNo);
} catch (com.ibm.db.DataException e) {
throw new RemoteException(e.toString());
}
}
public void ejbRemove() throws java.rmi.RemoteException {
try {
storage.deleteEmployee(empNo);
} catch (com.ibm.db.DataException e) {
throw new RemoteException(e.toString());
}
}
public EmployeeKey ejbCreate(EmployeeKey key, String firstName, String midInit,
String lastName, String workDept, String phoneNo, java.util.Date hireDate,
String job, short edLevel, String sex, java.util.Date birthDate, double salary,
double bonus, double comm) throws CreateException {
this.empNo = key.primaryKey;
this.firstName = firstName; this.lastName = lastName; this.midInit = midInit;
this.edLevel = edLevel; this.workDept = workDept; this.phoneNo = phoneNo;
this.hireDate = hireDate; this.job = job; this.sex = sex; this.comm = comm;
this.birthDate = birthDate; this.salary = salary; this.bonus = bonus;
try {
if (storage == null) storage = new itso.wasdb.ejb.EmployeeStorage();
storage.createEmployee(this);
} catch(Exception e) {
throw new CreateException(e.toString());
}
return key;
}

Chapter 10.Enterprise JavaBeans
169
The EmployeeStorage Class
Now we implement the EmployeeStorage class,and specifically,the two
methods of the storage class that were used in the two methods of the entity
bean,namely the findEmployee method and the updateEmployee method.
Class Declaration
We start with the declaration of the class (Figure 159).
Figure 159.EmployeeStorage Class Declaration
package itso.wasdb.ejb;
import java.sql.Connection;
import java.sql.Types;
import com.ibm.db.*;
import java.util.Date;
import java.util.Calendar;
import java.math.BigDecimal;
public class EmployeeStorage {
static int database = 1; // DB2
final static String URL = "jdbc:db2:itsowdb";
final static String DRIVERNAME = "COM.ibm.db2.jdbc.app.DB2Driver";
/*
static int database = 2; // Oracle
final static String URL = "jdbc:oracle:thin:@sonoma:1521:orcl";
final static String DRIVERNAME = "oracle.jdbc.driver.OracleDriver";
static int database = 3; // SQL Server
final static String URL = "jdbc:odbc:SqlServer";
final static String DRIVERNAME = "sun.jdbc.odbc.JdbcOdbcDriver";
*/
final static String USER = "ITSO";
final static String PASSWORD = "itso";
static String SELECT =
"SELECT EMPNO, FIRSTNME, MIDINIT, LASTNAME, WORKDEPT, PHONENO, " +
"JOB, SEX, SALARY, BONUS, COMM, HIREDATE, BIRTHDATE, EDLEVEL " +
"FROM ITSO.EMPLOYEE WHERE EMPLOYEE.EMPNO = :EMPNO";
final static String INSERT =
"INSERT INTO ITSO.EMPLOYEE VALUES (:EMPNO, :FIRSTNME, :MIDINIT, :LASTNAME,
:WORKDEPT, :PHONENO, :JOB, :SEX, :SALARY, :BONUS, :COMM, :HIREDATE, :BIRTHDATE,
:EDLEVEL)";
final static String DELETE =
"DELETE FROM ITSO.EMPLOYEE WHERE EMPNO = :EMPNO";
static StatementMetaData selectMetaData = null;
static StatementMetaData insertMetaData = null;
static StatementMetaData deleteMetaData = null;
static DatabaseConnection dbConn = null;
SelectResult resultSet = null;
}

170
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
The EmployeeStorage class defines the database that will be used,the user
ID,and the password.It also contains static references to the different SQL
statements:select,insert and delete.The actual statements will be created
later,we only define the string constants here.Notice that no update string is
defined.The update is done through the data access bean.We do not have to
define anything for this.At the very bottom we define a nonstatic
SelectResult.This result will be used to update a row fetched from the
database.
This example uses the DB2 database,but we could have used any other
database that provides the ITSOWDB database as well,although the code
differs a little bit (see “Different Mapping of SQL Types” on page 264).
Initializing the Meta Data
The next method we describe is the method that initializes the meta data for
the select statement.This method is static because the actual select
statement is the same and independent of specific instances of the entity
bean (Figure 160).
Figure 160.Initialize the Select Statement
static void initSelect() throws DataException {
selectMetaData = new StatementMetaData();
selectMetaData.setSQL(SELECT);
// Use the default mapping, Oracle seems to have problems otherwise
selectMetaData.addParameter("EMPNO", String.class, Types.CHAR);
// Next define all ouput parameter in the same way
selectMetaData.addColumn("EMPNO", String.class, Types.CHAR);
selectMetaData.addColumn("FIRSTNME", String.class, Types.VARCHAR);
selectMetaData.addColumn("MIDINIT", String.class, Types.CHAR);
selectMetaData.addColumn("LASTNAME", String.class, Types.VARCHAR);
selectMetaData.addColumn("WORKDEPT", String.class, Types.CHAR);
selectMetaData.addColumn("PHONENO", String.class, Types.CHAR);
selectMetaData.addColumn("JOB", String.class, Types.CHAR);
selectMetaData.addColumn("SEX", String.class, Types.CHAR);
if (database == 1) { //DB2
selectMetaData.addColumn("SALARY", Double.class, Types.DECIMAL);
selectMetaData.addColumn("BONUS", Double.class, Types.DECIMAL);
selectMetaData.addColumn("COMM", Double.class, Types.DECIMAL);
selectMetaData.addColumn("HIREDATE", Types.DATE, Types.DATE);
selectMetaData.addColumn("BIRTHDATE", Types.DATE, Types.DATE);
selectMetaData.addColumn("EDLEVEL", Short.class, Types.SMALLINT);
}
else if (database == 2) { //Oracle
selectMetaData.addColumn("SALARY", Types.DECIMAL, Types.DECIMAL);
...
}
// Define the table name which is used in the query
selectMetaData.addTable("ITSO.EMPLOYEE");
}

Chapter 10.Enterprise JavaBeans
171
The code of this method is specific for DB2,because Oracle and the SQL
Server differ in they way they map SQL types (see “Different Mapping of
SQL Types” on page 264).Due to that,we had to use different specifications
for the meta data for the different databases.The sample code of this class
contains the code for all three database systems.
Constructor
The constructor calls the initialize method to set up a database connection
and the meta data for the select,insert,and delete statements (Figure 161).
Figure 161.EmployeeBean ejbStore Method
The initDelete and initInsert methods are similar to the initSelect method.
Find an Employee
As we saw in Figure 156 on page 167 the ejbFindByPrimaryKey method used
the findEmployee method of the EmployeeStorage class.This method uses the
meta data for the select statement,which was initialized in the initSelect
method.
The findEmployee method is rather simple:A new SelectStatment is created
which uses the database connection and the prepared meta data to run a
query against the database.If a row was found,the employee bean is filled
with the column contents of that row (Figure 162).
Notice that the method does not close the resultSet,and that this resultSet is
an instance variable of the class.
public EmployeeStorage() throws DataException {
super();
initialize();
}
private void initialize() throws DataException{
// user code begin {1}
if (dbConn == null) {
initConnection();
initSelect();
initInsert();
initDelete();
}
// user code end
// user code begin {2}
// user code end
}

172
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 162.Find an Employee
Update an Employee
The updateEmployee method changes a record in the database based on the
data in the employee bean.This method uses the resultSet that was assigned
in the findEmployee method.This makes the update step quite simple
(Figure 163).
Based on the resultSet fromthe findEmployee method,the new values for the
row are set from the bean.Finally,the resultSet is updated.The resultSet is
not closed in the updateEmployee method;it is closed when the container
invokes the ejbPassivate method,which calls the close method of the
EmployeeStorage class.
public void findEmployee(EmployeeBean empl, String key)
throws java.rmi.RemoteException, DataException {
SelectStatement select = new SelectStatement();
select.setConnection(dbConn);
select.setMetaData(selectMetaData);
// Now set the Paramenter for the query
select.setParameter("EMPNO", key);
// and execute the query
select.execute();
// Retrieve the result set
resultSet = select.getResult();
if (resultSet.getNumRows() == 0)
throw new java.rmi.RemoteException("No Record found");
// Now get the resulting column by name
empl.setFirstName(resultSet.getColumnValueToString("FIRSTNME"));
empl.setMidInit(resultSet.getColumnValueToString("MIDINIT"));
empl.setLastName(resultSet.getColumnValueToString("LASTNAME"));
empl.setWorkDept(resultSet.getColumnValueToString("WORKDEPT"));
empl.setPhoneNo(resultSet.getColumnValueToString("PHONENO"));
empl.setJob(resultSet.getColumnValueToString("JOB"));
empl.setSex(resultSet.getColumnValueToString("SEX"));
empl.setHireDate((Date)resultSet.getColumnValue("HIREDATE"));
empl.setBirthDate((Date)resultSet.getColumnValue("BIRTHDATE"));
if (database == 1) { //DB2
empl.setSalary(((Double)resultSet.getColumnValue("SALARY")).doubleValue());
empl.setBonus(((Double)resultSet.getColumnValue("BONUS")).doubleValue());
empl.setComm(((Double)resultSet.getColumnValue("COMM")).doubleValue());
empl.setEdLevel(((Short)resultSet.getColumnValue("EDLEVEL")).shortValue());
}
else if (database == 2) { //Oracle
empl.setSalary(((BigDecimal)resultSet.getColumnValue("SALARY")).
doubleValue());
empl.setBonus(((BigDecimal)resultSet.getColumnValue("BONUS")).doubleValue());
empl.setComm(((BigDecimal)resultSet.getColumnValue("COMM")).doubleValue());
empl.setEdLevel(((BigDecimal)resultSet.getColumnValue("EDLEVEL")).
shortValue());
}
}

Chapter 10.Enterprise JavaBeans
173
The method uses a small date2Timestamp method that converts a
java.util.Date object to a java.sql.Timestamp object that is required by
Oracle.This code is specific to the Oracle database.
Figure 163.Update the Database
Delete an Employee
The deleteEmployee method removes the database record with a given key.
public void deleteEmployee(String key) throws DataException {
SQLStatement delete = new SQLStatement();
delete.setConnection(dbConn);
delete.setMetaData(deleteMetaData);
// Now set the Parameters for the delete
delete.setParameter("EMPNO", key);
delete.execute();
}
public void updateEmployee(EmployeeBean empl) throws DataException {
resultSet.setColumnValue("FIRSTNME", empl.getFirstName());
resultSet.setColumnValue("MIDINIT", empl.getMidInit());
resultSet.setColumnValue("LASTNAME", empl.getLastName());
resultSet.setColumnValue("WORKDEPT", empl.getWorkDept());
resultSet.setColumnValue("PHONENO", empl.getPhoneNo());
resultSet.setColumnValue("JOB", empl.getJob());
resultSet.setColumnValue("SEX", empl.getSex());
resultSet.setColumnValue("SALARY", new Double(empl.getSalary()));
resultSet.setColumnValue("BONUS", new Double(empl.getBonus()));
resultSet.setColumnValue("COMM", new Double(empl.getComm()));
if (database == 1) { //DB2
Calendar cal = Calendar.getInstance();
cal.setTime(empl.getHireDate());
java.sql.Date hireDate =
new java.sql.Date(cal.get(Calendar.YEAR) - 1900,
cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
resultSet.setColumnValue("HIREDATE", hireDate);
cal.setTime(empl.getBirthDate());
java.sql.Date birthDate =
new java.sql.Date(cal.get(Calendar.YEAR) - 1900,
cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
resultSet.setColumnValue("BIRTHDATE", birthDate);
resultSet.setColumnValue("EDLEVEL", new Short(empl.getEdLevel()));
} else if (database == 2) { //Oracle
resultSet.setColumnValue("HIREDATE",
date2Timestamp(empl.getHireDate()));
resultSet.setColumnValue("BIRTHDATE",
date2Timestamp(empl.getBirthDate()));
resultSet.setColumnValue("EDLEVEL",
new BigDecimal(Integer.toString(empl.getEdLevel())));
}
resultSet.updateRow();
}

174
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Testing the Employee Bean
Before testing the employee bean,you have to generate the home and helper
classes.Select EJBs -> Generate -> Deployed Code for the employee
enterprise bean.
Start the EJB Server
Add the ITSO_EJBs to the server (Add To -> Server Configuration) and start
the server (see “Starting the EJB Server in the Test Environment” on
page 144).Be sure to verify the properties of the server before starting it (see
Figure 134 on page 144).
Test Client
You can use the test client against the employee bean and retrieve an
employee and update its properties.Generate the test client code (select
Generate -> Test Client) and start it (see “Using the Test Client” on page 145).
Access the Employee Bean froma Servlet
For the employee enterprise bean we created a servlet (EmployeeEJBServlet)
that accesses the employee enterprise bean.
The structure of the servlet is exactly the same as the DepartmentEJBServlet
described in “Access the Department Bean from a Servlet” on page 153,
therefore we do not list the complete implementation here.
The servlet consists of the following methods:

init initializes the context and determines the home interface of the
employee bean.

doGet and doPost,which both call the performTask method.

performTask decides whether an employee should be retrieved (invoke
getEmployee) or updated (invoke updateEmployee).

findEmployee retrieves an employee bean based on the employee number
by using the home interface of the employee bean.

getEmployee uses findEmployee to retrieve the employee bean and display
it (partially) in an HTML form (Figure 164).

Chapter 10.Enterprise JavaBeans
175
Figure 164.Retrieve and Display the Employee Bean
public void getEmployee(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
response.setContentType("text/html");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// Get the output writer
java.io.PrintWriter out = response.getWriter();
String empNo = getParameter(request, "empNo");
out.println("<HTML><HEAD><TITLE>EmployeeEJBServlet </TITLE></HEAD><BODY>");
try {
Employee emp = findEmployee(empNo);
out.println("<TABLE>" +
"<FORM ACTION = \"/servlet/itso.wasdb.ejb.servlet.EmployeeEJBServlet\">");
out.println("<TR>");
out.println("<TH>Employee No");
out.println("<TH>Firstname");
out.println("<TH>Midinit");
out.println("<TH>Lastname");
out.println("<TH>Phone");
out.println("<TH>Salary");
out.println("<TR><TD>" + empNo);
out.println(" <TD><INPUT TYPE=TEXT NAME=firstName VALUE=\"" +
emp.getFirstName() + "\">");
out.println(" <TD><INPUT SIZE=1 TYPE=TEXT NAME=midinit VALUE=\"" +
emp.getMidInit() + "\">");
out.println(" <TD><INPUT TYPE=TEXT NAME=lastName VALUE=\"" +
emp.getLastName() + "\">");
out.println(" <TD><INPUT SIZE=4 TYPE=TEXT NAME=phoneNo VALUE=\"" +
emp.getPhoneNo() + "\">");
out.println(" <TD><INPUT SIZE=11 TYPE=TEXT NAME=salary VALUE=\"" +
emp.getSalary() + "\">");
out.println("<TR><TD><INPUT TYPE=HIDDEN NAME=command VALUE=updateEmployee>");
out.println("<TD><INPUT VALUE=Update TYPE=SUBMIT>");
out.println("</FORM></TABLE>");
HttpSession session = request.getSession(true);
session.putValue("itso.wasdb.ejb.servlet.EmployeeEJBServlet.employee", emp);
} catch (Exception e) {
out.println("<h2>Error: " + e + "</h2>");
e.printStackTrace(out);
}
out.println("</BODY></HTML>");
}
public Employee findEmployee(String key)
throws NamingException, ObjectNotFoundException, RemoteException, FinderException {
Employee emp = null;
emp = employeeHome.findByPrimaryKey(new EmployeeKey(key));
return emp;
}

176
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

updateEmployee sets the properties of the bean based on the input the
user has made in the HTML form(Figure 165).
Figure 165.Set the Properties of the Employee Bean
Testing the Servlet
Start the WebSphere Test Environment and make sure that the EJB Server
is running.Open a browser and enter the URL:
http://127.0.0.1:8080/servlet/itso.wasdb.ejb.servlet.EmployeeEJBServlet?
empNo=000010
The result of the employee servlet looks similar to the department servlet
(Figure 166).
Figure 166.EmployeeEJBServlet Output
public void updateEmployee(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
response.setContentType("text/html");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// Get the output writer
java.io.PrintWriter out = response.getWriter();
out.println("<HTML><HEAD><TITLE>Result</TITLE></HEAD><BODY>");
HttpSession session = request.getSession(false);
Employee emp = (Employee)
session.getValue("itso.wasdb.ejb.servlet.EmployeeEJBServlet.employee");
emp.setLastName(getParameter(request, "lastName"));
emp.setFirstName(getParameter(request, "firstName"));
emp.setPhoneNo(getParameter(request, "phoneNo"));
emp.setMidInit(getParameter(request, "midinit"));
emp.setSalary(Double.valueOf(getParameter(request, "salary")).doubleValue());
out.println("<H1>Update ok</H1>");
out.println("</BODY></HTML>");
}

Chapter 10.Enterprise JavaBeans
177
Deployment of the Employee Bean to WebSphere
The employee enterprise bean is deployed in the same way as the department
bean,but there are a few differences:

Export the JAR file (select Export -> EJS Jar).In the export dialog,click
on the Details button and add the EmployeeStorage class.It is required at
runtime to access the database.Name the file EmployeeEJS.jar.

In the WebSphere administration dialog for Enterprise Java Services,the
global settings are fine (Figure 140 on page 150),and we can use the same
ITSOContainer (Figure 141 on page 151).

Select the EmployeeEJS.jar file on the EJB Jar Files page (Figure 142 on
page 151) and click on Deploy.Select the ITSOContainer,and click on
Redeploy Existing in the warning dialog (Figure 143 on page 152).

Start the test client in VisualAge for Java and connect to the deployed
employee enterprise bean.

178
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

© Copyright IBM Corp.1999
179
11 WebSphere Studio
In this chapter we briefly introduce the WebSphere Studio tools.
We then describe in more detail the servlet generation wizards that allow
easy creation of servlets with relational database access.

180
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
WebSphere Studio Tools
WebSphere Studio can help a Web teambuild powerful,e-business Web sites
packed with features and interaction,without relying on the Common
Gateway Interface (CGI) and Active Server Pages (ASP).
WebSphere Studio combines easy-to-use wizards with site design and Java
development tools,simplifying and speeding the application development
process.It combines graphical development wizards with tools for Web site
design and Java development.
These wizards and tools simplify and speed the application development
process,and include:

Web Development Workbench—A Web site project organizer and
launch platform.

Servlet generation wizards—For building Java servlets to access
JDBC databases and JavaBean components.

VisualAge for Java,Professional Edition V2.0—IBM's Java
application development environment for building Java applications,
applets,servlets and JavaBean components.

NetObjects Fusion V3.0—Allows Web site developers to design and
produce an entire Web site,including individual pages and all links.It
features automated site building,automatic link management,remote
database access,and design and publishing capabilities.

NetObjects BeanBuilder V1.0—The visual authoring tool for
combining JavaBeans and Java applets,BeanBuilder allows individuals
overseeing the content of online business processes to create more
compelling,highly interactive Web sites.

NetObjects ScriptBuilder V3.0—Combines a text-based script editor
and development tools for creating and editing HTML,script,and
JavaServer Pages (JSP).
In this chapter we only cover the servlet wizards and show how to use them
to create simple database access servlets,the corresponding JSPs,and
JavaBeans.

Chapter 11.WebSphere Studio
181
Installation and Configuration
During our work we used WebSphere Studio 3.0 Beta 2 which was
downloadable from
http://www.software.ibm.com/webservers/
.
Although WebSphere Studio allow the specification of a class path,it seems
to be important to put any class files,for example,db2java.zip for DB2 or
classes111.zip for Oracle,in the system class path.Before you start
WebSphere Studio make sure the necessary zip/jar files are in the class path
(Control Panel -> System -> Environment -> CLASSPATH).
The next important point is to specify where WebSphere Studio stores the
published files.Usually this is the place where the WebSphere Application
Server resides.Unfortunately one cannot change the preferences without
having a project open.That is the reason why we come back to this point in
“Publish the Project” on page 194.
The examples described here are provided as archive files.To open them,
select File -> Open Archive from the menu and select the Itso.war file from
the Studio directory.Replace the content of the ITSO project with the extract
of the Itso.war file.
Project Setup
The development of Web sites is organized in projects in WebSphere Studio.
Naturally the first step is to create a new project that will be our playground.
When WebSphere Studio is started the first time,it asks whether it should
create a new project,use an existing project or exit.

We want to create a new project.So click Create new in the first dialog
(Figure 167).
Figure 167.Create New Project

Now specify the name ITSO for the project (Figure 168).

182
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 168.Specify Project Parameters

You should now have a project window (Figure 169).
Figure 169.WebSphere Studio Project Window

Chapter 11.WebSphere Studio
183
Create an SQL Statement and Servlet
To compose a database query and let WebSphere Studio create a servlet,we
use the SQL Wizard:

Select the project ITSO on the left side.

From the tool bar select SQL Wizard (Figure 170).
Figure 170.WebSphere Studio Tool Bar

In the SQL Wizard dialog you have to provide a name for the SQL
statement.In this example we want to select employees and some data
from their department from the database.The records will be selected by
partial employee last names.Therefore we name the new SQL statement
EmplDepByEmplName (Figure 171).
Figure 171.Specify a Name for the SQL Statement

184
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

The next step is to provide the wizard with the necessary database
connection information (Figure 172).As usual,we provide the following
for DB2:
• Driver:IBMDB2 UDB local (COM.ibm.db2.jdbc.app.DB2Driver)
• URL:jdbc:db2:itsowdb
• User ID and password:ITSO and itso (as in all examples)
To close the dialog,we click on Connect.
Figure 172.Specify the Connection Parameters
Note:For Oracle you would specify jdbc:oracle:thin:@localhost:1521:orcl
for the database URL and Oracle for the driver.For SQL Server you would
specify jdbc:odbc:SQLServer for the database URL and JDBC-ODBC
Bridge for the driver.

On the Tables page select the two tables,department and employee,of the
database (Figure 173) and click Next.

Chapter 11.WebSphere Studio
185
Figure 173.Select Tables from the Database

On the Join page,specify an inner join by selecting DEPNO in the
department table and WORKDEPT in the employee table and click on
Join (Figure 174).
Figure 174.Specify the Join

186
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

On the Columns page we select the columns to display (Figure 175).
During the time this book was written,the data access bean seemed to
have problems with date fields (see “Data Access Beans” on page 266).To
avoid trouble for the time being,it is better not to select a date field.
Figure 175.Specify the Columns to Display

On the Condition 1 page we specify the condition for the select statement
(Figure 176).
Select the EMPLOYEE table,the LASTNAME column and the contains
the character(s) operator.Now we have to enter a host variable for the
value of the condition.We click on the first text field and then on the
Parameter button.A dialog to name the parameter pops up and we enter
lastName.Parameter values are displayed in bold face in the text field
whereas fixed values are displated normal.
This specification generates a WHERE clause of the format:
LASTNAME LIKE '%'||?||'%'

Chapter 11.WebSphere Studio
187
Figure 176.Specify the Conditions

Because we do not want to sort the output we can now view the complete
SQL statement (Figure 177) and then generate the code by clicking on the
Finish button.
Figure 177.Completed SQL Statement
The WebSphere project window now contains a new file named
EmplDepByEmplName.sql.This file is the basis for the next step,that is,to
create a servlet that accesses the database using this SQL statement and to
create the corresponding JavaServer Pages that provide the input and output
for the servlet.
SELECT ITSO.EMPLOYEE.FIRSTNME, ITSO.EMPLOYEE.LASTNAME, ITSO.EMPLOYEE.PHONENO,
ITSO.EMPLOYEE.JOB, ITSO.EMPLOYEE.SALARY, ITSO.DEPARTMENT.DEPTNAME
FROM ITSO.DEPARTMENT, ITSO.EMPLOYEE
WHERE ( (ITSO.EMPLOYEE.WORKDEPT = ITSO.DEPARTMENT.DEPTNO) AND
(ITSO.EMPLOYEE.LASTNAME LIKE '%'||?||'%') )

188
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Create the Servlet and JSPs
The SQL query is ready and can be used in the Database Wizard to create a
servlet and a JSPO.Select the Database Wizard fromthe tool bar (Figure 170
on page 183) or from the menu.

We only have one query so far,EmplDepByEmplName.sql.It should be
selected by default,if not select it fromthe combobox (Figure 178).Click
Next.
Figure 178.Select the SQL Statement

The next step is to select what Web pages should be created (Figure 179).
Because WebSphere Studio 3 beta 2 and WebSphere Application server
2.0.2 are a little inconsistent at the moment,you should not use the last
option.For an explanation see “WebSphere Studio” on page 267.Click
Next.

Chapter 11.WebSphere Studio
189
Figure 179.Select the Generated Pages

Now we specify the parameters that will appear on the input page.We
need at least the lastName parameter (Figure 180).Click Next.
Figure 180.Specify Input Parameters

190
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

This next step is similar,but specifies the parameters for the resulting
output page (Figure 181).We select all the table columns,but are not
interested in the database driver,URL,SQL statement,and user ID,
although this might be useful for testing purposes.Click on Next.
Figure 181.Specify the Fields for the Result Page

In the next step you may add your own error message or you might even
specify your own error page.We do not have our own error handling for
now,and therefore we use the default.

The next step is to select which methods of the generated JavaBean have
to invoked and in what order.Because we only have one method,the
execute method,we do not change anything.The execute method does the
actual query.

Now we may specify whether the query is used in more than one output
page.If so,this step enables us to attach the bean to an HttpSession
object,which is then accessible by more than just the direct JSP output
page.
If multiple output pages are not selected,the resulting bean is just part of
the request object and is only accessible from the JSP result page of the
generated servlet.
For this simple example we do not select multiple output pages (Figure
182).

Chapter 11.WebSphere Studio
191
Figure 182.Specify to Use the Bean in a Session

The last step is to give more detailed information to the servlet generator
regarding the Java package (itso.wasdb.studio) and the name of the file.
To provide that information,click the Advanced button in the Finish
dialog and provide the package name and the prefix (Figure 183).
Figure 183.Provide Package Name and Class Name Prefix

The Finish dialog is shown in Figure 184.Click the Finish button.

192
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 184.Finish the Wizard
Project View after the Database Wizard
The project view should now show a few more files (Figure 185).
Figure 185.Project after Servlet Generation

Chapter 11.WebSphere Studio
193
Here is a description of the newly generated files:

EmplDepByEmplName.java and EmplDepByEmplName.class are
the Java source and class files for the bean that represents the actual
database query.The bean uses the IBMdata access bean and the IBM
connection manager to access the database.It offers several methods;the
most important one is the execute method,which performs the query.
Moreover,for each output column selected in Figure 181 on page 190,it
offers a getCOLUMN_NAME method,which returns the particular value
for that column of the query.

EmplDep.java and EmplDep.class are the Java source and class files
for the generated servlet.The servlet is derived from the PageListServlet
class.That means the pages that are called fromthis servlet are not
hard-coded,but specified in the servlet configuration file
EmplDepServlet.servlet.The most important method is the performTask
method.It reads parameters from the servlet configuration file (user ID,
password,database driver,URL).It also reads the input parameters
specified in Figure 180 on page 189 from the request,initializes the
database bean EmplDepByEmplName with the necessary parameters,
and invokes its execute method.Additionally,it adds the instance of the
EmplDepByEmplName bean to the request,then calls the output
JavaServer Page.

EmplDepServlet.servlet is an XML-based configuration file that sets the
rules for the servlet.It regulates which page is to call when the servlet
has successfully accessed the database,and also which page is to invoke
in case of an error or in case no data was found.Furthermore,it contains
key/value pairs for the user ID,the password,the database driver,and the
URL.

EmplDepInputPage.html is an HTML simple input page that enables
the user to enter a value for the lastName parameter and then invokes the
EmplDep servlet.

EmplDepResultPage.jsp is the JavaServer Page that is called fromthe
servlet after successfully accessing the database.To display the results it
uses the EmplDepByEmplName bean that was instantiated by the servlet
and given to this page through the request block.

EmplDepErrorPage.jsp is the JavaServer Page that is called in case of
an error.

194
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Publish the Project
The final step for this sample servlet is to deploy all the generated files to the
WebSphere application server,that is,to publish the files to the server.
We have to make sure that WebSphere Studio publishes the files to the
proper directories for the HTTP Server and WebSphere.If you have used a
default installation,WebSphere searches for servlets under:
c:\WebSphere\AppServer\servlets
and the IBMHTTP Server is looking for HTML files in:
c:\Program Files\IBM HTTP Server\htdocs
Publishing Configurations
There are three possible configurations between WebSphere Studio and
WebSphere Application Server:

In case WebSphere Studio and the WebSphere Application Server are on
the same machine,we check that the publishing properties of the server
are correct.We switch to the publishing view in the project window by
selecting View -> Publishing,or alternatively by clicking the appropriate
icon from the tool bar.Open the properties from the context menu of the
server (Figure 186).
Figure 186.Specify Publishing Options
In the properties dialog,click on the Default Publishing Target button
(Figure 187).Make sure the sure the targets match your configuration.

Chapter 11.WebSphere Studio
195
Figure 187.Publishing Server Properties

In case WebSphere Studio and the WebSphere Application Server are on
different Windows machines but the machine with the Application Server
is accessible as a shared drive,the publishing is almost the same,except
for the fact that the server name is not localhost.You may specify this also
in the server properties dialog (Figure 187).

In case the WebSphere Application Server is on a machine that is not
accessible as a shared drive,use an FTP upload mechanismto publish the
files to the application server.Again the server properties dialog (Figure
187) is the place to detail the FTP parameters.
Once the publishing configuration is specified,we can publish the files to the
Application Server and the HTTP Server.From the context menu of the
server
http://myserver
select Publish this Server.In the first dialog that is
displayed you may specify additional options.For our example,no changes
are required.In the next dialog we can select the files to be published;the
default selection is correct for this step as well.

196
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Assembly Stages
WebSphere Studio has two predefined assembly stages,test and production.
Select View -> Assembly Stage to switch between the two stages.
You can define the test assembly stage for local publishing (localhost) and the
production assembly stage for publishing to another Web server.Set up the
properties and publishing targets for both assembly stages.
Publishing Targets
The publishing target dialog defines the two directories for publishing
HTML/JSP files and servlet classes (Figure 188).
Figure 188.Publishing Targets

Chapter 11.WebSphere Studio
197
Testing the Generated Servlet
Testing is easy and straightforward once the project has been published to
WebSphere.From the context menu of the EmplDepInputPage.html file
select Preview -> netscape or iexplore depending on the favorite browser
(Figure 189).
Figure 189.Select Preview from the Context Menu
The HTML input page is plain but functional (Figure 190).
Figure 190.Generated HTML Input Page

198
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
The HTML output page that is generated by the JSP displays the result of
the simple servlet (Figure 191).
Figure 191.Generated HTML Output Page
Enhanced Insert Example
In the second example we will develop a simple servlet that inserts a new
employee in the database.With the help of the Studio Wizards this is also a
very simple process.In the second step we will enrich the input page of the
insert servlet so that it already shows all available department numbers in a
combobox.Because the department number is a foreign key in the employee
table,this assures that only existing values will be inserted.
Create an SQL Statement
The first step is to create an SQL statement with the SQL Wizard:

Name the SQL statement InsertEmployee.

Provide the connection parameters.

Select the employee table and make sure you select Insert for the SQL
type (Figure 192).

Chapter 11.WebSphere Studio
199
Figure 192.Creating an Insert Statement

Select the columns that are inserted.The columns with the blue
background are not null columns in the table,therefore they are
mandatory (Figure 193).In addition to the mandatory fields we select the
PHONENO and the WORKDEPT field.
Figure 193.Select the Input Fields

200
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

The resulting SQL statement is shown in Figure 194.
Figure 194.Generated SQL Insert Statement
Create a Servlet
With the SQL statement specified,the next step is to create a servlet based
on that statement.We use the Database Wizard:

Select the InsertEmplyee.sql statement that was just created.

Create an input and result page.

Select the input fields empNo,firstName,midInit,lastName,edLevel,
phoneNo,and workDept.You might also want to sort them using the up
and down arrows on the right side.

Select columns for the results page,for example firstName and lastName.

Select the execute Method in the next step.

We do not use the bean in more than one page,so no change is necessary
in the next dialog.

Finally specify the advanced parameters,that is,the package name
itso.wasdb.studio and a meaningful prefix,for example InsertEmpl.

Click Finish to generate the code.

Publish the project.
Viewing the Result
The input page is shown in Figure 195.Notice that the user is allowed to
specify any working department number regardless of whether the
department exists or not.
It would be nice to enhance this process to allow the user to select only
existing department numbers.This is what we will do next.
INSERT INTO ITSO.EMPLOYEE
( EMPNO, FIRSTNME, MIDINIT, LASTNAME, WORKDEPT, PHONENO, EDLEVEL )
VALUES
( ?, ?, ?, ?, ?, ?, ? )

Chapter 11.WebSphere Studio
201
Figure 195.Insert a New Employee
Determine all Available Department Numbers
To enhance the input formwith a combobox that displays all the available
department numbers,we create an SQL statement to retrieve them.
This step is quite easy with the SQL Wizard:

Create a new SQL statement named AllDepNumbers.

Provide the connection parameters.

Select the department table.

Select the DEPTNO column to include (Figure 196).

That is all.Click Finish.

202
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 196.Select the DEPNO Column
Based on this statement,we create a servlet using the Database Wizard.

Although we actually do not need input and output pages,we will create
an output page because the PageListServlet class apparently requires it.
Later,when the servlet is created,we will modify the source slightly,so
that no output page is called and we can also delete the output page.

Continue to the session step.Give the bean a meaningful name,for
example,change it from BeanAlias to depNumbers.

Continue to the end;no changes should be necessary.Specify as usual the
package itso.wasdb.studio and the prefix AllDepNumbers,and finish the
wizard.
Invoke the Servlet froma JSP
We want to use the servlet we just created as an input for the page where we
specify the new employee,that is in the InsertEmplInputPage.html.We use
the HTML page as a base and modify it and create a JSP file.
Open the page by double clicking on it.The WebSphere Studio Page Designer
is started (Figure 197).This is the HTML/JSP editor of WebSphere Studio.
We will not provide an introduction in the usage of this editor,but you can
use it to enhance the generated input and output pages.
We will use the editor to add the invocation of a servlet,the AllDepNumbers
servlet,and at the same time add the bean that is created by the
AllDepNumbers servlet.This process makes it possible to use the bean to
create a combobox that holds all the possible department numbers retrieved
by the database query.

Chapter 11.WebSphere Studio
203
Figure 197.WebSphere Page Designer

Start by adding a servlet invocation to the top of the page.Use the menu
Insert -> Servlet.In the dialog specify the name of the servlet to be
invoked,in our case the AllDepNumbersServlet (Figure 198).Enter
itso.wasdb.studio.AllDepNumbersServlet as the code attribute.
Figure 198.Specify the Servlet to be Invoked

204
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
This specification generates the code
<SERVLET code="itso.wasdb.studio.AllDepNumbersServlet">
</SERVLET>

The next step is to insert the bean,also at the top.Select Insert -> JSP
Tags -> Bean Tag from the menu.The name of the bean is depNumbers
(the name we gave a few steps earlier) and the type of the bean is
itso.wasdb.studio.AllDepNumbersBean (Figure 199).
Figure 199.Specify Bean Parameters
This specification generates the code
<BEAN name="depNumbers" type="itso.wasdb.studio.AllDepNumbersBean">
</BEAN>

Delete the last text field (the working department) and substitute it with
a combobox.The result page should now contain one servlet tag,one bean
tag,and a combobox instead of a text field (Figure 200).

Chapter 11.WebSphere Studio
205
Figure 200.Resulting Page

Next,you have to provide the possible values for the combobox that are
dynamically retrieved by the AllDepNumbers servlet.
Double-click on the combobox and specify the name workDept in the
dialog.This is the name the text field had when we specified it with the
database wizard (Figure 193 on page 199).

In the same dialog switch to the Dynamic pane.This is the place to
provide the combobox with possible values.
To retrieve the values dynamically,we select the Loop Property and click
on Browse.Another dialog is displayed where we can browse the available
properties of all the beans accessible from this page (Figure 201).Browse
the depNumbers bean and select the ITSO_DEPARTMENT_DEPTNO
property.

On the same page specify the ItemProperty that is displayed in the
combobox.In this case it is the same as the loop property (Figure 201).
Select Specify as Property and browse once more to the
ITSO_DEPARTMENT_DEPTNO property.If we had specified more than
one output field in the Database Wizard (for example,the department
name),such fields could also be used here.
Servlet and Bean
Combobox

206
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 201.Selecting the Loop Property

Switch to the Html Source view and look at the generated code for this
table cell (Figure 202).
The first cell of the table row contains the word workDept.The next cell
contains the dynamic content that is evaluated and replaced by the
WebSphere Application Server at runtime.Basically,we specified which
property is iterated and which property is displayed as a value.In our
case,both are the same property.

Chapter 11.WebSphere Studio
207
Figure 202.Generated Code for Dynamic Item Values
Everything is ready.You might want to enhance the look of the page.Also,
you might want to inspect the generated JSP and HTML code.
Save the Modified HTML Page as a JSP
Because we imbedded Java logic into the HTML page,we have to execute the
page as a JSP.
Save the page (File -> Save As) as InsertEmplInputPage.jsp and select Add to
WebSphere Studio in the save dialog.The Beta 2 product complains with an
error message that this is not possible and then asks you to save again.Just
save again,and although WebSphere Studio complained,everything works
fine.
Modify the Generated Servlet
The final step is to modify the generated source of the AllDepNumbers
servlet.The servlet is generated by WebSphere Studio and by default it is
derived from the PageListServlet.
A PageListServlet is configurable by the matching servlet XML configuration
file (AllDepNumbersServlet.servlet).This configuration file contains some
parameters for the servlet,such as the database user ID,password,the
<TR><TD>workDept</TD><TD>
<!--METADATA type="DynamicData" startspan
<SELECT dynamicelement loopproperty="depNumbers.ITSO_DEPARTMENT_DEPTNO()"
itemproperty="depNumbers.ITSO_DEPARTMENT_DEPTNO()" name="workDept" </SELECT>
--><%
try {
java.lang.String _p0 = depNumbers.getITSO_DEPARTMENT_DEPTNO(0);
%><SELECT name="workDept"><%
for (int _i0 = 0; ; ) { %>
<OPTION><%= _p0 %></OPTION><%
_i0++;
try {
_p0 = depNumbers.getITSO_DEPARTMENT_DEPTNO(_i0);
}
catch (java.lang.ArrayIndexOutOfBoundsException _e0) {
break;
}
} %>
</SELECT><%
} catch (java.lang.ArrayIndexOutOfBoundsException _e0) {
} %><!--METADATA type="DynamicData" endspan-->
</TD></TR>

208
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
driver,and the URL.It also specifies which output page is the default result
page for the servlet,which page is the error page,and all the other possible
output pages.
In our case,we do not want an output page called by the servlet.For this
reason,we change the source code of the servlet:

Open the Java source file of the servlet,that is,AllDepNumbers.java.It
has several methods;the most important one is the performTask method.
At the very end of this method,you will find the following line:
callPage(getPageNameFromRequest(request), request, response);
Comment it out.The effect is simple.The output JSP page we had
configured is not called anymore.
This is exactly what we would like to achieve,because the servlet is
already called froma JSP and the output of the servlet is the depNumbers
bean,which is included in the output JSP.

Save the file and select Compile from the context menu in WebSphere
Studio.

Finally select the server and publish the project to the server.
Test the JSP and the Servlet in WebSphere
After publishing the code to the WebSpere Application Server we can test the
JSP and the servlet.
Select Preview -> iexplore or navigator from the context menu of the
InsertEmplInputPage.jsp from the Publish view.As you can see in the
browser,the working department now contains a combobox with all available
department numbers (Figure 203).
Problemwith Servlet Configuration
The JSP calls the AllDepNumbers servlet.When loaded,the code looks for
the servlet configuration file,AllDepNumbers.servlet,but the generated file
is named AllDepNumbersServlet.servlet.
We copied the AllDepNumbersServlet.servlet file as AllDepNumbers.servlet
and deployed that copy into the same directory.
Note:We also tried to call the servlet by the name AllDepNumbersServlet
(Figure 198 on page 203),but then the load of the servlet fromthe JSP
servlet failed completely.

Chapter 11.WebSphere Studio
209
Figure 203.Enhanced Insert Servlet Input Page with Combobox

210
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Debugging the Servlets in VisualAge for Java
Here are the steps required to debug the generated servlets in VisualAge for
Java.

As a prerequisite,the ITSOWDB database must be available and
accessible through the same JDBC drivers,URL,user ID,and password
as specifed for WebSphere.

Import the six servlet source and bean files (.java) into the ITSO SG24
5471 project in VisualAge for Java.The itso.wasdb.studio package is
created.

Error messages are reported for each servlet.
import comm.ibm.webtools.runtime.*; <=== missing package
This package is not required,therefore you can delete the line from each
servlet.

Copy the HTML and JSP files to the proper HTML directory.If you
performed a local publishing,the files should be there already.

Copy the servlet configuration files (.servlet) into the resources directory:
c:\IBMVjava\ide\project_resources\ITSO SG24 5471\itso\wasdb\studio
Make sure that you have the AllDepNumbers.servlet file (copy of
AllDepNumbersServlet.servlet).

Start the WebSphere Test Environment and open a browser with the
URL:
http://127.0.0.1:8080/EmplDeptInputPage.html
http://127.0.0.1:8080/InsertEmplnputPage.jsp

You can set breakpoints in the servlet code to activate the debugger.

Chapter 11.WebSphere Studio
211
Hints and Tips for WebSphere Studio
The beta code of WebSphere Studio has some problems that may disturb
smooth operation.Here are a few pointers on how to deal with problems.
Synchronization between Development and Publishing View
It seems that sometimes files of the development view (left side of the project
view) do not show up in the publishing view (right side).The Sync left view
and right view push button in the tool bar does not work (Figure 204).
You can force a file to the publishing view by selecting it,open its properties,
and on the Publishing page deselect the Set publishable checkbox.Click OK.
Then open the properties again,select the checkbox,and click OK.This
copies the file to the publishing view.
Checkout
Files that have been edited have the checkout flag (a red check mark).You
can select the file and select Check In from the context menu.
In the directory structure,checked out files are in the directory:
c:\WebSphere\StudioBeta\check_out\..projectname...
Files that are checked in are marked read-only in the directory:
c:\WebSphere\StudioBeta\projects\..projectname...
Publishing Warnings About Old Class Files
When publishing,we got a warning message that the class file (.class) was
older than the source file (.java).In such situations you can check out the
source and class files and compile the source file (select Compile fromthe
context menu).After that check both files in.
Mismatch between Servlet and Servlet Configuration
For a servlet called from a JSP,the naming of the servlet (.java) and servlet
configuration file (.servlet) does not work.See “Problemwith Servlet
Configuration” on page 208 for a circumvention.

212
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Project View
Figure 204 shows our project view after completing all the samples.
Figure 204.Complete Project View

© Copyright IBM Corp.1999
213
12 WebSphere User
Profile
In this chapter we describe the user profile function of WebSphere.We also
develop a sample servlet utilizing the user profile function.

214
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Description
The WebSphere Application Server includes classes in the
com.ibm.servlet.personalization.userprofile package that make it easier to
maintain persistent information about your Web site visitors and use that
information to customize your Web pages.
At the time of the release of the WebSphere,the classes of the package
com.ibm.servlet.personalization.userprofile are considered a reference
implementation,because issues,such as database schema and runtime
environments,can vary greatly from application to application.Given these
considerations,WebSphere includes the source of the UserProfile class and
an example of how to extend the class.Both source files are in the
WebSphere\AppServer\samples\userprofile
directory.The only coupling between
the UserProfile class and the rest of WebSphere is that the UserProfile class
and IBM's session implementation (IBMSessionData) have a user name field
that can be used to create some synergy between the two classes.
The UserProfile class includes data members for a visitor's complete name,
postal and e-mail addresses,and telephone numbers,and has fields to store
language of choice,employment,and user-defined group information.In
addition,it has a generic message,a shopping cart,and a clipboard that is a
Java hash table.This allows you to easily incorporate other objects of your
choice into these data members and handle themas part of the UserProfile
class.Because these objects are persistent across successive instantiations of
the UserProfile,they must be serializable.If you use a JDBC database,the
objects will be stored in the database.Otherwise,they will be stored as files.
In the WebSphere Application Server Version 2.0,the UserProfile class uses
the database connection manager to maintain JDBC connections.
Configuration
To set up the user profile function,you should use WebSphere’s
administration dialogs.Once you have started the administration program
(see “WebSphere Configuration” on page 6),select Setup -> UserProfile.
Enable Page
On the Enable page select yes for Using UserProfile?(Figure 205).As you can
see the UserProfile class itself can be modified.So if your application has
some specific needs where it requires modifying the code,you can do so and
can place the modified class name in here.

Chapter 12.WebSphere User Profile
215
Figure 205.Enabling User Profile
Database Page
The database information required here is based on the URL used for the
JDBC connection.Every database server has its own URL and each differs
from the other.The way in which the UserProfile uses the information is as
follows:
URL:jdbc:"+ Config.DBUSED +":"+ Config.DB_NAME;
Table Name:Config.DB_OWNER +"."+ Config.USER_TABLE
Figure 206 shows the specification for DB2,Figure 207 for Oracle,and Figure
208 for SQL Server.

216
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
DB2
Figure 206.UserProfile Database Setup for DB2
Oracle
Figure 207.UserProfile Database Setup for Oracle

Chapter 12.WebSphere User Profile
217
SQL Server
Figure 208.UserProfile Database Setup for SQL Server
Connection Manager Page
On the ConnMgr (connection manager) page you select the pool with respect
to the database you have selected on the Database page (Figure 209).
Figure 209.UserProfile’s Connection Pool Selection

218
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Click on Save and log off.The changes for the user profile only take effect
when you stop the Web server and WebSphere.Then restart the Web server.
The UserProfile table is created the first time an attempt is made to access
the user profile function.
User Profile Create,Update and Retrieve
In this section we briefly describe howto create,update,and retrieve the user
profile fromthe database to provide you with some understanding on howthe
user profile works.
Create
Creating the user profile using new UserProfile() does not result in writing
the user profile into the database.To create an entry in the database table
use insertToDatabase(UserProfile userProf).When using the
insertToDatabase method,the UserProfile passed to it must already have the
UserName property set up,because there is no method for setting the
UserName.Therefore you cannot call new UserProfile() and then pass the
newly created class to the insertToDatabase method.
The way to create a UserProfile would be using the static method
addUserProfile.This method has three variations,which are:

UserProfile addUserProfile(String userName)
In this method the userName is directly passed,and the returned
UserProfile does contain the userName.You can set the rest of the
parameters using the setter methods available in the UserProfile class.

UserProfile addUserProfile(HttpSession httpSession)
In this method the session object is expected to have a parameter called
userName of type String.This can be done with a method call such as
httpSession.putValue("userName",userName).Once the userName is
extracted fromthe session,addUserProfile(userName) is called to create a
UserProfile.From this point on,other parameters are extracted fromthe
session,if any of themis set,and the appropriate setter method is called.
Note:The extraction of parameters fromthe session and calling the setter
methods is done behind the scene.

UserProfile addUserProfile(HashTable clipboard)
In this method the hash table object is expected to have a parameter
called userName of type String.This can be done with a method call such

Chapter 12.WebSphere User Profile
219
as clipboard.put("userName",userName).Once the userName is extracted,
the addUserProfile(userName) is called.After that point,other
parameters are extracted and the appropriate setter method is called.
Note:The extraction of parameters fromthe hash table and calling the
setter methods is done behind the scene.
The second and third methods are merely wrappers functions that call the
first method.
Update
You can update all of the fields except the userName in a UserProfile object:

void updateUserProfile(HttpSession sess)
In this method the session object is expected to have a parameter called
userName of type String.

void updateUserProfile(Hashtable clipboard)
In this method the hash table object is expected to have a parameter
called userName of type String.
Retrieve
There are several ways to retrieve a UserProfile object:

UserProfile retrieveUserProfileByUserName(String userName)
Based on the userName the UserProfile is returned.

UserProfile retrieveUserProfile(String type,String key)
type is the column name in the UserProfile table and key is the value of
the column.

Enumeration retrieveUserProfiles(String type,String key,Statement s)
In this method a statement is assembled based on the type and method
returned.The results are 0 or multiple user profiles returned through an
Enumeration object.
There are several other methods in the UserProfile class that can be used to
retrieve the UserProfile based on different properties,for example,
retrieveUserProfilesByLanguage.

220
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
UserProfile Example
In this example we want to demonstrate how the UserProfile is created and
accessed.Also the usage of a shopping cart in the UserProfile context is
discussed.
Classes and Methods
In this example we have employed JavaBean,JSP,and Servlet
methodologies.The package itso.wasdb.userprofile contains the classes used
in this example.The following is the list of classes (Figure 210).
Figure 210.itsowasdb.userprofile Class Listing
Item
Item is a JavaBean class that is passed as a shoppingCart object to a
UserProfile.The properties are:
String item; /* name of the item */
String price; /* price of the item */
An ItemBeanInfo class is generated by VisualAge for Java when the Item
class is created.
UserCart
UserCart is a JavaBean class that contains the basic information used to
create a UserProfile.The properties are:
String language;
String firstName;
String midName;
String lastName;
String userName;
A UserCartBeanInfo class is generated by VisualAge for Java when the
UserCart class is created.

Chapter 12.WebSphere User Profile
221
Note:The two classes Itemand UserCart are simple Java Bean classes with
getter and setter methods.We will not describe these classes in detail
because they do not need further explanation.
UserProfileCartServlet
The UserProfileCartServlet class handles all of the incoming requests and
outgoing responses.It is within this class where all of the transactions take
place.Figure 211 shows the methods of the UserProfileCartServlet class.
Figure 211.UserProfileCartServlet Methods Listing
The UserProfileCartServlet class manages the access to the user profile.It
creates a user profile if it does not exist and displays the contents for an
existing user profile.The content of the shopping cart of the profile is
modifiable.Our class is a subclass of PageListServlet,which is a subclass of
HttpServlet that can invoke JSPs (Figure 212).
Figure 212.UserProfileCartServlet Class Declaration
The addUserShoppingCart method adds the shopping cart object into the
UserProfile (Figure 213).
import javax.servlet.*;
import javax.servlet.http.*;
import com.ibm.servlet.personalization.userprofile.*;
public class UserProfileCartServlet extends com.ibm.servlet.PageListServlet
implements java.io.Serializable
{
private UserProfile profile = null;
}

222
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 213.UserProfileCartServlet addUserShoppingCart Method
Note:In our simple example,the shopping cart contains only one item.To
make this a true shopping cart,you can pass a Vector that contains multiple
objects.Any collection type of object can be placed here as long as all of the
collection objects and the objects inside of them are Serializable.
The createUserProfile method checks to see if the profile already exists under
the specified UserName.If not,it will create and update the UserProfile
fields.If there is a UserProfile under the same UserName it throws an
exception (Figure 214).
Figure 214.UserProfileCartServlet.createUserProfile
The doGet method processes incoming HTTP GET requests (Figure 215).
Figure 215.UserProfileCartServlet doGet Method
public void addUserShoppingCart(Item item)
throws java.io.NotSerializableException {
profile.addShoppingCart(item);
}
public void createUserProfile(UserCart userCart) throws java.lang.Exception {
profile = UserProfile.addUserProfile(userCart.getUserName());
if( profile != null) {
profile.setFirstName(userCart.getFirstName());
profile.setLastName(userCart.getLastName());
profile.setLanguage(userCart.getLanguage());
}
else {
throw new java.lang.Exception("UserName: " + userCart.getUserName()
+ " already exists");
}
}
public void doGet(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response) {
performTask(request, response);
}

Chapter 12.WebSphere User Profile
223
The doPost method processes incoming HTTP GET requests (Figure 216).
Figure 216.UserProfileCartServlet doPost Method
The getParameter method reads a parameter fromthe HTML or JSP file and
returns it as a string (Figure 217).
Figure 217.UserProfileCartServlet getParameter Method
public void doPost(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response) {
performTask(request, response);
}
public String getParameter(HttpServletRequest request,
String parameterName,
boolean checkRequestParameters,
boolean checkInitParameters,
boolean isParameterRequired,
defaultValue) throws java.lang.Exception {
java.lang.String[] parameterValues = null;
java.lang.String paramValue = null;
// Get the parameter from the request object if necessary.
if (checkRequestParameters) {
parameterValues = request.getParameterValues(parameterName);
if (parameterValues != null)
paramValue = parameterValues[0];
}
// Get the parameter from the servlet init parameters if
// it was not in the request parameter.
if ( (checkInitParameters) && (paramValue == null) )
paramValue = getServletConfig().getInitParameter(parameterName);
// Throw exception if the parameter was not found but was required
if ( (isParameterRequired) && (paramValue == null) )
throw new Exception("Parameter" + parameterName +
"was not specified.");
// Set the return to the default value if the parameter was not found
if (paramValue == null) paramValue = defaultValue;
return paramValue;
}

224
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
The itemCreateToOutputPage method takes care of creating an item and
adding it to the UserProfiles shopping cart.Once the update is done
successfully,the output page (a JSP) is called to display UserProfile and its
shopping cart content (Figure 218).This method is invoked when the Submit
button fromthe ItemCreate page (Figure 226 on page 229) is clicked.
Figure 218.UserProfileCartServlet itemCreateToOutputPage Method
The performTask method processes incoming requests for information.It
checks from which page the request has been made and,based on that
information,calls the appropriate method (Figure 219).In case any of the
methods called throw an exception,the handleError method is called,which
in turn launches the error output page (Figure 233 on page 234).
public void itemCreateToOutputPage(
HttpServletRequest request,
HttpServletResponse response,
UserCart userCart ,
Item itemInfo) throws java.lang.Exception {
UserCart tempCart = null;
javax.servlet.http.HttpSession session = null;
session = request.getSession(true);
itemInfo.setItem(getParameter(request,"item", true, true, true, null));
itemInfo.setPrice(getParameter(request,"price", true, true, true, null));
tempCart = (UserCart)session.getValue("userCart");
userCart.setFirstName(tempCart.getFirstName());
userCart.setLastName(tempCart.getLastName());
userCart.setUserName(tempCart.getUserName());
userCart.setLanguage(tempCart.getLanguage());
addUserShoppingCart(itemInfo);
//default output page
callPage(getPageNameFromRequest(request), request, response);
}

Chapter 12.WebSphere User Profile
225
Figure 219.UserProfileCartServlet performTask Method
public void performTask(HttpServletRequest request,
HttpServletResponse response) {
try
{
// instantiate the bean and store it in the request so it can be accessed by
// the called page
UserCart userProfileCart = null;
Item itemInfo = null;
String aPageName = new String();
itemInfo = (Item) java.beans.Beans.instantiate(getClass().getClassLoader(),
"itso.wasdb.userprofile.Item");
userProfileCart = (UserCart)
java.beans.Beans.instantiate(getClass().getClassLoader(),
"itso.wasdb.userprofile.UserCart");
// store the bean in the request so it can be accessed
// by pages which are accessed with callPage()
setRequestAttribute("userProfileCart", userProfileCart, request);
setRequestAttribute("itemInfo", itemInfo, request);
aPageName = getParameter(request, "PageName", true, true, true, null);
if(aPageName.compareTo("ItemCreate") == 0) {
itemCreateToOutputPage(request, response, userProfileCart, itemInfo);
}
else if(aPageName.compareTo("UserOutput") == 0) {
callPage("ItemCreate", request, response);
}
else if(aPageName.compareTo("UserCreate") == 0) {
userCreateToOutputPage(request, response,userProfileCart);
}
else { //request is from "Login" page
userProfileCart.setUserName(getParameter(request, "userName", true, true,
true, null));
processLogin(request, response, userProfileCart, itemInfo);
}
}
catch (Exception theException)
{
handleError(request, response, theException);
try{
java.io.PrintWriter out = response.getWriter();
out.println("_____________________________________________________<br>");
out.println("Exception occurred: " + theException + "<br>");
out.println("___________________________________________________<p><br>");
theException. printStackTrace(out);
}
catch(java.io.IOException e){
System.out.println("IOException occurred: " + e);
}//endcatch
}//endcatch
}

226
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
The processLogin method (Figure 220) is called fromthe Login Page (Figure
229 on page 231).Depending on whether the UserProfile with the UserName
already exists or not,it calls the UserOutput page (Figure 231 on page 233)
or UserCreate page (Figure 230 on page 232).
Figure 220.UserProfileCartServlet processLogin Method
The retrieveUserProfile method attempts to retrieve a UserProfile based on
the userName passed.If the UserProfile does not exist,it returns false,
otherwise it returns true (Figure 221).
public void processLogin(HttpServletRequest request,
HttpServletResponse response,
UserCart userCart ,
Item itemInfo) throws java.lang.Exception{

boolean isFound = false;
Item tempItem = null;
javax.servlet.http.HttpSession session = request.getSession(true);
isFound = retrieveUserProfile(userCart.getUserName());
if( isFound == true ) { // if the UserProfile already exists
userCart.setFirstName(profile.getFirstName());
userCart.setLastName(profile.getLastName());
userCart.setUserName(profile.getUserName());
userCart.setLanguage(profile.getLanguage());
session.putValue("userCart",userCart);
tempItem = (Item)profile.retrieveShoppingCart();
if(tempItem != null) { //make sure there is an object in shopping cart
itemInfo.setItem(tempItem.getItem());
itemInfo.setPrice(tempItem.getPrice());
}
//default output page
callPage(getPageNameFromRequest(request), request, response);
}//endif
else { // UserCreate page is called if it's a new user
callPage("UserCreate", request, response);
}//endelse
}

Chapter 12.WebSphere User Profile
227
Figure 221.UserProfileCartServlet retrieveUserProfile Method
The userCreateToOutputPage method (Figure 222) is called from the
UserCreate page (Figure 230 on page 232),it takes the parameters passed for
the UserProfile fields and then creates the UserProfile.It also stores the new
information of the UserProfile inside an HttpSession object so it can be
accessed fromother pages (see the itemCreateToOutputPage method in
Figure 218 on page 224).
Figure 222.UserProfileCartServlet userCreateToOutputPage Method
public boolean retrieveUserProfile(String userName) {
boolean isFound = true;
profile = UserProfile.retrieveUserProfileByUserName(userName);
if( profile == null) {
isFound = false;
}
return isFound;
}
public void userCreateToOutputPage( HttpServletRequest request,
HttpServletResponse response,
UserCart userCart) throws java.lang.Exception {
//create if it's not already created
javax.servlet.http.HttpSession session = request.getSession(true);
userCart.setUserName(
getParameter(request, "userName", true, true, true, null));
userCart.setFirstName(
getParameter(request, "firstName", true, true, true, null));
userCart.setLastName(
getParameter(request, "lastName", true, true, true, null));
userCart.setLanguage(
getParameter(request, "language", true, true, true, null));
session.putValue("userCart",userCart);
createUserProfile(userCart);
//default output page
callPage(getPageNameFromRequest(request), request, response);
}

228
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Output Result
In this section we will step through a flow of HTML pages to demonstrate the
creation or retrieval of a user profile starting from the login page.
The Login page (Figure 223) is the first page displayed.After entering the
user name,control passes to the next page,depending on whether a user
profile for the user name exists or not.
Figure 223.UserProfile Login Page
If the user name entered in the Login page does not exist in the UserProfile
table,the UserCreate page (Figure 224) is called.This page prompts you to
create a new UserProfile.
Figure 224.UserProfile UserCreate Page

Chapter 12.WebSphere User Profile
229
Once the user profile is created,the result is displayed in the default output
page (Figure 225).
Figure 225.UserProfile Default OutputPage After User Registration
You can create a new item to be added to the shopping cart part of the
UserProfile.The shopping cart only takes one object.A new object always
overwrites the old one.This is done using the ItemCreate page (Figure 226).
Figure 226.UserProfile ItemCreate Page
After submitting the item,the default output page with the newly created
item is displayed (Figure 227).

230
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 227.UserProfile Default Output Page After Item Creation
XML,HTML,and JSP Files
In this section we list all the source files that control the servlet and the
HTML output.The servlet knows fromwhich page a request is initiated by
looking at the value of the hidden field PageName.This field is set in all JSP
and HTML files.
XML Servlet Configuration File
The UserProfileCartServlet.servlet file (Figure 228) is an XML file which
contains the definition for all of the pages called by callPage method.The
pages listed in the servlet file are all of type JavaServer Pages.

Chapter 12.WebSphere User Profile
231
Figure 228.UserProfileCartServlet.Servlet Control File
Login HTML File
The UserProfileLogin.html file (Figure 229) is the first page displayed for the
user login.
Figure 229.UserProfileLogin.html File
<?xml version="1.0"?>
<servlet>
<page-list>
<default-page>
<uri>/itso/jsp/UserProfileCartOutputPage.jsp</uri>
</default-page>
<error-page>
<uri>/itso/jsp/UserProfileCartErrorPage.jsp</uri>
</error-page>
<page>
<uri>/itso/jsp/UserProfileCreate.jsp</uri>
<page-name>UserCreate</page-name>
</page>
<page>
<uri>/itso/jsp/ItemInfoInputPage.jsp</uri>
<page-name>ItemCreate</page-name>
</page>
</page-list>
<code>itso.wasdb.userprofile.UserProfileCartServlet</code>
<description></description>
</servlet>
<HTML><HEAD> <TITLE>Login </TITLE> </HEAD> <BODY>
<FORM METHOD=POST
ACTION="/servlet/itso.wasdb.userprofile.UserProfileCartServlet">
Please Input Your User Name:
<INPUT TYPE=HIDDEN NAME="PageName" VALUE="Login">
<TABLE BORDER=0 FRAME=NONE RULES=NONE ALIGN=LEFT WIDTH="50%"
CELLPADDING="4" CELLSPACING="1">
<TR><TH ALIGN="LEFT" WIDTH="5%"></TH> <TH ALIGN="LEFT" WIDTH="45%"></TH>
</TR>
<TR><TD><INPUT TYPE=TEXT NAME="userName" ID="userName"> </TD>
<TD><INPUT TYPE=SUBMIT NAME="submit" ID="submit" VALUE="Login"></TD>
</TR>
</TABLE><br CLEAR="LEFT">
</FORM> </BODY></HTML>

232
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
JSP Files
The UserProfileCreate.jsp file (Figure 230) contains the entry fields for
creating a new UserProfile.
Figure 230.UserProfileCreate.jsp File
<HTML><HEAD>
<TITLE>User Profile Create</TITLE>
</HEAD><BODY>
<BEAN NAME="userProfileCart" TYPE="itso.wasdb.userprofile.UserCart" INTROSPECT=NO
CREATE=NO SCOPE=REQUEST></BEAN>
<form ACTION="/servlet/itso.wasdb.userprofile.UserProfileCartServlet" METHOD="POST"
ENCTYPE="application/x-www-form-urlencoded">
<INPUT TYPE="HIDDEN" NAME="PageName" VALUE="UserCreate">
<div align="center"><center><p><font SIZE="4">Register a New User</font></p>
</center></div><div align="center"><center><table BORDER="0" WIDTH="85%">
<tr>
<td WIDTH="100%">Enter the user parameters below and click Submit. A user profile
with the specified properties will be created if it does not already exist.
Otherwise, you will receive a duplicate user message.</td>
</tr>
<tr> <td WIDTH="100%"></td> </tr>
</table>
</center></div><div align="center"><center><table BORDER="0" CELLPADDING="4"
CELLSPACING="1" WIDTH="50%" FRAME="NONE" RULES="NONE">
<tr>
<td><div align="right"><p>User Name: </td>
<td><input TYPE="TEXT" NAME="userName" SIZE="25" ID="userName"
VALUE=<INSERT BEAN="userProfileCart" PROPERTY="userName"></INSERT>></td>
</tr>
<tr>
<td><div align="right"><p>First Name: </td>
<td><input TYPE="TEXT" NAME="firstName" SIZE="25" ID="firstName"></td>
</tr>
<tr>
<td><div align="right"><p>Last Name: </td>
<td><input TYPE="TEXT" NAME="lastName" SIZE="25" ID="lastName"></td>
</tr>
<tr>
<tr>
<td><div align="right"><p>Language: </td>
<td><input TYPE="TEXT" NAME="language" SIZE="25" ID="language"></td>
</tr>
<td></td>
<td></td>
</tr>
<tr>
<td COLSPAN="2"><div align="center"><center><p><input TYPE="SUBMIT"
NAME="submit"
VALUE="Submit" ID="submit"> <input TYPE="RESET" NAME="reset" VALUE="Reset"
ID="reset"> </td>
</tr>
</table>
</center></div>
</form>
</BODY></HTML>

Chapter 12.WebSphere User Profile
233
Notice the BEAN tag that refers to the UserCart JavaBean.The INSERT tag
retrieves the userName property from the bean.
The UserProfileCartOutputPage.jsp File (Figure 231) displays the content of
the UserProfile,including the contents of its shopping cart.
Figure 231.UserProfileCartOutputPage.jsp File
In this JSP file the userCart is defined in a BEANtag and its property values
are retrieved in INSERT tags.
The ItemInfoInputPage.jsp file (Figure 232) contains the entry fields for
creating a new Item.
<HTML><HEAD>
<TITLE>User Information</TITLE>
</HEAD><BODY>
<H2>User Information<br>
___________________________________________________</H2>
<BEAN NAME="userProfileCart" TYPE="itso.wasdb.userprofile.UserCart" INTROSPECT=NO
CREATE=NO SCOPE=REQUEST></BEAN>
<b> UserName :</b>
<INSERT BEAN="userProfileCart" PROPERTY="userName"> </INSERT><BR>
<b> FirstName:</b>
<INSERT BEAN="userProfileCart" PROPERTY="firstName"> </INSERT><BR>
<b> LastName :</b>
<INSERT BEAN="userProfileCart" PROPERTY="lastName"> </INSERT><BR>
<b> Language :</b>
<INSERT BEAN="userProfileCart" PROPERTY="language"> </INSERT><BR>
<H2>
<br>Item Detail
<br>__________________________________________________</H2>
<BEAN NAME="itemInfo" TYPE="itso.wasdb.userprofile.Item" INTROSPECT=NO CREATE=NO
SCOPE=REQUEST></BEAN>
<b> Item :</b> <INSERT BEAN="itemInfo" PROPERTY="item"></INSERT><BR>
<b> Price:</b> <INSERT BEAN="itemInfo" PROPERTY="price"></INSERT><BR>
<form ACTION="/servlet/itso.wasdb.userprofile.UserProfileCartServlet" METHOD="POST"
ENCTYPE="application/x-www-form-urlencoded">
<input TYPE="HIDDEN" NAME="PageName" VALUE="UserOutput">
To overwrite the existing item or create a new one press:
<input TYPE="SUBMIT" NAME="submit" VALUE="New Item" ID="submit">
</form>
</BODY></HTML>

234
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 232.ItemInfoInputPage.jsp File
The UserProfileCartErrorPage.jsp file (Figure 233) contains a message for
the error.The detail of the error is loaded into the page dynamically (Figure
219 on page 225).
Figure 233.UserProfileCartErrorPage.jsp File
<HTML><HEAD>
<TITLE></TITLE>
</HEAD><BODY>
<FORM METHOD=POST ACTION="/servlet/itso.wasdb.userprofile.UserProfileCartServlet">
Please complete the form and click on Submit.
<input TYPE="HIDDEN" Name="PageName" VALUE="ItemCreate">
<TABLE BORDER=0 FRAME=NONE RULES=NONE ALIGN=LEFT WIDTH="50%" CELLPADDING="4"
CELLSPACING="1">
<TR><TH ALIGN="LEFT" WIDTH="5%"></TH>
<TH ALIGN="LEFT" WIDTH="45%"></TH>
</TR>
<TR>
<TD>Item</TD>
<TD><INPUT TYPE=TEXT NAME="item" ID="item"></TD>
</TR>
<TR>
<TD>Price</TD>
<TD><INPUT TYPE=TEXT NAME="price" ID="price"></TD>
</TR>
</TABLE>
<BR CLEAR="LEFT">
<BR>
<INPUT TYPE=SUBMIT NAME="submit" ID="submit" VALUE="Submit">
</FORM>
</BODY></HTML>
<HTML><HEAD><TITLE>User Profile Error</TITLE></HEAD><BODY>
<h3>An error has occurred using User Profile.</h3>
</BODY></HTML>

Chapter 12.WebSphere User Profile
235
Testing the User Profile Sample in VisualAge for Java
The user profile sample will fail in the VisualAge for Java test environment
because no database information has been prepared for storing user profiles.
Setting up the Database
To activate the user profile function we have to change a properties file of the
WebSphere Test Environment:
c:\IBMVJava\ide\project_resources\IBM WebSphere Test Environment\
properties\server\servlet\servletservice\userprofile.properties
Change the properties files as shown in Figure 234.
Figure 234.User Profiles Properties File
Source Files
The source files must be placed into appropriate directories:

UserProfileCartServlet.servlet:store in the project resources directory of
VisualAge for Java:
c:\IBMVJava\ide\project_resources\ITSO SG24 5471\itso\wasdb\userprofile

UserProfileLogin.html:store in a subdirectory of the HTML directory:
c:\Program Files\IBM HTTP Server\htdocs\itso

JSP Files:store in a subdirectory of the HTML directory:
c:\Program Files\IBM HTTP Server\htdocs\itso\jsp
# @(#)userprofile.properties
#
# User Profiles Configuration Screen
#
ncf.userprofile=true
ncf.userprofile.db.used=db2
ncf.userprofile.db.jdbcdriver=COM.ibm.db2.jdbc.app.DB2Driver
ncf.userprofile.db.instance=ITSOWDB
ncf.userprofile.db.owner=ITSO
ncf.userprofile.tablename=userprofile
ncf.userprofile.userid=ITSO
ncf.userprofile.password=itso
ncf.userprofile.classname=
com.ibm.servlet.personalization.userprofile.UserProfile
#
# Properties for connection manager initialization
ncf.userprofile.connmgr.poolname=JdbcDb2
ncf.userprofile.connmgr.waitretry=false

236
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Run the User Profile Sample
Start the WebSphere Test Environment and open a browser with the URL:
http://127.0.0.1:8080/itso/UserProfileLogin.html
The first access to a user profile creates the ITSO.USERPROFILE table in
the ITSOWDB database.
The output of the user profile sample should match the decription in “Output
Result” on page 228.
Running the User Profile Sample in WebSphere
Configure WebSphere as described in “Configuration” on page 214.Deploy
the beans and the servlet to WebSphere:
c:\WebSphere\AppServer\servlets
The source files must be placed into appropriate directories:

UserProfileCartServlet.servlet:store in the servlets directory of
WebSphere,together with the servlet code and the beans.
c:\WebSphere\AppServer\servlets\itso\wasdb\userprofile

UserProfileLogin.html:store in a subdirectory of the HTML directory:
c:\Program Files\IBM HTTP Server\htdocs\itso

JSP Files:store in a subdirectory of the HTML directory:
c:\Program Files\IBM HTTP Server\htdocs\itso\jsp
Open a browser with the URL:
http://hostname/itso/UserProfileLogin.html
The first access to a user profile creates the ITSO.USERPROFILE table in
the ITSOWDB database on the WebSphere server machine.
The output of the user profile sample should match the description in
“Output Result” on page 228.

© Copyright IBM Corp.1999
237
13 WebSphere Security
and the LDAP
Directory
In this chapter we discuss securing access to resources using a directory
service.

238
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Introduction
A directory service is a combination of:

An information repository:A database is often used to store location
information and other details about resources such as users,printers,file
servers,and application servers.

An access method:The Lightweight Directory Access Protocol (LDAP)
or other access methods can be used to communicate with directory
service components.

Related services:A directory service provides facilities for querying,
manipulating,and authenticating the information in its database.
You might already have set up a directory service product to hold global
security data that many other software applications,such as an application
server and your Web server,can use to authenticate users who try to access
resources.
WebSphere supports the use of three different directory service types:

Domino 4.6

Netscape Directory Server

e-Network IBMSuite
The security access to the directory service is provided through ACL,access
control list,in WebSphere.When using directory service for security,
WebSphere acts as the client of the directory service.The setup for the
resource security is done through an ACL.Resources such as servlets are
assigned an ACL,and based on the permissions provided through the ACL,
the level of access is provided.
Access Control Lists
The ACL associated with a resource specifies which users or groups in a
realmare permitted to access the resource.ACLs,realms,and resources have
the following relationships (Figure 235):

A realm can contain many ACLs.

A realm can contain many resources.

An ACL can belong to only one realm.

A resource is associated with only one ACL.

A resource can belong to only one realm.

An ACL can be associated with many resources.

Chapter 13.WebSphere Security and the LDAP Directory
239
Figure 235.Relationships Between Realm,ACL,and Resource
In some cases,a service does not require its customers to be in an access
control list.For example,many Web page (HTTP) services make their
documents available to all users without requiring them to register in an
access control list (ACL).
Realms
A realmis a database of users,groups,and ACLs.A user must be defined in a
realm in order to access any resources belonging to that realm.
A user can belong to several realms,but a user ID cannot be duplicated
within a realm.For example,the user amy can belong to the realms
fileRealm and anyRealm.Each of those realms can contain only one user
named amy.The user amy can be given different permissions for different
resources in each realm.
A person,such as Amy,might have different user IDs in each realm.For
example,she might be amy in the fileRealm,but is amy2 in the anyRealm
because there is already a person in anyRealmusing the user ID amy.
IBMWebSphere Application Server ships with some pre-established realms:

The defaultRealmdefines how users may access resources defined
locally.You can establish ACLs to determine which users and groups have
access to which resources.

The NT realmand the UNIX realmdefine how users with accounts on
your operating systems may access WebSphere resources.Users defined
in the operating systemare shared by WebSphere and continue to exist
for as long as they exist in the underlying system.The application server
manager interface lets you view this realm;to change it,you must use the
facilities provided by your operating system.Currently,WebSphere can
share users,but not groups,defined in the operating system.
Realm
ACL Resource
1
1
m
1
m
m
m = many

240
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

The servletMgrRealmdefines how servlets may access resources
defined remotely,such as remotely loaded servlets.The servlet ACL is the
only access control list in this realm.When a remotely loaded,
digitally-signed servlet tries to access a protected resource,the digital
certificate in the servlet is compared with digital certificates associated
with users in the servletMgrRealm.The servletACL decides whether
permission is granted or denied.
For example,suppose the digital certificate of userX is packaged in the
anyServlet.JAR file.If userX is added to the servletMgrRealm,any servlet
containing the same digital certificate as that of userX (found in
anyServlet.JAR) may execute and access resources granted to userX.

Finally,the ldapRealm(Figure 240 on page 244) is displayed if you have
a directory service enabled on the Directory Management page (Figure
236 on page 241) of the WebSphere Application Server Administration.
Users and groups defined in the directory service are shared by WebSphere
and continue to exist for as long as they exist in the directory service or until
you disable directory management support.The application server manager
interface lets you viewthis realm;to change it,you must use the facilities the
LDAP server provides.
For more information about LDAP,directory services,and the ldapRealm,see
Using a directory service with Application Server in the WebSphere online
documentation.Also refer to the LDAP Implementation Cookbook,
SG24-5110.
Resources
Resources are the valuable items accessible by the Web server:

HTML files and directories,such as
http://www.anycompany.com

Other files and directories,such as
ftp://www.anycompany.com

Web applications:Java servlets or CGI programs
and by the WebSphere Application Server:

Java servlets

Connections,sockets,files,and other resources used by servlets

Customservlets that enable access to enterprise resources and
applications (such as databases)
Each resource can be protected by establishing a single ACL for that
resource,in a single realm.The ACL specifies which users or groups are
allowed to access or modify the resource.

Chapter 13.WebSphere Security and the LDAP Directory
241
For each resource to protect,you specify:

An access control list (ACL)—a list defining who can use the resource

A security realm—a logical security area the resource belongs to

An authentication scheme—a way to verify users who ask for the resource
The relationship among these items will become apparent shortly.
Setup and Configuration
You must have installed a directory server and user IDs created for the
directory server prior to starting the setup on WebSphere.
Netscape Directory Server 4.0 was used to provide security access to our
resources,namely servlets.
The following are the steps involved in setup and configuration of security
through directory service.

Start the WebSphere Administration panel,see “WebSphere
Configuration” on page 6.Select Directory Management (Figure 236).
Figure 236.WebSphere Administration Setup for Directory Management

From the WebSphere Administration main menu,select the Access
Control List and select the ldapRealmand click on the Add button (Figure
237).

242
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 237.WebSphere Access Control List Main Panel

From the Add ACL panel,input the New ACL Name (Figure 238).
Figure 238.Adding a New Access Control List

From the Access Control List main panel (Figure 237) select the newly
created ACL and click on the lower Add button to add permissions to the
new ACL.Select the type of permissions you want to give to a user or a
group (Figure 239).As you may have noticed,the user IDs and group IDs
are the same as the ones you have specified in the directory service.

Chapter 13.WebSphere Security and the LDAP Directory
243
Note:To enable access to the servlets,you should Assign Permission for Files
and Folders.Files and Folders refers to local directories and files,including
servlets.Servlets refers to remotely loaded servlets.If you are unsure which
to choose,Files and Folders is the appropriate choice in most cases.
Figure 239.Add Permission to ACL

At this point you have specified your ACL,and the ACL panel displays
information based on the choices you made in the Add Permission to ACL
panel (Figure 240).

244
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Figure 240.WebSphere Access Control List Main Panel with New ACL

Next is to set up access to your resources based on the newly created ACL.
Select Resources fromthe WebSphere Administration main menu.From
the Resources panel select the ldapRealm.Click on the Add button to add
a resource to the desired ACL (Figure 241).
Figure 241.WebSphere Resources Security Setup

Chapter 13.WebSphere Security and the LDAP Directory
245

From the Protect a Resource dialog,select a Scheme,ACL,and a Resource
to Protect,and click on the OK button (Figure 242).
Figure 242.Protect a Resource using Directory Service through ACL
After performing these steps you have specified your resource security access
based on the setting in the directory service (Figure 243).
Figure 243.Resource Panel after Adding a Resource to be Protected

246
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

© Copyright IBM Corp.1999
247
14 Using SQLJ to
Access a Database
SQLJ is a method of accessing database information using embedded SQL in
Java code.In this chapter we will discuss what you need in order to use SQLJ
and give a simple example of using SQLJ with WebSphere and DB2.
Note:To use the SQLJ functionality,you must install fixpack 8 for DB2
Version 5.

248
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
What is SQLJ?
SQLJ allows Java programmers to create static SQL for Java applications as
well as the dynamic SQL which traditional JDBC drivers provide.The use of
static SQL allows all authority,syntax,access strategy,and logic checking to
be done at SQL compile time.This unique ability provides performance
increases for applications that repeatedly use the same SQL.
SQLJ was developed as a combined effort by Oracle,Tandem,and IBMto add
static SQL to JDBC.It has recently become an ANSI standard.The
specification consists of three parts:

Part 0 specifies the SQLJ language syntax and semantics for embedded
SQL statements in a Java application.

Part 1 specifies extensions that define:
• Installation of Java classes in an SQL database
• Invocation of static methods as stored procedures

Part 2 specifies extensions for accessing Java classes as SQL distinct
types.
Most database vendors have implemented part 0 of this plan,but the goal is
to gradually introduce the SQLJ technology into the products.We focus only
on part 0 in this book.
SQLJ and VisualAge for Java
Unfortunately,VisualAge for Java 2.0 does not support SQLJ.SQLJ
operability is scheduled for Version 3,which should be available in the third
quarter of 1999.
VisualAge for Java Version 2 cannot be used with SQLJ because VisualAge
does not recognize the precompiler directives that SQLJ uses before its
translation.This implies that the embedded SQL statements and other SQLJ
resources will cause errors within VisualAge for Java.
This will cause some development slowdown,because you cannot use the
WebSphere Test Environment and the VisualAge for Java debugger.

Chapter 14.Using SQLJ to Access a Database
249
SQLJ Translators,Input,and Output
To compile Java code with embedded SQLJ,you must put your code through
a translator that will comment out and preprocess the SQLJ code so that a
normal Java compiler can successfully compile the code.
The naming convention for SQLJ states that all Java files with embedded
SQLJ should have an extension of.sqlj before they are translated.The SQLJ
translator is used to translate.sqlj files into.java files.It comments out all of
the SQLJ code and replaces it with the proper Java code used to implement
the static SQL.
When this translation occurs,Java classes are created for the SQLJ iterators.
Iterators are cursors that are used to store the results of the embedded SQL
query.Because the result sets can have different numbers of columns and
column types,a separate iterator must be created for each unique result set.
You define a cursor to parse a result set using embedded SQL that is later
interpreted by the translator.
A class and a serialized file are also created for the query profile.This profile
maintains the data used to keep the SQL static.
Updating Serialized Profiles
Instead of having to rebuild information as dynamic SQL does,the serialized
profile stores the information necessary to make the SQL execution perform
as quick as possible.The serialization file stores the bind information used to
access the database.
This profile can be customized by running it through a customizer which
connects to the database and updates the information stored regarding
accessing your data.This customizer is found in the same directory as the
translator.For DB2 the customizer is called db2profc.exe.It has the following
usage:
db2profc -user=<user> -password=<password> url=<database url>
<profile name>.ser
You should customize the profile when the tables significantly change in
volume.Failure to do so can cause inefficient access methods to be used to
access the data.This is important,considering that performance is the main
point of using static SQL files.

250
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
DB2 Resources for SQLJ
For applications written using SQLJ to work properly,certain resources must
be available.It is imperative that you include the following files in your class
path.These files must be in the WebSphere class path as well.
c:\sqllib\java\db2java.zip
c:\sqllib\java\runtime.zip
c:\sqllib\java\sqlj.zip
The sqlj.zip file is included with the DB2 Software Developer’s Kit.It places
the zip file in the same directory as other DB2 Java resources.
SQLJ is implemented using a JDBC connection to the database on Windows
NT.To execute the SQL,you must first create a connection and a connection
context as shown in our example.
Using the SQLJ Translator with DB2
With the installation of DB2 Software Developer’s Kit,an executable named
sqlj.exe is placed into the d:\sqllib\bin directory (where sqllib is the DB2
installation directory).This file is the SQLJ translator.
The SQLJ command has the format:
sqlj <Java file name with SQLJ>
The output file of type.java can be used by any Java translator and can be
compiled normally.The SQLJ translator,by default,automatically compiles
the output file and creates a serialized profile.The translator does not catch
errors in the Java source file,so make sure the file is syntactically correct.
Employees in Department Example
We use our example of finding all the employees in a specific department.We
implement this example using a servlet and a bean.The servlet contains all
the Java code to communicate with the bean.The bean controls the retrieval
and displaying of the data.

Chapter 14.Using SQLJ to Access a Database
251
Servlet
The SqljServ servlet (Figure 244) contains no SQLJ code,therefore it is not
run through the SQLJ translator.The servlet code invokes methods that
contain SQLJ through the SQLJServBean.
Figure 244.SQLJ Example Servlet (SqljServ.java)
package itso.wasdb.sqlj;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SqljServ extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res) {
PrintWriter out = null;
String drv = "COM.ibm.db2.jdbc.app.DB2Driver";
String dep = "A00";
SqljServBean sqljBean = new SqljServBean();
try {
out = res.getWriter();
out.println("<HTML><BODY>");
Class.forName(drv).newInstance();
sqljBean.setUserID("itso");
sqljBean.setPassword("itso");
sqljBean.setURL("jdbc:db2:itsowdb");
sqljBean.startConnection();
sqljBean.executeSQLJ(out, dep);
}
catch(SQLException sqlExcept) { sqlExcept.printStackTrace(out);
catch (Exception except) {
if( out != null ) {
out.println(except);
except.printStackTrace(out);
}
else {
System.out.println(except);
except.printStackTrace();
}
}
}
}

252
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Bean with SQLJ Code Before Translation
The SQLJServBean (Figure 245 and Figure 246) creates the connection,
executes the SQL,and displays the result.These figures show the.sqlj code
before translation.
Figure 245.SQLJ Example Servlet Bean (SqljServBean.sqlj) Part 1
package itso.wasdb.sqlj;
import java.io.*;
import java.sql.*;
import sqlj.runtime.*;
import sqlj.runtime.ref.*;
#sql iterator DataCursor (String empno, String lastname, String workdept,
String Sex);
public class SqljServBean {
// Instance variable for the URL property
private String url=null;
// Instance variable for the userID property
private String userID=null;
// Instance variable for the password property
private String password=null;
// Instance variable for Connection
private Connection con=null;
public void startConnection() throws SQLException{
try {
con = DriverManager.getConnection(url, userID, password);
con.setAutoCommit(false);
DefaultContext ctx = new DefaultContext(con);
DefaultContext.setDefaultContext(ctx);
} catch (SQLException e) {
System.out.println("Error: could not get a default context");
System.err.println(e) ;
System.exit(1);
} catch (Exception e){
System.out.println(e);
}
return;
}
}
}

Chapter 14.Using SQLJ to Access a Database
253
The startconnection method creates a connection to the database and creates
a default context.SQLJ uses the default context connection in use with its
serialized profile,so you must set one in your code.
Figure 246.SQLJ Example Servlet Bean (SqljServBean.sqlj) Part 2
public void executeSQLJ(PrintWriter out, String dep) throws SQLException
{
DataCursor cursor1;

long count1 = 0;

// retrieve data from the database
System.out.println("Retrieve some data from the database...");
#sql cursor1 = { SELECT empno, lastname, workdept, sex from itso.employee
where WORKDEPT = :dep };
// retrieve number of employees in the department
#sql {SELECT count(*) into :count1 from employee where workdept = :dep };
try {
outputInfo(cursor1, count1, out);
}catch (SQLException hmmm) { throw hmmm;}
}
public void outputInfo(DataCursor cs, long count, PrintWriter out)
throws SQLException{
out.println("<TABLE BORDER=3 FRAME=ALL CELLPADDING=1 CELLSPACING=1
COLS=4>");
out.println("<CAPTION ALIGN=TOP><b>UDB - " + count +
" Employees in Department</b></CAPTION>");
out.println("<TR>");
out.println("<TH ALIGN=CENTER WIDTH=20%>Emp Number ");
out.println("<TH ALIGN=CENTER WIDTH=20%>Last Name ");
out.println("<TH ALIGN=CENTER WIDTH=20%>Dept Name ");
out.println("<TH ALIGN=CENTER WIDTH=20%>Sex ");
while(cs.next()) {
out.print("<TR><TD ALIGN=CENTER>" + cs.empno());
out.print("<TD ALIGN=CENTER>" + cs.lastname());
out.print("<TD ALIGN=CENTER>" + cs.workdept());
out.print("<TD ALIGN=CENTER>" + cs.Sex());
}
out.println("</TABLE>");
}

254
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
The executeSQLJ method contains the embedded SQL.It creates a cursor to
store the result of the SQL query and returns the result set to the cursor.
The outputInfo method displays the information contained in the cursor.It
uses the PrintWriter class to write the results to the Web browser.You can
see how simple it is to retrieve a data item from the result set by the use of
the cursor methods.
SQLJ Code After Translation
The SQLJ code in these listings performs multiple functions.Each is
described here and the Java code resulting from translation is shown.
Iterator
#sql iterator DataCursor (String empno, String lastname, String workdept,
String Sex)
This statement creates the iterator,or cursor,used to store the result set of
the SQL query.Because the SQL can return varying columns and types,we
need to define an iterator before including what kind of output to expect so it
can properly store the information.The cursor data types and the data types
returned from the SQL statement are compared and checked during the
translation of the SQLJ code.
In Figure 247,we see the definition of the iterator.The SQLJ translator
creates this code,which makes it extremely easy to manage the output of
queries.You just call the method named after the column to retrieve the
information stored in that column.The rows are traversed using the
iterator’s next method as shown in the outputInfo method (Figure 246 on
page 253).

Chapter 14.Using SQLJ to Access a Database
255
Figure 247.SQLJ Code after Translation:Iterator Class Definition
/*@lineinfo:generated-code*//*@lineinfo:10^1*/
// ************************************************************
// SQLJ iterator declaration:
// ************************************************************
class DataCursor
extends sqlj.runtime.ref.ResultSetIterImpl
implements sqlj.runtime.NamedIterator
{
public DataCursor(sqlj.runtime.profile.RTResultSet resultSet)
throws java.sql.SQLException
{
super(resultSet);
empnoNdx = findColumn("empno");
lastnameNdx = findColumn("lastname");
workdeptNdx = findColumn("workdept");
SexNdx = findColumn("Sex");
}
public String empno()
throws java.sql.SQLException
{
return resultSet.getString(empnoNdx);
}
private int empnoNdx;
public String lastname()
throws java.sql.SQLException
{
return resultSet.getString(lastnameNdx);
}
private int lastnameNdx;
public String workdept()
throws java.sql.SQLException
{
return resultSet.getString(workdeptNdx);
}
private int workdeptNdx;
public String Sex()
throws java.sql.SQLException
{
return resultSet.getString(SexNdx);
}
private int SexNdx;
}
// ************************************************************
/*@lineinfo:user-code*//*@lineinfo:10^85*/

256
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Select Statement with Result Set
#sql cursor1 = { SELECT empno, lastname, workdept, sex from itso.employee
where WORKDEPT = :dep };
This select statement is embedded SQL code that uses a host variable (:dep).
You can see how simple it is to use the SQL and return the result into the
iterator.Figure 248 shows the translated SQLJ code.It is challenging to
understand this code,but notice the use of the connection context.This is
why it is necessary to create a default connection context when you create
your initial connection to the database.Also notice how the code uses the
ProfileKeys.ProfileKeys are an index to all the profiles used in the
application and are stored in the serialized files that allow SQLJ to be static.
Figure 248.SQLJ Code after Translation:Simple Select Statement
// ************************************************************
// #sql cursor1 = { SELECT empno, lastname, workdept, sex from itso.employee
where WORKDEPT = :dep };
// ************************************************************
{
sqlj.runtime.ConnectionContext __sJT_connCtx =
sqlj.runtime.ref.DefaultContext.getDefaultContext();
if (__sJT_connCtx == null)
sqlj.runtime.error.RuntimeRefErrors.raise_NULL_CONN_CTX();
sqlj.runtime.ExecutionContext __sJT_execCtx =
__sJT_connCtx.getExecutionContext();
if (__sJT_execCtx == null)
sqlj.runtime.error.RuntimeRefErrors.raise_NULL_EXEC_CTX();
String __sJT_1 = dep;
sqlj.runtime.profile.RTStatement __sJT_stmt =
__sJT_execCtx.registerStatement(__sJT_connCtx,
itso.wasdb.sqlj.SqljServBean_SJProfileKeys.getKey(0), 0);
try
{
__sJT_stmt.setString(1, __sJT_1);
sqlj.runtime.profile.RTResultSet __sJT_result =
__sJT_execCtx.executeQuery();
cursor1 = new DataCursor(__sJT_result);
}
finally
{
__sJT_execCtx.releaseStatement();
}
}
// ************************************************************
/*@lineinfo:user-code*//*@lineinfo:47^100*/

Chapter 14.Using SQLJ to Access a Database
257
Select Statement without Result Set
#sql { SELECT count(*) into :count1 from employee where workdept = :dep };
This statement does not return a result set,so it does not require an iterator.
Instead,this set simply counts the number of elements in the result and
stores that number in a host variable (:count1).Figure 249 shows the
translation of this statement.
Figure 249.SQLJ Code after Translation:Select Without Result Set
// ************************************************************
// #sql { SELECT count(*) from employee where workdept = :dep };
// ************************************************************
{
sqlj.runtime.profile.RTResultSet __sJT_rtRs;
sqlj.runtime.ConnectionContext __sJT_connCtx =
sqlj.runtime.ref.DefaultContext.getDefaultContext();
if (__sJT_connCtx == null)
sqlj.runtime.error.RuntimeRefErrors.raise_NULL_CONN_CTX();
sqlj.runtime.ExecutionContext __sJT_execCtx = __sJT_connCtx.getExecutionContext();
if (__sJT_execCtx == null)
sqlj.runtime.error.RuntimeRefErrors.raise_NULL_EXEC_CTX();
String __sJT_1 = dep;
sqlj.runtime.profile.RTStatement __sJT_stmt =
__sJT_execCtx.registerStatement(__sJT_connCtx,
itso.wasdb.sqlj.SqljServBean_SJProfileKeys.getKey(0), 1);
try {
__sJT_stmt.setString(1, __sJT_1);
sqlj.runtime.profile.RTResultSet __sJT_result = __sJT_execCtx.executeQuery();
__sJT_rtRs = __sJT_result;
}
finally {
__sJT_execCtx.releaseStatement();
}
try {
sqlj.runtime.ref.ResultSetIterImpl.checkColumns(__sJT_rtRs, 1);
if (!__sJT_rtRs.next())
{ sqlj.runtime.error.RuntimeRefErrors.raise_NO_ROW_SELECT_INTO(); }
count1 = __sJT_rtRs.getLongNoNull(1);
if (__sJT_rtRs.next())
{
sqlj.runtime.error.RuntimeRefErrors.raise_MULTI_ROW_SELECT_INTO();
}
}
finally
{
__sJT_rtRs.close();
}
}
// ************************************************************
/*@lineinfo:user-code*//*@lineinfo:51^76*/

258
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Deployment of an SQLJ Servlet to WebSphere
Moving the SQLJ servlet is relatively simple.Place the servlet files in the
same directory as the other servlets on your machine.It is important that
you remember to move all of the class files that are output fromthe
translator.Any iterators you had will create their own class file.A ProfileKey
class,which is an index to your profiles,and the actually profile (.ser file) will
also be created and must be accessible by the servlet.The servlet directory
containing your classes is a natural place to put both of these files.
You should also make sure that the proper class paths are listed in the Java
Engine setup (Figure 250).You must be sure to add the files:
c:\sqllib\java\db2java.zip
c:\sqllib\java\runtime.zip
c:\sqllib\java\sqlj.zip
Figure 250.WebSphere Application Server:Java Engine Setup

Chapter 14.Using SQLJ to Access a Database
259
SQLJ Servlet Output
The servlet is now ready to run under WebSphere.The output is shown in
Figure 251.
Figure 251.Output of SQLJ Servlet Employees in Department Example

260
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

© Copyright IBM Corp.1999
261
Appendixes

262
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

© Copyright IBM Corp.1999
263
A Odds and Ends
In this chapter we describe the various odds and ends that we encountered
while working with the different database systems.

264
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Different Mapping of SQL Types
After creating the database and inspecting it with the tools to build query
statements,we discovered that the databases differ in the way they interpret
types.To see this,it is sufficient to compare the mapping of the employee
table for DB2 (Figure 252),Oracle (Figure 253),and SQL Server (Figure 254).
As you can see,mapping differs in those fields related to dates and to the
SMALLINT field.Unfortunately,this makes it impossible to use one meta
data specification for the data access bean for all databases.
This applies to code that depends on the IBMdata access beans,that is code
generated by the Studio Wizards,code generated by the SmartGuides of
VisualAge for Java when using the Select bean,and also non-generated hand
written code.
Figure 252.Default Mapping for the Employee Table in DB2

Appendix A.Odds and Ends
265
Figure 253.Default Mapping for the Employee Table in Oracle
Figure 254.Default Mapping for the Employee in ODBC/SqlServer

266
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
Correct Deployment of Enterprise Beans
Because the deployment of enterprise beans is a somewhat difficult step,
especially because very few error messages are thrown that could indicate
what was wrong,keep the following rules in mind:

For a BMP bean,deploy the EJS file rather than the EJB file in VisualAge
for Java.This preserves the mapping to the database.

In WebSphere,do not regenerate the bean,only redeploy it.

Delete any unused containers (Entity and Session).

Session containers must specify a valid DB2 database;also verify that the
specified user ID may access the database.

To complete the deployment process,you have to stop WebSphere,then
stop the HTTP Server,and finally start the HTTP Server again,which
should start up the WebSphere Servlet Service as well.

Add memory to your computer;to use VisualAge for Java and WebSphere
we suggest 128MB minimum.
Data Access Beans
It seems that the data access beans do generally work better when the input
and output types are not specified using classes,but using integer values of
the java.sql.Types class.Unfortunately,this creates problems with the
Wizards of WebSphere Studio,because they always generate
ClassName.class instead of Types.TYPE.
According to IBMsupport,this is a known bug and is fixed as of version 1.1 of
the data access beans.Unfortunately this version is neither part of
WebSphere nor WebSphere Studio.Especially for WebSphere Studio,this is
unfortunate because it makes the usage of Date fields impossible.
The bug manifests itself in the following example:
addColumn("HIREDATE", java.sql.Date.class, Types.DATE);
addColumn("HIREDATE", Types.DATE, Types.DATE);
The first call fails and throws a com.ibm.db.DataException (Not supported
Java class is specified in parameter field),but the second call works.
The same problems appear with time stamps:
addColumn("HIREDATE", java.sql.Timestamp.class, Types.TIMESTAMP); <== fails
addColumn("HIREDATE", Types.TIMESTAMP, Types.TIMESTAMP); <== works

Appendix A.Odds and Ends
267
WebSphere Studio
When WebSphere Studio 3 beta 2 is used to create a servlet accessing a
database,there is a little bug when using the option Create page for when no
data is returned (Figure 255).
Figure 255.WebSphere Studio Database Wizard
If this option is used,the Database Wizard creates a servlet class that
catches a com.ibm.webtools.runtime.NoDataException.This exception is part
of the file webtlsrn.jar,which is part of the WebSphere Studio distribution,
but apparently it is not part of WebSphere Application Server.The result is
that the servlet will not execute at all when published to the application
server because it cannot find the exception class.
One workaround is simply not to use this option,but this is not a solution
when one would like to use the servlets to update the database.
The second possibility is simply to copy the JAR file to the application server:
from: c:\WebSphere\StudioBeta\lib\AppServerV2\webtlsrn.jar
to: c:\WebSphere\AppServer\lib
With this solution,the generated servlet works well.

268
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

© Copyright IBM Corp.1999
269
B Special Notices
This publication is intended to help Java developers develop enterprise
applications with VisualAge for Java Enterprise Version 2 and IBM
WebSphere,together with relational database systems.The information in
this publication is not intended as the specification of any programming
interfaces that are provided by VisualAge for Java Enterprise and IBM
WebSphere.See the PUBLICATIONS section of the IBMProgramming
Announcement for VisualAge for Java Enterprise and IBMWebSphere for
more information about what publications are considered to be product
documentation.
References in this publication to IBMproducts,programs or services do not
imply that IBMintends to make these available in all countries in which
IBMoperates.Any reference to an IBMproduct,program,or service is not
intended to state or imply that only IBM's product,program,or service may
be used.Any functionally equivalent program that does not infringe any of
IBM's intellectual property rights may be used instead of the IBMproduct,
program or service.
Information in this book was developed in conjunction with use of the
equipment specified,and is limited in application to those specific hardware
and software products and levels.

270
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
IBMmay have patents or pending patent applications covering subject
matter in this document.The furnishing of this document does not give you
any license to these patents.You can send license inquiries,in writing,to the
IBMDirector of Licensing,IBMCorporation,North Castle Drive,Armonk,
NY 10504-1785.
Licensees of this program who wish to have information about it for the
purpose of enabling:(i) the exchange of information between independently
created programs and other programs (including this one) and (ii) the mutual
use of the information which has been exchanged,should contact IBM
Corporation,Dept.600A,Mail Drop 1329,Somers,NY 10589 USA.
Such information may be available,subject to appropriate terms and
conditions,including in some cases,payment of a fee.
The information contained in this document has not been submitted to any
formal IBMtest and is distributed AS IS.The information about non-IBM
("vendor") products in this manual has been supplied by the vendor and IBM
assumes no responsibility for its accuracy or completeness.The use of this
information or the implementation of any of these techniques is a customer
responsibility and depends on the customer's ability to evaluate and
integrate theminto the customer's operational environment.While each item
may have been reviewed by IBMfor accuracy in a specific situation,there is
no guarantee that the same or similar results will be obtained elsewhere.
Customers attempting to adapt these techniques to their own environments
do so at their own risk.
Any pointers in this publication to external Web sites are provided for
convenience only and do not in any manner serve as an endorsement of these
Web sites.
Any performance data contained in this document was determined in a
controlled environment,and therefore,the results that may be obtained in
other operating environments may vary significantly.Users of this document
should verify the applicable data for their specific environment.
This document contains examples of data and reports used in daily business
operations.To illustrate themas completely as possible,the examples
contain the names of individuals,companies,brands,and products.All of
these names are fictitious and any similarity to the names and addresses
used by an actual business enterprise is entirely coincidental.
Reference to PTF numbers that have not been released through the normal
distribution process does not imply general availability.The purpose of
including these reference numbers is to alert IBMcustomers to specific
information relative to the implementation of the PTF when it becomes

Appendix B.Special Notices
271
available to each customer according to the normal IBMPTF distribution
process.
The following terms are trademarks of the International Business Machines
Corporation in the United States and/or other countries:
The following terms are trademarks of other companies:
C-bus is a trademark of Corollary,Inc.in the United States and/or other
countries.
Java and all Java-based trademarks and logos are trademarks or registered
trademarks of Sun Microsystems,Inc.in the United States and/or other
countries.
Microsoft,Windows,Windows NT,and the Windows logo are trademarks of
Microsoft Corporation in the United States and/or other countries.
PC Direct is a trademark of Ziff Communications Company in the United
States and/or other countries and is used by IBMCorporation under license.
ActionMedia,LANDesk,MMX,Pentium and ProShare are trademarks of
Intel Corporation in the United States and/or other countries.
UNIX is a registered trademark in the United States and/or other countries
licensed exclusively through X/Open Company Limited.
SET and the SET Logo are trademarks owned by SET Secure Electronic
Transactions LLC.
Other company,product,and service names may be trademarks or service
marks of others.
IBM
AS/400 DB2
CICS OS/2
OS/390 OS/400
S/390 TeamConnection
ThinkPad VisualAge
WebSphere

272
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

© Copyright IBM Corp.1999
273
C Related Publications
The publications listed in this section are considered particularly suitable for
a more detailed discussion of the topics covered in this redbook.

274
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
International Technical Support Organization
Publications
For information on ordering these ITSO publications see “How to Get ITSO
Redbooks” on page 277.

Developing an e-Business Application for the WebSphere Application
Server,SG24-5423

VisualAge for Java Enterprise Version 2:Persistence Builder with GUIs,
Servlets,and Java Server Pages,SG24-5426

WebSphere Application Servers - Standard and Advanced,SG24-5460

Enterprise JavaBeans Development Using VisualAge for Java,SG24-5429

Java Application Development for CICS:Base Services and CORBA Client
Support,SG24-5275

Using VisualAge Smalltalk ObjectExtender,SG24-5258

VisualAge for Java Enterprise Version 2:Data Access Beans - Servlets -
CICS Connector,SG24-5265

Programming with VisualAge for Java Version 2,SG24-5264,published by
Prentice Hall,ISBN 0-13-021298-9,1999 (IBMform number SR23-9016)

VisualAge for Java Enterprise Version 2 Team Support,SG24-5245

Using VisualAge for Java Enterprise Version 2 to Develop CORBA and
EJB Applications,SG24-5276

VisualAge Java-RMI-Smalltalk,The ATMSample fromA to Z,SG24-5418

Using VisualAge UML Designer,SG24-4997

Application Development with VisualAge for Java Enterprise,SG24-5081

Creating Java Applications with NetRexx,SG24-2216

Unlimited Enterprise Access with Java and VisualAge Generator,
SG24-5246

FromClient/Server to Network Computing,A Migration to Java,
SG24-2247

Connecting the Enterprise to the Internet with MQSeries and VisualAge for
Java,SG24-2144

Appendix C.Related Publications
275
Redbooks on CD-ROMs
Redbooks are also available on the following CD-ROMs.Click the CD-ROMs
button at
http://www.redbooks.ibm.com/
for information about all the CD-ROMs
offered,updates and formats.
Other Publications
These publications are also relevant as further information sources:

Developing JavaBeans with VisualAge for Java Version 2,SC34-4735

Design Patterns:Elements of Reusable Object-Oriented Software,Erich
Gamma,Richard Helm,Ralph Johnson,and John Vlissides,published by
Addison-Wesley Professional Computing Series,ISBN 0-201-63361,1995
(IBMformnumber SR28-5629)
CD-ROM Title Collection Kit
Number
System/390 Redbooks Collection SK2T-2177
Networking and Systems Management Redbooks Collection SK2T-6022
Transaction Processing and Data Management Redbooks Collection SK2T-8038
Lotus Redbooks Collection SK2T-8039
Tivoli Redbooks Collection SK2T-8044
AS/400 Redbooks Collection SK2T-2849
Netfinity Hardware and Software Redbooks Collection SK2T-8046
RS/6000 Redbooks Collection (BkMgr) SK2T-8040
RS/6000 Redbooks Collection (PDF Format) SK2T-8043
Application Development Redbooks Collection SK2T-8037
IBMEnterprise Storage and Systems Management Solutions SK3T-3694

276
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

© Copyright IBM Corp.1999
277
How to Get ITSO Redbooks
This section explains how both customers and IBMemployees can find out about ITSO
redbooks,redpieces,and CD-ROMs.A form for ordering books and CD-ROMs by fax or
e-mail is also provided.
• Redbooks Web Site
http://www.redbooks.ibm.com/
Search for,view,download or order hardcopy/CD-ROMredbooks from the redbooks web
site.Also read redpieces and download additional materials (code samples or
diskette/CD-ROMimages) from this redbooks site.
Redpieces are redbooks in progress;not all redbooks become redpieces and sometimes just
a few chapters will be published this way.The intent is to get the information out much
quicker than the formal publishing process allows.
• E-mail Orders
Send orders by e-mail including information from the redbooks fax order form to
:
• Telephone Orders
• Fax Orders
This information was current at the time of publication,but is continually subject to change.
The latest information may be found at the redbooks Web site.
In United States
Outside North America
e-mail address
usib6fpl@ibmmail.com
Contact information is in the “How to Order” section at this
site:
http://www.elink.ibmlink.ibm.com/pbl/pbl/
United States (toll free)
Canada (toll free)
Outside North America
1-800-879-2755
1-800-IBM-4YOU
Country coordinator phone number is in the “How to Order”
section at this site:
http://www.elink.ibmlink.ibm.com/pbl/pbl/
United States (toll free)
Canada
Outside North America
1-800-445-9269
1-403-267-4455
Fax phone number is in the “How to Order” section at this site:
http://www.elink.ibmlink.ibm.com/pbl/pbl/
IBMemployees may register for information on workshops,residencies,and redbooks by
accessing the IBMIntranet Web site at
http://w3.itso.ibm.com/
and clicking the ITSO
Mailing List button.Look in the Materials repository for workshops,presentations,
papers,and Web pages developed and written by the ITSO technical professionals;click
the Additional Materials button.Employees may access
MyNews
at
http://w3.ibm.com/
for
redbook.residency,and workshop announcements.
IBM Intranet for Employees

278
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
IBM Redbook Fax Order Form
Please send me the following:
We accept American Express,Diners,Eurocard,Master Card,and Visa.Payment by credit card not
available in all countries.Signature mandatory for credit card payment.
Title
Order Number
Quantity
First name
Last name
Company
Address
City
Postal code
Telephone number
Telefax number
VAT number
Invoice to customer number
Country
Credit card number
Credit card expiration date
Signature
Card issued to

© Copyright IBM Corp.1999
279
List of Abbreviations
API application programming interface
ASP Active Server Pages
ATM automated teller machine
AWT Abstract Windowing Toolkit
CGI Common Gateway Interface
CORBA Common Object Request Broker
Architecture
DBMS database management system
DLL dynamic link library
GUI graphical user interface
HTML Hypertext Markup Language
HTTP Hypertext Transfer Protocol
IBM International Business Machines
Corporation
IDE integrated development
environment
ITSO International Technical Support
Organization
JAR Java archive
JDBC Java Database Connectivity
JDK Java Developer’s Kit
JFC Java Foundation Classes
JSDK Java Servlet Development Kit
JSP Java Server Pages
JVM Java Virtual Machine
ODBC Open Database Connectivity
PIN personal identification number
RDBMS relational database management
system
RMI Remote Method Invocation
SQL structured query language
TCP/IP Transmission Control
Protocol/Internet Protocol
UOW unit of work
URL uniform resource locator
WWW World Wide Web

280
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server

© Copyright IBM Corp.1999
281
Index
A
access control list 238
alias 4
Apache 4,5
Application Server
see WebSphere
assembly stages 196
B
bean managed persistence 132,164
C
class path 129
Java 60
Persistence Builder 130
VisualAge for Java 48
WebSphere 49,129
WebSphere Studio 181
configuration
security 241
VisualAge for Java 10
WebSphere 6
WebSphere Studio 181
publishing 194
connection
data access beans 89
JDBC 83,112
connection manager 65,84
JDBC 69
sample 70
data access beans 98
DB2 77
Oracle 79
SQL Server 79
servlet interaction 66
connection pool 67,68,84
container managed persistence 132,133
D
data access beans 81
EJB persistence 167
output tailoring 96
sample
connection manager 98
DB2 86
Oracle 95
SQL Server 95
servlet sample 86
SQL types 266
Database Wizard 188,200
datastore
activation 126
DB2
application driver 44
Client Application Enabler 44
Command Window 24
Control Center 22
create database 23
data load 26
fixpack 22,247
installation 22
JDBC 44,82
applet server 45
drivers 26
network driver 45,56
sample
connection manager 77
data access beans 86
EJB 170
JDBC 56
Persistence Builder 112,127
SQLJ 251
WebSphere Studio 183
Software Developer’s Kit 250
SQL types 264
SQLJ 22,249
table creation 24
DBNavigator bean 82
debugging
VisualAge for Java 210
directory service 238,245
document root 4
E
EJB
container 132,133,166
Development Environment
VisualAge for Java 134
fields 138

282
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
generate code 143
group 134
mapping 142
methods 139
primary key 137
sample
DB2 133
servlet 174
server 132,133,143,144
test client 145
enterprise bean
application sample 160
deployment 149,177,266
servlet sample 153
test 143
test client 145
Enterprise Java Server 132
Enterprise JavaBeans
architecture 131
see also enterprise beans
Enterprise Update 10
entity bean 132
H
home interface 132,146
HTML
visual composition 122
HTTP Server 3,152,195
installation 4
I
import
schema 112
installation
DB2 UDB 22
HTTP Server 4
Oracle 27
SQL Server 33
VisualAge for Java 10
WebSphere 5
WebSphere Studio 181
Internet Explorer 3
iterator 254
ITSO
project 17
user ID 23
itso.wasdb.accessbean package 86
itso.wasdb.connaccess package 98
itso.wasdb.connmgr package 70
itso.wasdb.ejb package 134
itso.wasdb.ejb.client package 160
itso.wasdb.jdbc package 53
itso.wasdb.persist package 118
itso.wasdb.persist.gui package 121
itso.wasdb.persist.metadata package 121
itso.wasdb.persist.Services package 120
itso.wasdb.studio package 191
J
Java Database Connectivity
see JDBC
Java Foundation Classes
see Swing
JavaServer Pages
see JSP
JDBC
applet server 56
connection 83,112
manager 69
DB2 26,44
drivers 43
ODBC bridge 38,47,82
Oracle 27,32,46
programming examples 53
result set 85
sample
DB2 56
Oracle 57
run 60
SQL Server 58
test 60
servlet 61
simple application 53
SQL classes 82
SQL Server 38,47
SQLJ 248
statement types 85
URL 150
VisualAge for Java 10,48
WebSphere 7,49
JSP 12,180,187,193
servlet invocation 202
JSP Page Compile Generated Code 12
JTable 82

283
L
LDAP 238,240
Lotus Domino Go Webserver 5
M
Map Browser 111,116,142
Model Browser 111,115
N
NetObjects
BeanBuilder 180
Fusion 180
ScriptBuilder 180
Netscape
Directory Server 238
Enterprise Server 5
Navigator 3
O
ODBC 82
data source 38,47,58
Oracle
create tables 29
data load 31
installation 27
JDBC 27,32,46
OCI driver 46
Persistence Builder 111
sample
connection manager 79
data access beans 95
EJB 170
JDBC 57
Persistence Builder 127
WebSphere Studio 184
SQL types 264
thin client driver 46
user ID 27
P
Page Designer 202
persistence 110
Persistence Builder 109
EJB mapping 140
palette 123
result table 125
sample 112
DB2 127
Oracle 127
servlet 121
SQL Server 127
tools 110
primary key 137
project view 192,212
Q
Quick Start 10,82
R
realm 238,239
relational database
products 21
remote interface 133
repository 17
resource 240
result set
JDBC 85
SQLJ 256
Rollup 2 10
S
sample
code 15
database 21
Schema Browser 111,112,140
schema import 112
Secure Socket Layer 4
security 237
Select bean 81,82,88
select statement
data access beans 91
SQLJ 256
serialization 110
SQLJ 249
SERunner 12,97,106,158
class path 12
service classes 119
servlet
configuration file 208,210
configurationl file 11
data access beans 86
deployment 107,128
directory 11
EJB sample 153,174

284
IBM WebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
JDBC sample 61
launcher 63,106
location 7
Persistence Builder sample 121
run
VisualAge for Java 12,63,97
WebSphere 63
security 243
SQLJ sample 251
test 197
URL 7
visual composition 86
WebSphere Studio sample 188
Servlet Builder 86,122
session bean 132
SmartGuide
EJB 136
Persistence Builder code generation 118
SQL Assist 81,91
SQL
types 171,264
SQL Assist SmartGuide 81,91
SQL Server
create database 33
create tables 34
data load 35
installation 33
JDBC 38,47
Persistence Builder 111
sample
connection manager 79
data access beans 95
EJB 170
JDBC 58
Persistence Builder 127
WebSphere Studio 184
SQL types 264
user ID 34
SQL Wizard 183,198
SQLJ 22,247
deployment 258
iterator 254
select 256
translator 249,250
Swing 82
T
test client 145,152,174
U
UDB
see DB2
V
VisualAge for Java 9
class path 48
enterprise bean test 143
export 159
installation 10
JDBC 10,48
test 60
persistence builder 109
Professional Edition 180
Servlet Builder 86
servlet debugging 210
setup for EJB 134
SQLJ 248
W
WebSphere 3,5
administration 6,7,67,129
class path 129
EJB deployment 149,177
installation 5
Java engine 129
JDBC 7,49
servlet
deployment 107,128
service 8,152
SQLJ deployment 258
start/stop 8
Studio 179
Test Environment 10,63,158
class path 13
WebSphere Studio
Page Designer 202
publish 194
sample
DB2 183
Oracle 184
servlet 188
SQL Server 184
X
XML
servlet configuration file 11,193,210

© Copyright IBM Corp.1999
285
ITSO Redbook Evaluation
IBMWebSphere and VisualAge for Java Database Integration with DB2,Oracle,and SQL Server
SG24-5471-00
Your feedback is very important to help us maintain the quality of ITSO redbooks.Please complete
this questionnaire and return it using one of the following methods:
• Use the online evaluation form found at http://www.redbooks.ibm.com/
• Fax this form to:USA International Access Code + 1 914 432 8264
• Send your comments in an Internet note to redbook@us.ibm.com
Which of the following best describes you?
_ Customer _ Business Partner _ Solution Developer _ IBM employee
_ None of the above
Please rate your overall satisfaction with this book using the scale:
(1 = very good,2 = good,3 = average,4 = poor,5 = very poor)
Overall Satisfaction __________
Please answer the following questions:
Was this redbook published in time for your needs?Yes___ No___
If no,please explain:
What other redbooks would you like to see published?
Comments/Suggestions:(THANK YOU FOR YOUR FEEDBACK!)

Printed in the U.S.A.
SG24-5471-00
IBMWebSphereandVisualAgeforJavaDatabaseIntegrationwithDB2,Oracle,andSQLServer
SG24-5471-00
First page image
We are pleased to offer a download of this document free of charge.
Files available for download:
  • a representative PDF of the primary file (contains all the relevant information for most users)
To obtain the file, please enter the "captcha" below and click the Download button.
Avoid entering CAPTCHAs! Sign In or Create a Free Account.

Challenge image
  • Please enter letters and numbers only; no spaces.
  • Cannot read this one? Click the image.
  • Difficulty with captchas? Contact us with the URL of this page and we will email it to you.