Introduction...1
0.1 About This Book..2
0.2 How to Use This Book ....3
0.3 How This Book Is Organized...3
0.3.1 Book I: Java Basics ...3
0.3.2 Book II: Programming Basics ....3
0.3.3 Book III: Object-Oriented Programming ....4
0.3.4 Book IV: Strings, Arrays, and Collections..4
0.3.5 Book V: Programming Techniques ....4
0.3.6 Book VI: Swing ....4
0.3.7 Book VII: Web Programming ..4
0.3.8 Book VIII: File and Database Programming..4
0.3.9 Book IX: Fun and Games...5
0.4 This book’s Web site ...5
0.5 Icons Used in This Book....5
0.6 Where to Go from Here...6
Book I: Java Basics ....7
1.1 Chapter 1: Welcome to Java . . 9
1.1.1 What Is Java, and Why Is It So Great?...9
1.1.1.1 Platform independence ..10
1.1.1.2 Object orientation ..11
1.1.1.3 The Java API...12
1.1.1.4 The Internet....12
1.1.2 Comparing Java to Other Languages..13
1.1.3 Important Features of the Java Language...15
1.1.3.1 Type checking...15
1.1.3.2 Automatic memory management....17
1.1.3.3 Exception handling ...17
1.1.4 On the Downside: Java’s Weaknesses ...18
1.1.5 Java Version Insanity ....19
1.1.6 What’s in a Name? ...20
1.2 Chapter 2: Installing and Using Java Tools . . . .21
1.2.1 Downloading and Installing the Java Development Kit..21
1.2.1.1 Downloading the JDK...22
1.2.1.2 Installing the JDK....23
1.2.1.3 Perusing the JDK folders ...23
1.2.1.4 Setting the path ...24
1.2.2 Using Java’s Command-Line Tools...25
1.2.2.1 Compiling a program ...26
1.2.2.2 Compiling more than one file..26
1.2.2.3 Using Java compiler options...27
1.2.2.4 Running a Java program....29
1.2.2.5 Using the javap command....31
1.2.2.6 Other Java command-line tools....32
1.2.3 Using Java Documentation....32
1.2.3.1 JS2E API Docs....33
1.2.3.2 Java Language Specification ...34
1.3 Chapter 3: Working with TextPad . . . . .35
1.3.1 Downloading and Installing TextPad ..35
1.3.2 Editing Source Files....36
1.3.3 Compiling a Program ....38
1.3.4 Running a Java Program..40
1.3.5 Running an Applet...41
1.4 Chapter 4: Using Eclipse . . . .43
1.4.1 Getting Some Perspective on Eclipse ....44
1.4.2 Understanding Projects...46
1.4.3 Creating a Simple Project ...47
1.4.4 Adding a Class File ..52
1.4.5 Running a Program ....56
1.4.6 Debugging a Java Program....57
1.4.6.1 Stepping through your programs....57
1.4.6.2 Examining variables ..59
1.4.6.3 Setting breakpoints ...60
1.4.7 Refactoring Your Code..61
Book II: Programming Basics ...63
2.1 Chapter 1: Java Programming Basics . . .65
2.1.1 Looking At the Infamous Hello, World! Program....65
2.1.2 Dealing with Keywords....68
2.1.3 Working with Statements ...70
2.1.3.1 Types of statements..71
2.1.3.2 White space....71
2.1.4 Working with Blocks ..72
2.1.5 Creating Identifiers ....73
2.1.6 Crafting Comments ....74
2.1.6.1 End-of-line comments ..74
2.1.6.2 Traditional comments..75
2.1.6.3 JavaDoc comments ...76
2.1.7 Introducing Object-Oriented Programming...76
2.1.7.1 Understanding classes and objects ...76
2.1.7.2 Understanding static methods ..76
2.1.7.3 Creating an object from a class ....77
2.1.7.4 A program that uses an object ..78
2.1.7.5 So what’s the difference?...80
2.1.8 Importing Java API Classes...81
2.2 Chapter 2: Working with Variables and Data Types . . . . .83
2.2.1 Declaring Variables ....83
2.2.1.1 Declaring two or more variables in one statement..84
2.2.1.2 Declaring class variables...84
2.2.1.3 Declaring instance variables...85
2.2.1.4 Declaring local variables ...86
2.2.2 Initializing Variables...88
2.2.2.1 Initializing variables with assignment statements ...88
2.2.2.2 Initializing variables with initializers ....89
2.2.3 Using Final Variables (Or Constants)..89
2.2.4 Working with Primitive Data Types ....90
2.2.4.1 Integer types ..91
2.2.4.2 Floating-point types ..93
2.2.4.3 The char type....94
2.2.4.4 The boolean type....95
2.2.4.5 Wrapper classes ..96
2.2.5 Using Reference Types ....96
2.2.6 Working with Strings..98
2.2.6.1 Declaring and initializing strings..98
2.2.6.2 Combining strings ..99
2.2.6.3 Converting primitives to strings ..99
2.2.6.4 Converting strings to primitives ...100
2.2.7 Converting and Casting Numeric Data ...101
2.2.7.1 Automatic conversions....101
2.2.7.2 Type casting....102
2.2.8 Understanding Scope..102
2.2.9 Shadowing Variables...104
2.2.10 Printing Data with System.out..105
2.2.10.1 Standard input and output streams....105
2.2.10.2 Using System.out and System.err..107
2.2.11 Getting Input with the Scanner Class ..107
2.2.11.1 Importing the Scanner class ....108
2.2.11.2 Declaring and creating a Scanner object..109
2.2.11.3Getting input ...109
2.2.12 Getting Input with the JOptionPane Class ...111
2.3 Chapter 3: Working with Numbers and Expressions . . .113
2.3.1 Working with Arithmetic Operators ....113
2.3.2 Dividing Integers ...116
2.3.3 Combining Operators ....118
2.3.4 Using the Unary Plus and Minus Operators ...119
2.3.5 Using Increment and Decrement Operators...120
2.3.6 Using the Assignment Operator..122
2.3.7 Using Compound Assignment Operators ....123
2.3.8 Using the Math Class ..124
2.3.8.1 Constants of the Math class..125
2.3.8.2 Mathematical functions...126
2.3.8.3 Creating random numbers ....129
2.3.8.4 Rounding functions ....131
2.3.9 Formatting Numbers...133
2.3.10 Weird Things about Java Math ....136
2.3.10.1 Integer overflow....136
2.3.10.2 Floating-point weirdness ....137
2.3.10.3 Dividing by zero....138
2.4 Chapter 4: Making Choices . . .141
2.4.1 Using Simple Boolean Expressions ...141
2.4.2 Using If Statements ..144
2.4.2.1 Simple if statements...144
2.4.2.2 if-else statements..146
2.4.2.3 Nested if statements ..147
2.4.2.4 else-if statements..151
2.4.3 Mr. Spock’s Favorite Operators (The Logical Ones, of Course)....153
2.4.3.1 Using the ! operator ...153
2.4.3.2 Using the & and && operators....154
2.4.3.3 Using the | and || operators...155
2.4.3.4 Using the ^ operator ..156
2.4.3.5 Combining logical operators....157
2.4.4 Using the Conditional Operator ..159
2.4.5 Comparing Strings....159
2.5 Chapter 5: Going Around in Circles (Or, Using Loops) . . 161
2.5.1 Your Basic while Loop ...162
2.5.1.1 The while statement ..162
2.5.1.2 A counting loop ....162
2.5.2 Breaking Out of a Loop..163
2.5.3 Looping Forever ....164
2.5.3.1 Letting the user decide when to quit..165
2.5.3.2 Another way to let the user decide..166
2.5.4 Using the continue Statement ..167
2.5.5 do-while Loops ...168
2.5.6 Validating Input from the User ....170
2.5.7 The Famous for Loop..173
2.5.7.1 The formal format of the for loop ....173
2.5.7.2 Scoping out the counter variable..176
2.5.7.3 Counting even numbers ..177
2.5.7.4 Counting backwards ..177
2.5.7.5 for loops without bodies ....178
2.5.7.6 Ganging up your expressions ..179
2.5.7.7 Omitting expressions....181
2.5.7.8 Breaking and continuing your for loops...181
2.5.8 Nesting Your Loops..182
2.5.8.1 A simple nested for loop ....182
2.5.8.2 A guessing game ...183
2.6 Chapter 6: Pulling a Switcheroo . . . . .187
2.6.1 else-if Monstrosities....187
2.6.2 A Better Version of the Voter Machine Error Decoder Program...189
2.6.3 Using the switch Statement ...190
2.6.4 A Boring Business Example Complete with Flowchart ...191
2.6.5 Putting if Statements Inside switch Statements...193
2.6.6 Creating Character Cases....194
2.6.7 Falling through the Cracks..195
2.7 Chapter 7: Adding Some Methods to Your Madness . . . .199
2.7.1 The Joy of Methods ....199
2.7.2 The Basics of Making Methods....200
2.7.2.1 An example...201
2.7.2.2 Another example ..202
2.7.3 Methods That Return Values ....204
2.7.3.1 Declaring the method’s return type....205
2.7.3.2 Using the return statement to return the value...205
2.7.3.3 Using a method that returns a type ....206
2.7.3.4 You gotta have a proper return statement..206
2.7.3.5 Another version of the guessing game program ....208
2.7.4 Using Methods That Take Parameters ...211
2.7.4.1 Declaring parameters ...211
2.7.4.2 Scoping out parameters ..212
2.7.4.3 Understanding pass-by-value...213
2.7.4.4 Yet another example of the guessing game program...214
2.8 Chapter 8: Handling Exceptions . . . . .217
2.8.1 Understanding Exceptions..217
2.8.1.1 Witnessing an exception..219
2.8.1.2 Finding the culprit...219
2.8.2 Catching Exceptions ...220
2.8.2.1 A simple example ....221
2.8.2.2 Another example ..222
2.8.3 Handling Exceptions with a Pre-emptive Strike ...223
2.8.4 Catching All Exceptions at Once ....225
2.8.5 Displaying the Exception Message ...226
2.8.6 Using a finally Block....227
2.8.7 Handling Checked Exceptions..229
2.8.7.1 The catch-or-throw compiler error..229
2.8.7.2 Catching FileNotFoundException..230
2.8.7.3 Throwing the FileNotFoundException...231
2.8.7.4 Throwing an exception from main ...232
2.8.7.5 Swallowing exceptions..232
2.8.8 Throwing Your Own Exceptions..233
Book III: Object-Oriented Programming ....235
3.1 Chapter 1: Understanding Object-Oriented Programming . . . . .237
3.1.1 What Is Object-Oriented Programming? ...237
3.1.2 Understanding Objects..238
3.1.2.1 Objects have identity....239
3.1.2.2 Objects have type....240
3.1.2.3 Objects have state...240
3.1.2.4 Objects have behavior..241
3.1.3 The Life Cycle of an Object....242
3.1.4 Working with Related Classes...243
3.1.4.1 Inheritance ...243
3.1.4.2 Interfaces...244
3.1.5 Designing a Program with Objects....244
3.1.6 Diagramming Classes with UML..245
3.1.6.1 Drawing classes ....246
3.1.6.2 Drawing arrows..248
3.2 Chapter 2: Making Your Own Classes . .249
3.2.1 Declaring a Class ...249
3.2.1.1 Picking class names ...250
3.2.1.2 What goes in the class body ....250
3.2.1.3 Where classes go ..251
3.2.2 Working with Members ....253
3.2.2.1 Fields....253
3.2.2.2 Methods..253
3.2.2.3 Understanding visibility ..254
3.2.3 Getters and Setters ..254
3.2.4 Overloading Methods ....257
3.2.5 Creating Constructors ...258
3.2.5.1 Basic constructors ..258
3.2.5.2 Default constructors ..259
3.2.5.3 Calling other constructors ....260
3.2.6 More Uses for this ....262
3.2.7 Using Initializers....263
3.3 Chapter 3: Working with Statics . . . . .265
3.3.1 Understanding Static Fields and Methods ..265
3.3.2 Working with Static Fields...266
3.3.3 Using Static Methods..267
3.3.4 Counting Instances ..268
3.3.5 Preventing Instances ..271
3.3.6 Using Static Initializers ..271
3.4 Chapter 4: Using Subclasses and Inheritance . . .273
3.4.1 Introducing Inheritance....273
3.4.1.1 Plains, trains, and automobiles ..274
3.4.1.2 Playing games ....275
3.4.1.3 A businesslike example ...276
3.4.1.4 Inheritance hierarchies....276
3.4.2 Creating Subclasses ....277
3.4.3 Overriding Methods....278
3.4.4 Protecting Your Members ...279
3.4.5 Using this and super in Your Subclasses ..280
3.4.6 Inheritance and Constructors ..281
3.4.7 Using final...283
3.4.7.1 Final methods ....283
3.4.7.2 Final classes ....283
3.4.8 Casting Up and Down ....284
3.4.9 Determining an Object’s Type ..286
3.4.10 Poly What? ....287
3.4.11 Creating Custom Exceptions ....289
3.4.11.1 The Throwable hierarchy...289
3.4.11.2 Creating an exception class ..290
3.4.11.3 Throwing a custom exception ....291
3.5 Chapter 5: Using Abstract Classes and Interfaces . . . . .293
3.5.1 Using Abstract Classes ..293
3.5.2 Using Interfaces..296
3.5.2.1 Creating a basic interface...296
3.5.2.2 Implementing an interface ....297
3.5.2.3 Using an interface as a type ..298
3.5.3 More Things You Can Do with Interfaces..299
3.5.3.1 Adding fields to an interface ....299
3.5.3.2 Extending interfaces ..299
3.5.3.3 Using interfaces for callbacks..300
3.6 Chapter 6: Using the Object and Class Classes . .305
3.6.1 The Mother of All Classes: Object..305
3.6.1.1 Every object is an Object ...305
3.6.1.2 Using Object as a type ..306
3.6.1.3 Methods of the Object class ....307
3.6.1.4 Primitives aren’t objects ....308
3.6.2 The toString Method...309
3.6.2.1 Using toString ....309
3.6.2.2 Overriding toString ....310
3.6.3 The equals Method ..311
3.6.3.1 Using equals....312
3.6.3.2 Overriding the equals method....313
3.6.4 The clone Method ....316
3.6.4.1 Implementing the clone method ...317
3.6.4.2 Using clone to create a shallow copy ....320
3.6.4.3 Creating deep copies ....321
3.6.5 The Class Class...327
3.7 Chapter 7: Using Inner Classes . . . . 329
3.7.1 Declaring Inner Classes ....329
3.7.1.1 Understanding inner classes....330
3.7.1.2 An example...330
3.7.2 Using Static Inner Classes ...333
3.7.3 Using Anonymous Inner Classes....334
3.7.3.1 Creating an anonymous class ..335
3.7.3.2 Tick Tock with an anonymous class ...336
3.8 Chapter 8: Packaging and Documenting Your Classes . . .339
3.8.1 Working with Packages..339
3.8.1.1 Importing classes and packages....339
3.8.1.2 Creating your own packages....340
3.8.1.3 An example...342
3.8.2 Putting Your Classes in a JAR File ..343
3.8.2.1 jar command-line options ..344
3.8.2.2 Archiving a package ...345
3.8.2.3 Adding a jar to your classpath ...346
3.8.2.4 Running a program directly from an archive....346
3.8.3 Using JavaDoc to Document Your Classes...347
3.8.3.1 Adding JavaDoc comments...347
3.8.3.2 Using the javadoc command....350
3.8.3.3 Viewing JavaDoc pages....351
Book IV: Strings, Arrays, and Collections ..353
4.1 Chapter 1: Working with Strings . . . .355
4.1.1 Reviewing Strings..355
4.1.2 Using the String Class....357
4.1.2.1 Finding the length of a string ...359
4.1.2.2 Making simple string modifications....360
4.1.2.3 Extracting characters from a string ....360
4.1.2.4 Extracting substrings from a string ....361
4.1.2.5 Table of Contents xvii
4.1.2.6 Splitting up a string ....363
4.1.2.7 Replacing parts of a string ....365
4.1.3 Using the StringBuilder and StringBuffer Classes...365
4.1.3.1 Creating a StringBuilder object ..366
4.1.3.2 Using StringBuilder methods...367
4.1.3.3 A StringBuilder example..369
4.1.4 Using the CharSequence Interface....369
4.2 Chapter 2: Using Arrays . . . .371
4.2.1 Understanding Arrays ...371
4.2.2 Creating Arrays...372
4.2.3 Initializing an Array..373
4.2.4 Using for Loops with Arrays ..374
4.2.5 Solving Homework Problems with Arrays ...375
4.2.6 Using the Enhanced for Loop ...377
4.2.7 Using Arrays with Methods ...378
4.2.8 Using Two-Dimensional Arrays ...379
4.2.8.1 Creating a two-dimensional array ....380
4.2.8.2 Accessing two-dimensional array elements...381
4.2.8.3 Initializing a two-dimensional array....382
4.2.8.4 Using jagged arrays....382
4.2.8.5 Going beyond two dimensions ...384
4.2.9 A Fun but Complicated Example: A Chess Board ...385
4.2.10 Using the Arrays Class...392
4.2.10.1 Filling an array ...393
4.2.10.2 Sorting an array ....393
4.2.10.3 Searching an array...394
4.2.10.4 Comparing arrays....394
4.2.10.5 Converting arrays to strings ....395
4.3 Chapter 3: Using the ArrayList Class . . .397
4.3.1 The ArrayList Class..398
4.3.2 Creating an ArrayList Object ....401
4.3.3 Adding Elements ...402
4.3.4 Accessing Elements ....403
4.3.5 Printing an ArrayList ..403
4.3.6 Using an Iterator....404
4.3.7 Updating Elements...406
4.3.8 Deleting Elements ....407
4.4 Chapter 4: Using the LinkedList Class . .409
4.4.1 The LinkedList Class...409
4.4.2 Creating a LinkedList ..413
4.4.3 Adding Items to a LinkedList ....414
4.4.4 Retrieving Items from a LinkedList...416
4.4.5 Updating LinkedList Items ..417
4.4.6 Removing LinkedList Items....417
4.5 Chapter 5: Creating Generic Collection Classes . . . . .419
4.5.1 Why Generics?....420
4.5.2 Creating a Generic Class ..421
4.5.3 A Generic Stack Class ....422
4.5.4 Using Wildcard Type Parameters...426
4.5.5 A Generic Queue Class ..427
Book V: Programming Techniques ....431
5.1 Chapter 1: Programming Threads . . . .433
5.1.1 Understanding Threads....433
5.1.2 Creating a Thread..434
5.1.2.1 Understanding the Thread class ...435
5.1.2.2 Extending the Thread class...436
5.1.2.3 Creating and starting a thread....437
5.1.3 Implementing the Runnable Interface ...438
5.1.3.1 Using the Runnable interface...438
5.1.3.2 Creating a class that implements Runnable...439
5.1.3.3 Using the CountDownApp class ....440
5.1 4 Creating Threads That Work Together...442
5.1.5 Synchronizing Methods....446
5.1.6 Threadus Interruptus ....447
5.1.6.1 Finding out if you’ve been interrupted..447
5.1.6.2 Aborting the countdown ....449
5.2 Chapter 2: Network Programming . . .453
5.2.1 Understanding Network Programming...453
5.2.1.1 IP addresses and ports ....454
5.2.1.2 Host names, DNS, and URLs..455
5.2.1.3 Telnet ...455
5.2.2 Getting Information about Internet Hosts....456
5.2.2.1 The InetAddress class...456
5.2.2.2 A program that looks up host names ....458
5.2.3 Creating Network Server Applications...460
5.2.3.1 The Socket class ..461
5.2.3.2 The ServerSocket class....462
5.2.4 Introducing BART..463
5.2.4.1 The BartQuote class ..464
5.2.4.2 The BartServer program ....465
5.2.4.3 The BartClient program...468
5.2.5 BartServer 2.0 ..471
5.3 Chapter 3: Using Regular Expressions . . . . .475
5.3.1 A Program for Experimenting with Regular Expressions...476
5.3.2 Basic Character Matching...478
5.3.2.1 Matching single characters...479
5.3.2.2 Using predefined character classes ....479
5.3.2.3 Using custom character classes....481
5.3.2.4 Using ranges....482
5.3.2.5 Using negation ...483
5.3.2.6 Matching multiple characters..483
5.3.2.7 Using escapes ....485
5.3.2.8 Using parentheses to group characters ...485
5.3.2.9 Using the | symbol..487
5.3.3 Using Regular Expressions in Java Programs....488
5.3.3.1 The String problem ....488
5.3.3.2 Using regular expressions with the String class..489
5.3.3.3 Using the Pattern and Matcher classes ....489
5.4 Chapter 4: Using Recursion . . .491
5.4.1 The Classic Factorial Example..491
5.4.1.1 The non-recursive solution ...491
5.4.1.2 The recursive solution..492
5.4.2 Displaying Directories ...494
5.4.3 Writing Your Own Sorting Routine....497
5.4.3.1 Understanding how Quicksort works....498
5.4.3.2 The sort method...499
5.4.3.3 The partition method....500
5.4.3.4 Putting it all together ....502
Book VI: Swing ...505
6.1 Chapter 1: Swinging into Swing . . . . .507
6.1.1 Some Important Swing Concepts You Need to Know...507
6.1.1.1 Understanding what Swing does ...507
6.1.1.2 The Swing class hierarchy..508
6.1.2 I’ve Been Framed! ..510
6.1.3 Hello, World! in Swing ....511
6.1.4 Positioning the Frame On-Screen...513
6.1.5 Using the JPanel Class ...514
6.1.6 Using Labels..516
6.1.7 Creating Buttons ...518
6.1.8 A Word about the Layout of Components....520
6.2 Chapter 2: Handling Events . . .521
6.2.1 Examining Events ..521
6.2.2 Handling Events..524
6.2.3 The ClickMe Program ....526
6.2.4 Using Inner Classes to Listen for Events...528
6.2.5 Adding an Exit Button....530
6.2.6 Catching the WindowClosing Event..532
6.2.7 The ClickMe Program Revisited ..534
6.3 Chapter 3: Getting Input from the User . . . . .537
6.3.1 Using Text Fields ...537
6.3.1.1 Looking at a sample program ..539
6.3.1.2 Using text fields for numeric entry ..541
6.3.1.3 Creating a validation class ....543
6.3.2 Using Text Areas....544
6.3.2.1 The JTextArea class ...545
6.3.2.2 The JScrollPane class....547
6.3.3 Using Check Boxes...548
6.3.4 Using Radio Buttons ...551
6.3.5 Using Borders ..553
6.3.6 Designing a Pizza-Ordering Program ...556
6.3.7 Using Sliders ....559
6.4 Chapter 4: Choosing from a List . . . . .563
6.4.1 Using Combo Boxes ....563
6.4.1.1 Creating combo boxes ..565
6.4.1.2 Getting items from a combo box ...566
6.4.1.3 Handling combo box events ....567
6.4.2 Using Lists..567
6.4.2.1 Creating a list ..569
6.4.2.2 Getting items from a list ..570
6.4.2.3 Changing list items..571
6.4.3 Using Spinners....573
6.4.4 Using Trees ...575
6.4.4.1 Building a tree....576
6.4.4.2 Creating a JTree component ....579
6.4.4.3 Getting the selected node ..580
6.4.4.4 Putting it all together ....581
6.5 Chapter 5: Using Layout Managers . . 585
6.5.1 Introducing Layout Managers...585
6.5.2 Using Flow Layout....587
6.5.3 Using Border Layout...588
6.5.4 Table of Contents xxi
6.5.5 Using Box Layout ..590
6.5.6 Using Grid Layout ....592
6.5.7 Using GridBag Layout ....593
6.5.7.1 Sketching out a plan...594
6.5.7.2 Adding components to a GridBag ....595
6.5.7.3 Working with GridBagConstraints....597
6.5.7.4 A GridBag layout example..598
Book VII: Web Programming ..603
7.1 Chapter 1: Creating Applets . . .605
7.1.1 Understanding Applets..605
7.1.2 The JApplet Class..606
7.1.3 Looking At a Sample Applet...607
7.1.4 Creating an HTML Page for an Applet ....611
7.1.5 Testing an Applet ..611
7.2 Chapter 2: Creating Servlets . .613
7.2.1 Understanding Servlets....613
7.2.2 Using Tomcat...614
7.2.2.1 Installing and configuring Tomcat....615
7.2.2.2 Starting and stopping Tomcat ....617
7.2.2.3 Testing Tomcat ..618
7.2.4 Creating a Simple Servlet ....619
7.2.4.1 Importing the servlet packages..619
7.2.4.2 Extending the HttpServlet class ....619
7.2.4.3 Printing to a Web page..620
7.2.4.4 Responding with HTML ...620
7.2.5 Running a Servlet ..623
7.2.6 An Improved HelloWorld Servlet ...623
7.2.7 Getting Input from the User ...625
7.2.7.1 Working with forms....625
7.2.7.2 The InputServlet servlet..626
7.2.8 Using Classes in a Servlet ...627
7.3 Chapter 3: Using Java Server Pages . . .633
7.3.1 Understanding Java Server Pages..633
7.3.2 Using Page Directives ....635
7.3.3 Using Expressions ....636
7.3.4 Using Scriptlets ..638
7.3.5 Using Declarations ...640
7.3.6 Using Classes ...642
7.4 Chapter 4: Using JavaBeans . .647
7.4.1 What Is a JavaBean?....647
7.4.2 Looking Over a Sample Bean ....648
7.4.3 Using Beans with JSP Pages...651
7.4.3.1 Creating bean instances ..651
7.4.3.2 Getting property values...652
7.4.3.3 Setting property values ...653
7.4.3.4 A JSP page that uses a bean..654
7.4.4 Scoping Your Beans ....656
7.4.4.1 A shopping cart application..657
7.4.4.2 The shopping cart page...658
7.4.4.3 The BookCart JavaBean...659
Book VIII: Files and Databases..663
8.1 Chapter 1: Working with Files . . . . .665
8.1.1 Using the File Class ..665
8.1.1.1 Creating a File object ....667
8.1.1.2 Creating a file ..668
8.1.1.3 Getting information about a file ....668
8.1.1.4 Getting the contents of a directory..669
8.1.1.5 Renaming files....670
8.1.1.6 Deleting a file ..670
8.1.2 Using Command-Line Parameters..671
8.1.3 Choosing Files in a Swing Application....672
8.1.3.1 Creating an Open dialog box....674
8.1.3.2 Getting the selected file...675
8.1.3.3 Using file filters ..676
8.2 Chapter 2: Using File Streams . . . . .679
8.2.1 Understanding Streams....679
8.2.2 Reading Character Streams ...680
8.2.2.1 Creating a BufferedReader ....682
8.2.2.2 Reading from a character stream..682
8.2.2.3 Reading the movies.txt file....683
8.2.3 Writing Character Streams..686
8.2.3.1 Connecting a PrintWriter to a text file ...687
8.2.3.2 Writing to a character stream..688
8.2.3.3 Writing the movies.txt file ..689
8.2.4 Reading Binary Streams ...692
8.2.4.1 Creating a DataInputStream..693
8.2.4.2 Reading from a data input stream....694
8.2.4.3 Reading the movies.dat file...695
8.2.5 Writing Binary Streams..698
8.2.5.1 Creating a DataOutputStream..699
8.2.5.2 Writing to a binary stream ....700
8.2.5.3 Writing the movies.dat file ....700
8.3 Chapter 3: Database for $100, Please . . .703
8.3.1 What Is a Relational Database? ...703
8.3.2 What Is SQL, and How Do You Pronounce It?....704
8.3.3 SQL Statements ..704
8.3.4 Creating a SQL Database ..705
8.3.5 Querying a Database...707
8.3.5.1 Using your basic select....707
8.3.5.2 Narrowing down the query ...709
8.3.5.3 Excluding rows...709
8.3.5.4 Singleton selects...709
8.3.5.5 Sounds like ...710
8.3.5.6 Column functions ....710
8.3.5.7 Selecting from more than one table ....711
8.3.5.8 Eliminating duplicates ..713
8.3.6 Updating and Deleting Rows..713
8.3.6.1 The delete statement ....713
8.3.6.2 The update statement...715
8.4 Chapter 4: Using JDBC to Connect to a Database . . . . 717
8.4.1 Setting Up a Driver...717
8.4.1.1 Setting up an ODBC data source ...717
8.4.1.2 Setting up the MySQL JDBC connector ....719
8.4.2 Connecting to a Database ...720
8.4.3 Querying a Database...721
8.4.3.1 Executing a select statement ...723
8.4.3.2 Navigating through the result set..723
8.4.3.3 Getting data from a result set ..723
8.4.3.4 Putting it all together: A program that reads from a database..725
8.4.5 Updating SQL Data...728
8.4.6 Using an Updatable RowSet Object ..729
8.4.6.1 Deleting a row....730
8.4.6.2 Updating the value of a row column...731
8.4.6.3 Inserting a row...732
8.5 Chapter 5: Working with XML . . . . .733
8.5.1 What Exactly Is XML, Anyway?....733
8.5.1.1 Tags ...734
8.5.1.2 Attributes ..735
8.5.1.3 The movies.xml file ....735
8.5.2 Using a DTD ..736
8.5.3 Processing XML in Two Ways ...739
8.5.4 Reading a DOM Document ..741
8.5.4.1 Creating a document builder factory..741
8.5.4.2 Configuring the document builder factory ....742
8.5.4.3 Creating a document builder and the document ...742
8.5.4.4 Using the getDocument method....743
8.5.5 Reading DOM Nodes...743
8.5.5.1 Processing elements ..745
8.5.5.2 Getting attribute values...746
8.5.5.3 Getting child element values ...747
8.5.6 Putting It All Together: A Program That Lists Movies..748
Book IX: Fun and Games..751
9.1 Chapter 1: Fun with Fonts and Colors . .753
9.1.1 Working with Fonts ..753
9.1.1.1 Using font names ..754
9.1.1.2 Using font styles ...754
9.1.1.3 Setting a component’s font ...755
9.1.1.4 Getting a list of all available fonts ....756
9.1.1.5 A program that plays with fonts....756
9.1.2 Working with Color ..760
9.1.2.1 Creating colors...760
9.1.2.2 Using system colors...761
9.1.2.3 Setting the color of Swing components....763
9.1.2.4 Using a color chooser ...763
9.2 Chapter 2: Drawing Shapes . . .767
9.2.1 Getting a Graphics Context....767
9.2.2 Drawing Shapes..768
9.2.3 Creating Shapes..771
9.2.3.1 Creating lines ..772
9.2.3.2 Creating rectangles ....773
9.2.3.3 Creating ellipses ...774
9.2.3.4 Creating arcs ...774
9.2.3.5 Looking at the ShapeMaker program..775
9.2.4 Filling Shapes...777
9.2.4.1 Drawing transparently ..777
9.2.4.2 Using a gradient fill ....778
9.2.5 Rotating and Translating ....780
9.2.5.1 Translate method ....780
9.2.5.2 Rotate method ...780
9.2.6 Drawing Text....782
9.2.7 Letting the User Draw on a Component....782
9.3 Chapter 3: Using Images and Sound . . .789
9.3.1 Using Images....790
9.3.2 Using the ImageIcon Class ..790
9.3.2.1 Using ImageIcon in a Swing application ...791
9.3.2.2 Using ImageIcon in an applet...793
9.3.3 Using the Image Class....793
9.3.3.1 Creating an Image object....794
9.3.3.2 Drawing an Image object ....795
9.3.3.3 An Image example ...796
9.3.4 Playing Sounds and Making Music....799
9.4 Chapter 4: Animation and Game Programming . .803
9.4.1 Animating a Sprite....803
9.4.2 What about Double Buffering? ....807
9.4.3 Bouncing the Ball ..807
9.4.4 Bouncing a Bunch of Balls ..809
9.4.4.1 Creating a Ball class ...809
9.4.4.2 Animating random balls ..811
9.4.5 Creating Collidable Balls ..812
9.4.6 Playing Games ....814
Index...821