Sun Microsystems, Inc.
MS BRM01-209
500 Eldorado Blvd
Broomfield, Colorado 80021
U.S.A.
®
JavaProgrammingLanguage
Workshop
Revision B.2, February 2000
SL-285
StudentGuide
Please
Recycle
Copyright © 2000 Sun Microsystems, Inc. 901 San Antonio Road, Palo Alto, California 94303, U.S.A. All rights reserved.
This product or document is protected by copyright and distributed under licenses restricting its use, copying,
distribution, and decompilation. No part of this product or document may be reproduced in any form by any means
without prior written authorization of Sun and its licensors, if any.
Third-party software, including font technology, is copyrighted and licensed from Sun suppliers.
Sun, Sun Microsystems, the Sun Logo, Solaris, OpenWindows, Java, JavaSoft, and HotJava are trademarks or registered
trademarks of Sun Microsystems, Inc. in the U.S. and other countries. All SPARC trademarks are used under license and
are trademarks or registered trademarks of SPARC International, Inc. in the U.S. and other countries. Products bearing
SPARC trademarks are based upon an architecture developed by Sun Microsystems, Inc.
X Window System is a trademark of X Consortium, Inc.
The OPEN LOOK and Sun Graphical User Interface was developed by Sun Microsystems, Inc. for its users and licensees.
Sun acknowledges the pioneering efforts of Xerox in researching and developing the concept of visual or graphical user
interfaces for the computer industry. Sun holds a non-exclusive license from Xerox to the Xerox Graphical User Interface,
which license also covers Sun’s licensees who implement OPEN LOOK GUIs and otherwise comply with Sun’s written
license agreements.
U.S. Government approval required when exporting the product.
RESTRICTED RIGHTS: Use, duplication, or disclosure by the U.S. Govt is subject to restrictions of FAR 52.227-14(g)
(2)(6/87) and FAR 52.227-19(6/87), or DFAR 252.227-7015 (b)(6/95) and DFAR 227.7202-3(a).
DOCUMENTATION IS PROVIDED "AS IS" AND ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS,
AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH
DISCLAIMERS ARE HELD TO BE LEGALLAY INVALID.
iii
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services February 2000, Rev. B.2
Contents
About This Course.......................................................................................xi
Course Overview ............................................................................... xii
Course Map........................................................................................ xiv
Module-by-Module Overview ......................................................... xv
Course Objectives............................................................................ xvii
Skills Gained by Module............................................................... xviii
Guidelines for Module Pacing ........................................................ xix
Topics Not Covered........................................................................... xx
How Prepared Are You?.................................................................. xxi
Introductions .................................................................................. xxiii
How to Use Course Materials ...................................................... xxiv
Course Icons and Typographical Conventions ......................... xxvi
Typographical Conventions ................................................ xxvii
Java Application Design...........................................................................1-1
Relevance............................................................................................ 1-2
Software Development Cycle.......................................................... 1-3
Analysis ......................................................................................1-4
Design .........................................................................................1-4
Software Development Cycle.......................................................... 1-5
Implementation .........................................................................1-5
Testing ........................................................................................1-5
Revisions ....................................................................................1-6
Developing Java Programs .............................................................. 1-7
Java Program Types..................................................................1-8
Class Definition .........................................................................1-9
Java API Design.........................................................................1-9
Complete API Declarations ...................................................1-10
Coding Conventions...............................................................1-11
Ensuring Project Success........................................................1-14
The BrokerTool Program ............................................................... 1-15
Problem Definition..................................................................1-15
Legacy System .........................................................................1-16
What You Need to Do ............................................................1-17
iv Java Programming Language Workshop
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services February 2000, Rev. B.2
Specifications of the Legacy Server ......................................1-18
Defining the BrokerTool Program........................................1-20
Project Constraints..................................................................1-23
Notes ................................................................................................. 1-24
Exercise: The BrokerTool Program Initial Design...................... 1-25
Preparation...............................................................................1-25
Tasks .........................................................................................1-25
Notes ................................................................................................. 1-26
Exercise Summary........................................................................... 1-27
Check Your Progress ...................................................................... 1-28
Think Beyond .................................................................................. 1-29
Managing Database Queries....................................................................2-1
Relevance............................................................................................ 2-2
Relational Database Management Systems................................... 2-3
Relational Models ............................................................................. 2-5
Tables ..........................................................................................2-5
Data Independence ...................................................................2-6
Structured Query Language (SQL) ........................................2-7
Introducing mSQL ............................................................................ 2-9
Implementing mSQL ..............................................................2-11
mSQL Tools..............................................................................2-12
mSQL Commands........................................................................... 2-14
The SELECT Statement............................................................2-15
The INSERT Statement............................................................2-18
The DELETE Statement............................................................2-18
The UPDATE Statement............................................................2-18
Exercise: mSQL Database Queries................................................ 2-20
Preparation...............................................................................2-20
Tasks .........................................................................................2-20
Exercise Summary........................................................................... 2-22
Check Your Progress ...................................................................... 2-23
Think Beyond .................................................................................. 2-24
Introduction to JDBC ................................................................................3-1
Relevance............................................................................................ 3-2
Introducing the JDBC Interface....................................................... 3-3
ANSI SQL-2 Conformance.......................................................3-4
JDBC Driver Interface...............................................................3-6
MsqlDriver – A Real-World JDBC Driver............................3-8
Connecting Through the JDBC Interface..................................... 3-10
The java.sql Package...........................................................3-10
The JDBC Flow ................................................................................ 3-11
JDBC Example ................................................................................. 3-12
Explicitly Creating an Instance of a JDBC Driver ..............3-14
Registering a Driver................................................................3-17
Specifying a Database.............................................................3-18
v
Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services February 2000, Rev. B.2
Opening a Database Connection ..........................................3-19
Using Database Resolution....................................................3-20
JDBC Statements ............................................................................. 3-21
Submitting a Query ................................................................3-21
Receiving Results ....................................................................3-22
Using the getXXX Methods ................................................3-24
Working With Prepared Statements ....................................3-25
An Example of Using a Prepared Statement.......................3-26
Using the setXXX Methods..................................................3-27
Creating Callable Statements ................................................3-28
An Example of Using a Callable Statement ........................3-29
Mapping SQL Data Types Into Java Data Types........................ 3-30
ABCStock – APIs to Access the StockMarket Database........... 3-31
The StockMarket Database ..................................................3-31
The Database Utility Classes .................................................3-31
ABCStock – Support Classes for the StockMarket Database .. 3-33
CustomerRec.java ................................................................3-33
SharesRec.java.....................................................................3-35
StockRec.java.......................................................................3-37
ABCStock – Database API ............................................................. 3-39
Methods....................................................................................3-40
Exercise: Implementing A Database Class Wrapper ................. 3-45
Preparation...............................................................................3-45
Tasks .........................................................................................3-45
Notes ................................................................................................. 3-46
Exercise Summary........................................................................... 3-47
Check Your Progress ...................................................................... 3-48
Think Beyond .................................................................................. 3-49
Building GUIs.............................................................................................4-1
Relevance............................................................................................ 4-2
GUI Design Principles...................................................................... 4-3
Basic Principles..........................................................................4-3
Subjective Versus Objective.....................................................4-4
Assess Your Audience..............................................................4-4
Clarify User Tasks.....................................................................4-5
Keep It Simple ...........................................................................4-6
Maintain Consistency...............................................................4-7
Style Is Everything..................................................................4-10
Classes and Object-Oriented Design............................................ 4-12
AWT and Swing .............................................................................. 4-15
The java.awt and javax.swing Packages................................ 4-17
Building Blocks................................................................................ 4-18
Containers ................................................................................4-18
AWT..........................................................................................4-19
Swing ........................................................................................4-20