Vous êtes sur la page 1sur 14

Oracle9i: Java Programming

Volume 1 - Student Guide

D16390GC20
Edition 2.0
June 2003
D38327

Authors

Copyright 2003, Oracle. All rights reserved.

Jeff Gallus
Glenn Stokol

This documentation contains proprietary information of Oracle Corporation. It is


provided under a license agreement containing restrictions on use and disclosure and
is also protected by copyright law. Reverse engineering of the software is prohibited.
If this documentation is delivered to a U.S. Government Agency of the Department of
Defense, then it is delivered with Restricted Rights and the following legend is
applicable:

Technical Contributors
and Reviewers
Anna Atkinson
William Bates
Kenneth Cooper
Craig Cummings
Michael Curtis
Patrice Daux
Mangesh Koli
Glenn Maslen
Monica Motley
Dan Pillay
Blaise Ribet
Gary Williams
Publisher
Sujatha Nagendra

Restricted Rights Legend


Use, duplication or disclosure by the Government is subject to restrictions for
commercial computer software and shall be deemed to be Restricted Rights software
under Federal law, as set forth in subparagraph (c)(1)(ii) of DFARS 252.227-7013,
Rights in Technical Data and Computer Software (October 1988).
This material or any portion of it may not be copied in any form or by any means
without the express prior written permission of Oracle Corporation. Any other copying
is a violation of copyright law and may result in civil and/or criminal penalties.
If this documentation is delivered to a U.S. Government Agency not within the
Department of Defense, then it is delivered with Restricted Rights, as defined in
FAR 52.227-14, Rights in Data-General, including Alternate III (June 1987).
The information in this document is subject to change without notice. If you find any
problems in the documentation, please report them in writing to Education Products,
Oracle Corporation, 500 Oracle Parkway, Box SB-6, Redwood Shores, CA 94065.
Oracle Corporation does not warrant that this document is error-free.
All references to Oracle and Oracle products are trademarks or registered trademarks
of Oracle Corporation.
All other products or company names are used for identification purposes only, and
may be trademarks of their respective owners.

Contents

Preface
Introduction
Objectives I-2
Course Overview I-3
Java Developer Courses I-4
1

Introducing the Java and Oracle Platforms


Objectives 1-2
What Is Java? 1-3
Key Benefits of Java 1-4
An Object-Oriented Approach 1-6
Platform Independence 1-7
Using Java with Enterprise Internet Computing 1-8
Using the Java Virtual Machine 1-10
How Does the JVM Work? 1-12
Benefits of Just-In-Time (JIT) Compilers 1-14
Implementing Security in the Java Environment 1-16
What Are Java Applications? 1-18
Using Java with Oracle9i 1-19
The Java Software Development Kit 1-20
Using the Appropriate Development Kit 1-21
Integrated Development Environment 1-22
Exploring the JDeveloper Environment 1-23
Oracle9i Products 1-25
Oracle9iDS Application Development 1-26
Oracle9iDS Business Intelligence 1-27
Summary 1-28

Defining Object-Oriented Principles


Objectives 2-2
What Is Modeling? 2-4
What Are Classes and Objects? 2-5
An Objects Attributes Maintain Its State 2-6
Objects Have Behavior 2-8
Objects Are Modeled as Abstractions 2-9
Defining Object Composition 2-11
The Donut Diagram 2-13
Guided Practice: Spot the Operations and Attributes 2-14
Collaborating Objects 2-15

iii

Objects Interact Through Messages 2-16


What Is a Class? 2-17
How Do You Identify a Class? 2-18
Comparing Classes and Objects 2-19
What Is Encapsulation? 2-21
What Is Inheritance? 2-22
Using the Is-a-Kind-of Relationship 2-23
What Is Polymorphism? 2-24
Order Entry UML Diagram 2-26
Summary 2-27
Practice 2 Overview 2-28
Order Entry System Partial UML Class Model 2-31
3 Basic Java Syntax and Coding Conventions
Objectives 3-2
Examining Toolkit Components 3-4
Exploring Packages in J2SE/J2EE 3-5
Documenting Using the J2SE 3-6
Contents of a Java Source 3-7
Establishing Naming Conventions 3-8
More on Naming Conventions 3-10
Defining a Class 3-12
Rental Class Example 3-13
Creating Code Blocks 3-15
Defining Java Methods 3-16
Examples of a Method 3-17
Styles for Declaring Variables 3-18
Examples of Variables in the Context of a Method 3-19
Rules for Creating Statements 3-20
Compiling and Running a Java Application 3-21
Examining the CLASSPATH variable 3-22
CLASSPATH Example 3-23
Summary 3-24
Practice 3 Overview 3-25
4 Exploring Primitive Data Types and Operators
Objectives 4-2
Reserved Keywords 4-4
Variable Types 4-5

iv

Primitive Data Types 4-7


What Are Variables? 4-9
Declaring Variables 4-10
Local Variables 4-11
Defining Variable Names 4-12
What Are Numeric Literals? 4-13
What Are the Nonnumeric Literals? 4-15
Guided Practice: Declaring Variables 4-17
What Are Operators? 4-19
Categorizing Operators 4-20
Using the Assignment Operator 4-21
Working with Arithmetic Operators 4-22
More on Arithmetic Operators 4-23
Examining Conversions and Casts 4-24
Incrementing and Decrementing Values 4-26
Relational and Equality Operators 4-27
Using the Conditional Operator (?:) 4-28
Using Logical Operators 4-29
Compound Assignments Operators 4-30
Operator Precedence 4-31
More on Operator Precedence 4-32
Concatenating Strings 4-33
Summary 4-34
Practice 4 Overview 4-35
5 Controlling Program Flow
Objectives 5-2
Categorizing Basic Flow Control Types 5-4
Using Flow Control in Java 5-6
Using the if Statement 5-7
Nesting if Statements 5-8
Guided Practice: Spot the Mistakes 5-9
Defining the switch Statement 5-10
More About the switch Statement 5-12
Looping in Java 5-13
Using the while Loop 5-14
Using the dowhile Loop 5-15
Using the for Loop 5-16
More About the for Loop 5-17
Guided Practice: Spot the Mistakes 5-18

Implementing the break Statement 5-19


Comparing Labeled break and continue Statements 5-20
Summary 5-21
Practice 5 Overview 5-22
6 Building Applications with Oracle9i JDeveloper
Objectives 6-2
What Is Oracle9i JDeveloper? 6-3
Exploring the JDeveloper Environment 6-4
Examining Workspaces 6-5
What Are Projects? 6-7
Creating JDeveloper Items 6-8
Creating an Application Project 6-9
Specifying Project Details 6-10
Selecting Additional Libraries 6-11
Creating an Application 6-12
Looking at the Directory Structure 6-13
Exploring the Skeleton Java Application 6-14
Finding Methods and Fields 6-15
Supporting Code Development with the Profiler and Code Coach 6-16
Using the Help System 6-17
Obtaining Help on a Topic 6-18
Oracle9i JDeveloper Debugger 6-19
Setting Breakpoints 6-21
Using the Debugger Windows 6-23
Stepping Through a Program 6-24
Watching Data and Variables 6-25
Adding a New J2SE 6-26
Summary 6-27
Practice 6 Overview 6-28
7 Creating Classes and Objects
Objectives 7-2
Using Java Classes 7-4
Comparing Classes and Objects 7-5
Creating Objects 7-6
Using the new Operator 7-7
Comparing Primitives and Objects 7-8
Using the null Reference 7-9
Assigning References 7-10
Declaring Instance Variables 7-11

vi

Accessing public Instance Variables 7-12


Guided Practice: Creating and Using Objects 7-13
Defining Methods 7-14
Calling a Method 7-15
Specifying Method Arguments: Examples 7-16
Returning a Value from a Method 7-17
Calling Instance Methods 7-18
Applying Encapsulation in Java 7-19
Passing Primitives into Methods 7-20
Passing Object References into Methods 7-21
What Are Class Variables? 7-22
Initializing Class Variables 7-23
What Are Class Methods? 7-24
Guided Practice: Class Methods or Instance Methods 7-25
Examples in Java 7-26
Creating Classes Using the Class Editor 7-27
What Are Java Packages? 7-28
Grouping Classes in a Package 7-29
Setting the CLASSPATH with Packages 7-30
Access Modifiers 7-31
Summary 7-32
Practice 7 Overview 7-33
8 Object Life Cycle and Inner Classes
Objectives 8-2
Overloading Methods 8-4
Using the this Reference 8-5
Initializing Instance Variables 8-6
What Are Constructors? 8-7
Defining and Overloading Constructors 8-8
Sharing Code Between Constructors 8-9
final Variables, Methods, and Classes 8-10
Reclaiming Memory 8-11
Using the finalize() Method 8-12
Controlling Garbage Collection 8-13
Defining Removable Objects 8-14
Delaying Memory Collection 8-15
What Are Inner Classes? 8-16
Defining Static Inner Classes 8-17

vii

Defining Member Inner Classes 8-18


Defining Local Inner Classes 8-19
Defining Anonymous Inner Classes 8-20
Summary 8-21
Practice 8 Overview 8-22
9 Using Strings, String Buffer, Wrapper, and Text-Formatting Classes
Objectives 9-2
What Is a String? 9-3
Creating a String 9-4
Concatenating Strings 9-5
Performing Operations on Strings 9-6
Performing More Operations on Strings 9-7
Comparing String Objects 9-8
Producing Strings from Other Objects 9-9
Producing Strings from Primitives 9-10
Producing Primitives from Strings 9-11
Wrapper Class Conversion Methods 9-12
Changing the Contents of a String 9-13
Formatting Classes 9-14
Using the SimpleDateFormat Class 9-15
Using DecimalFormat 9-16
Guided Practice 9-17
Using Regular Expressions 9-19
About System.out.println 9-20
About OutputStream and PrintStream 9-21
What Is Object Serialization? 9-22
Serialization Streams, Interfaces,and Modifiers 9-23
Summary 9-24
Practice 9 Overview 9-25
10 Reusing Code with Inheritance and Polymorphism
Objectives 10-2
Key Object-Oriented Components 10-3
Example of Inheritance 10-4
Specifying Inheritance in Java 10-5
Defining Inheritance by Using Oracle9i JDeveloper 10-6
What Does a Subclass Object Look Like? 10-7
Default Initialization 10-8

viii

The super Reference 10-9


The super Reference Example 10-10
Using Superclass Constructors 10-11
Specifying Additional Methods 10-13
Overriding Superclass Methods 10-15
Invoking Superclass Methods 10-17
Example of Polymorphism in Java 10-19
Treating a Subclass as Its Superclass 10-20
Browsing Superclass References by Using Oracle9i JDeveloper 10-21
Acme Video and Polymorphism 10-22
Using Polymorphism for Acme Video 10-23
Using the instanceof Operator 10-25
Limiting Methods and Classes with final 10-26
Ensuring Genuine Inheritance 10-27
Summary 10-28
Practice 10 Overview 10-29
11 Using Arrays and Collections
Objectives 11-2
What Is an Array? 11-3
Creating an Array of Primitives 11-4
Declaring an Array of Primitives 11-5
Creating an Array Object for an Array of Primitives 11-6
Initializing Array Elements 11-8
Creating an Array of Object References 11-9
Initializing the Objects in the Array 11-10
Using an Array of Object References 11-11
Arrays and Exceptions 11-12
Multidimensional Arrays 11-13
main()Revisited 11-14
Working with Variable-Length Structures 11-15
Modifying a Vector 11-16
Accessing a Vector 11-17
Java Collections Framework 11-18
Collections Framework Components 11-20
Using ArrayList and Hashtable 11-21
Using Enumerators to Implement Arrays 11-22
Summary 11-23
Practice 11 Overview 11-24

ix

12 Structuring Code by Using Abstract Classes and Interfaces


Objectives 12-2
Defining Abstract Classes 12-3
Creating Abstract Classes 12-4
What Are Abstract Methods? 12-5
Defining Abstract Methods 12-7
Defining and Using Interfaces 12-8
Examples of Interfaces 12-9
Creating Interfaces 12-10
Implementing Interfaces 12-12
Sort: A Real-World Example 12-13
Overview of the Classes 12-14
How the Sort Works 12-15
The Sortable Interface 12-16
The Sort Class 12-17
The Movie Class 12-18
Using the Sort 12-19
Using instanceof with Interfaces 12-20
Summary 12-21
Practice 12 Overview 12-22
13 Throwing and Catching Exceptions
Objectives 13-2
What Is an Exception? 13-3
How Does Java Handle Exceptions? 13-4
Advantages of Java Exceptions: Separating Error Handling Code 13-5
Advantages of Java Exceptions: Passing Errors Up the Call Stack 13-7
Advantages of Java Exceptions: Exceptions Cannot Be Ignored 13-8
Checked Exceptions, Unchecked Exceptions, and Errors 13-9
What to Do with an Exception 13-10
Catching and Handling Exceptions 13-11
Catching a Single Exception 13-12
Catching Multiple Exceptions 13-13
Cleaning Up with a finally Block 13-14
Catching and Handling Exceptions: Guided Practice 13-15
Allowing an Exception Pass to the Calling Method 13-17
Throwing Exceptions 13-18
Creating Exceptions 13-19
Catching an Exception and Throwing a Different Exception 13-20

Summary 13-21
Practice 13 Overview 13-2
14 User Interface Design: Swing Basics Planning the Application Layout
Objectives 14-2
Running Java UI Applications 14-3
AWT, Swing, and JFC 14-4
Swing Features 14-6
Lightweight or Heavyweight Components? 14-8
Planning the UI Layout 14-9
The Containment Hierarchy 14-10
Top-Level Containers 14-12
Intermediate Containers 14-14
Atomic Components 14-15
Layout Management Overview 14-16
Border Layout 14-18
GridBag Layout 14-19
GridBag Constraints 14-20
Using Layout Managers 14-22
Combining Layout Managers 14-24
Using Frames or Dialogs 14-25
Using JPanel Containers 14-27
Adding Borders to Components 14-29
Using Internal Frames 14-30
Swing Text Controls 14-32
Adding Components with Oracle9i JDeveloper 14-33
Creating a Frame 14-34
Adding Components 14-35
Setting Pluggable Look and Feel 14-37
Summary 14-39
Practice 14 Overview 14-40
15 Adding User Interface Components and Event Handling
Objectives 15-2
Swing Components 15-3
Swing Components in JDeveloper 15-5
Invoking the UI Editor 15-7
How to Add a Component to a Form 15-8
Edit the Properties of a Component 15-9
Code Generated by JDeveloper 15-10
Creating a Menu 15-12

xi

Using JDeveloper Menu Editor 15-13


Practice 15-1 Overview 15-14
Sample Order Entry Application 15-15
Java Event Handling Model 15-20
Event Handling Code Basics 15-21
Event Handling Process: Registration 15-22
Event Handling Process: The Event Occurs 15-24
Event Handling Process: Running the Event Handler 15-25
Implementing an Event Listener as an Inner Class 15-26
Using Adapter Classes for Listeners 15-27
Swing Model View Controller Architecture 15-28
Basic Text Component Methods 15-31
Basic JList Component Methods 15-32
What Events Can a Component Generate? 15-33
How to Define an Event Handler in Jdeveloper 15-34
Default Event Handling Code Style Generated by Jdeveloper 15-35
Completing the Event Handler Method 15-36
Example Menu and Code 15-37
Summary 15-38
Practice 15-2 Overview 15-39
16 Building Components with JavaBeans
Objectives 16-2
What Defines JavaBeans? 16-3
Architecture Rules for Reuse 16-4
Engineering for a Black Box Environment 16-5
Managing Bean Properties 16-6
Exposing Properties and Methods 16-7
Event Handling with JavaBeans 16-8
JavaBeans at Design Time 16-9
Introspection with JavaBeans 16-10
JavaBeans at Run Time 16-11
Differentiating JavaBeans, Java Classes, and Widgets 16-12
Building and Using a JavaBean in JDeveloper: Overview 16-13
1. Develop a JavaBean 16-14
Define the JavaBean 16-15
Add a Simple Property 16-16
Add Events to a Bean 16-17
2. Store the JavaBean in an Archive File 16-18
3. Create a JDeveloper Library Identifying the Archive 16-19

xii

4. Install the JavaBean in JDevelopers Component Palette, via Its Library Name
16-20
5. Develop an Application Using the JavaBean 16-21
Summary 16-22
Practice 16 Overview 16-23
17 Using JDBC to Access the Database
Objectives 17-2
Java, J2EE, and Oracle9i 17-3
Connecting to a Database with Java 17-4
What Is JDBC? 17-5
Preparing the Environment 17-6
Steps for Using JDBC to Execute SQL Statements 17-8
Step 1: Registering the Driver 17-9
Connecting to the Database 17-10
Oracle JDBC Drivers: Thin Client Driver 17-11
Oracle JDBC Drivers: OCI Client Drivers 17-12
Choosing the Right Driver 17-13
Step 2: Getting a Database Connection 17-14
About JDBC URLs 17-15
JDBC URLs with Oracle Drivers 17-16
Step 3: Creating a Statement 17-17
Step 4: Executing SQL Statements 17-18
Using the Statement Interface 17-19
Step 4a: Executing a Query 17-20
The ResultSet Object 17-21
4b: Submitting DML Statements 17-22
4b: Submitting DDL Statements 17-23
Step 5: Processing the Query Results 17-24
A Basic Query Example 17-25
Step 6: Closing Connections 17-26
Mapping Database Types to Java Types 17-27
Handling an Unknown SQL Statement 17-29
Handling Exceptions 17-30
Managing Transactions 17-31
The PreparedStatement Object 17-32
How to Create a PreparedStatement 17-33
How to Execute a PreparedStatement 17-34
Maximize Database Access 17-35
Connection Pooling 17-36
Summary 17-39
Practice 17 Overview 17-40

xiii

18 Deploying Applications Using Java Web Start


Objectives 18-2
What Is Java Web Start? 18-3
Running a Web Start Application 18-4
Advantages of Web Start 18-5
Examining the JNLP File 18-6
Deploying Applications with JDeveloper 18-7
Creating the Deployment Profile File 18-8
Saving the Deployment Profile 18-9
Selecting Files to Deploy 18-10
Making an Executable .jar File 18-11
Creating and Deploying the Archive File 18-12
Using JDeveloper to Deploy an Application to Java Web Start 18-13
Step 1: Generate Deployment Profiles and Archive Application 18-14
Step 2a: Start OC4J and Create a Connection 18-15
Step 2b: Creating a Connection 18-16
Step 3: Use Web Start Wizard to Create a JNLP File 18-17
Step 4: Archive and Deploy the Application to the OC4J Server 18-18
Summary 18-19
Practice 18 Overview 18-20
Appendix A: Practice Solutions
Appendix B: Java Language Quick-Reference Guide
Appendix C: Order Entry Solution

xiv

Vous aimerez peut-être aussi