ebook img

Core Java® Volume II—Advanced Features - Pearsoncmg PDF

149 Pages·2013·8.47 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Core Java® Volume II—Advanced Features - Pearsoncmg

Core Java ® Volume II—Advanced Features Ninth Edition This page intentionally left blank Core Java ® Volume II—Advanced Features Ninth Edition Cay S. Horstmann Gary Cornell Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid Capetown • Sydney • Tokyo • Singapore • Mexico City Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners. The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. This document is provided for information purposes only and the contents hereof are subject to change without notice. This document is not warranted to be error-free, nor subject to any other warranties or conditions, whether expressed orally or implied in law, including implied warranties and conditions of merchantability or fitness for a particular purpose. We specifically disclaim any liability with respect to this document and no contractual obligations are formed either directly or indirectly by this document. This document may not be reproduced or transmitted in any form or by any means, electronic or mechanical, for any purpose, without our prior written permission. The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact: U.S. Corporate and Government Sales (800) 382-3419 [email protected] For sales outside the United States, please contact: International Sales [email protected] Visit us on the Web: informit.com/ph Library of Congress Cataloging-in-Publication Data: Horstmann, Cay S., 1959- Core Java / Cay S. Horstmann, Gary Cornell.—Ninth edition. pages cm Includes index. ISBN 978-0-13-708189-9 (v. 1 : pbk. : alk. paper) 1. Java (Computer program language) I. Cornell, Gary. II. Title. QA76.73.J38H6753 2013 005.13'3—dc23 2012035397 Copyright © 2013 Oracle and/or its affiliates. All rights reserved. 500 Oracle Parkway, Redwood Shores, CA 94065 Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. To obtain permission to use material from this work, please submit a written request to Pearson Education, Inc., Permissions Department, One Lake Street, Upper Saddle River, New Jersey 07458, or you may fax your request to (201) 236-3290. ISBN-13: 978-0-13-708160-8 ISBN-10: 0-13-708160-X Text printed in the United States on recycled paper at Edwards Brothers Malloy in Ann Arbor, Michigan. Second printing, September 2013 Contents Preface ........................................................................................................... xv Acknowledgments ........................................................................................ xix Chapter 1: Streams and Files ........................................................................ 1 1.1 Streams .......................................................................................................... 2 1.1.1 Reading and Writing Bytes .......................................................... 2 1.1.2 The Complete Stream Zoo ........................................................... 4 1.1.3 Combining Stream Filters ............................................................. 9 1.2 Text Input and Output ............................................................................. 13 1.2.1 How to Write Text Output ......................................................... 13 1.2.2 How to Read Text Input ............................................................. 16 1.2.3 Saving Objects in Text Format ................................................... 16 1.2.4 Character Sets ............................................................................... 20 1.3 Reading and Writing Binary Data .......................................................... 25 1.3.1 Random-Access Files .................................................................. 28 1.4 ZIP Archives .............................................................................................. 33 1.5 Object Streams and Serialization ............................................................ 36 1.5.1 Understanding the Object Serialization File Format .............. 42 1.5.2 Modifying the Default Serialization Mechanism .................... 48 1.5.3 Serializing Singletons and Typesafe Enumerations ............... 50 1.5.4 Versioning ..................................................................................... 52 1.5.5 Using Serialization for Cloning ................................................. 54 1.6 Working with Files .................................................................................... 57 1.6.1 Paths .............................................................................................. 57 1.6.2 Reading and Writing Files .......................................................... 60 1.6.3 Copying, Moving, and Deleting Files ....................................... 61 1.6.4 Creating Files and Directories ................................................... 62 1.6.5 Getting File Information ............................................................. 63 1.6.6 Iterating over the Files in a Directory ....................................... 64 1.6.7 ZIP File Systems ........................................................................... 67 v vi Contents 1.7 Memory-Mapped Files ............................................................................. 68 1.7.1 The Buffer Data Structure ........................................................... 77 1.7.2 File Locking .................................................................................. 79 1.8 Regular Expressions .................................................................................. 81 Chapter 2: XML ............................................................................................. 93 2.1 Introducing XML ....................................................................................... 94 2.1.1 The Structure of an XML Document ......................................... 96 2.2 Parsing an XML Document ...................................................................... 99 2.3 Validating XML Documents .................................................................. 113 2.3.1 Document Type Definitions ..................................................... 114 2.3.2 XML Schema ............................................................................... 122 2.3.3 A Practical Example .................................................................. 125 2.4 Locating Information with XPath ......................................................... 140 2.5 Using Namespaces .................................................................................. 147 2.6 Streaming Parsers .................................................................................... 150 2.6.1 Using the SAX Parser ................................................................ 150 2.6.2 Using the StAX Parser ............................................................... 156 2.7 Generating XML Documents ................................................................. 159 2.7.1 Documents without Namespaces ............................................ 159 2.7.2 Documents with Namespaces ................................................. 160 2.7.3 Writing Documents ................................................................... 161 2.7.4 An Example: Generating an SVG File .................................... 161 2.7.5 Writing an XML Document with StAX .................................. 164 2.8 XSL Transformations .............................................................................. 173 Chapter 3: Networking ............................................................................... 185 3.1 Connecting to a Server ........................................................................... 185 3.1.1 Socket Timeouts ......................................................................... 190 3.1.2 Internet Addresses ..................................................................... 192 3.2 Implementing Servers ............................................................................ 194 3.2.1 Serving Multiple Clients ........................................................... 197 3.2.2 Half-Close ................................................................................... 201 3.3 Interruptible Sockets ............................................................................... 202 3.4 Getting Web Data .................................................................................... 210 3.4.1 URLs and URIs ........................................................................... 210 Contents vii 3.4.2 Using a URLConnection to Retrieve Information ........................... 212 3.4.3 Posting Form Data ..................................................................... 222 3.5 Sending E-Mail ........................................................................................ 230 Chapter 4: Database Programming .......................................................... 235 4.1 The Design of JDBC ................................................................................ 236 4.1.1 JDBC Driver Types .................................................................... 236 4.1.2 Typical Uses of JDBC ................................................................ 238 4.2 The Structured Query Language .......................................................... 239 4.3 JDBC Configuration ................................................................................ 245 4.3.1 Database URLs ........................................................................... 246 4.3.2 Driver JAR Files ......................................................................... 246 4.3.3 Starting the Database ................................................................ 247 4.3.4 Registering the Driver Class .................................................... 248 4.3.5 Connecting to the Database ..................................................... 249 4.4 Executing SQL Statements ..................................................................... 252 4.4.1 Managing Connections, Statements, and Result Sets ........... 255 4.4.2 Analyzing SQL Exceptions ....................................................... 256 4.4.3 Populating a Database .............................................................. 258 4.5 Query Execution ...................................................................................... 262 4.5.1 Prepared Statements ................................................................. 263 4.5.2 Reading and Writing LOBs ...................................................... 269 4.5.3 SQL Escapes ............................................................................... 271 4.5.4 Multiple Results ......................................................................... 272 4.5.5 Retrieving Autogenerated Keys .............................................. 273 4.6 Scrollable and Updatable Result Sets ................................................... 274 4.6.1 Scrollable Result Sets ................................................................. 274 4.6.2 Updatable Result Sets ............................................................... 277 4.7 Row Sets ................................................................................................... 281 4.7.1 Constructing Row Sets .............................................................. 282 4.7.2 Cached Row Sets ........................................................................ 282 4.8 Metadata ................................................................................................... 286 4.9 Transactions ............................................................................................. 296 4.9.1 Save Points .................................................................................. 297 4.9.2 Batch Updates ............................................................................ 298 viii Contents 4.9.3 Advanced SQL Types ............................................................... 300 4.10 Connection Management in Web and Enterprise Applications ....... 302 Chapter 5: Internationalization .................................................................. 305 5.1 Locales ....................................................................................................... 306 5.2 Number Formats ..................................................................................... 311 5.2.1 Currencies ................................................................................... 318 5.3 Date and Time .......................................................................................... 319 5.4 Collation ................................................................................................... 328 5.4.1 Collation Strength ...................................................................... 329 5.4.2 Decomposition ........................................................................... 329 5.5 Message Formatting ................................................................................ 336 5.5.1 Choice Formats .......................................................................... 338 5.6 Text Files and Character Sets ................................................................. 340 5.6.1 Character Encoding of Source Files ........................................ 340 5.7 Resource Bundles .................................................................................... 341 5.7.1 Locating Resource Bundles ...................................................... 342 5.7.2 Property Files ............................................................................. 343 5.7.3 Bundle Classes ........................................................................... 344 5.8 A Complete Example .............................................................................. 346 Chapter 6: Advanced Swing ...................................................................... 363 6.1 Lists ........................................................................................................... 364 6.1.1 The JList Component ................................................................. 364 6.1.2 List Models ................................................................................. 370 6.1.3 Inserting and Removing Values .............................................. 375 6.1.4 Rendering Values ...................................................................... 377 6.2 Tables ........................................................................................................ 381 6.2.1 A Simple Table ........................................................................... 382 6.2.2 Table Models .............................................................................. 386 6.2.3 Working with Rows and Columns ......................................... 390 6.2.3.1 Column Classes .......................................................... 390 6.2.3.2 Accessing Table Columns ......................................... 392 6.2.3.3 Resizing Columns ....................................................... 392 6.2.3.4 Resizing Rows ............................................................. 393 6.2.3.5 Selecting Rows, Columns, and Cells ....................... 394 Contents ix 6.2.3.6 Sorting Rows ............................................................... 395 6.2.3.7 Filtering Rows ............................................................. 396 6.2.3.8 Hiding and Displaying Columns ............................. 398 6.2.4 Cell Rendering and Editing ...................................................... 408 6.2.4.1 Rendering the Header ................................................ 409 6.2.4.2 Cell Editing .................................................................. 410 6.2.4.3 Custom Editors ........................................................... 411 6.3 Trees .......................................................................................................... 420 6.3.1 Simple Trees ............................................................................... 421 6.3.1.1 Editing Trees and Tree Paths .................................... 431 6.3.2 Node Enumeration .................................................................... 440 6.3.3 Rendering Nodes ....................................................................... 442 6.3.4 Listening to Tree Events ........................................................... 445 6.3.5 Custom Tree Models ................................................................. 453 6.4 Text Components .................................................................................... 462 6.4.1 Change Tracking in Text Components ................................... 463 6.4.2 Formatted Input Fields ............................................................. 467 6.4.2.1 Integer Input ............................................................... 468 6.4.2.2 Behavior on Loss of Focus ......................................... 468 6.4.2.3 Filters ............................................................................ 470 6.4.2.4 Verifiers ........................................................................ 471 6.4.2.5 Other Standard Formatters ....................................... 472 6.4.2.6 Custom Formatters ..................................................... 474 6.4.3 The JSpinner Component ............................................................. 485 6.4.4 Displaying HTML with the JEditorPane ...................................... 494 6.5 Progress Indicators .................................................................................. 501 6.5.1 Progress Bars .............................................................................. 501 6.5.2 Progress Monitors ...................................................................... 505 6.5.3 Monitoring the Progress of Input Streams ............................. 509 6.6 Component Organizers and Decorators .............................................. 514 6.6.1 Split Panes ................................................................................... 514 6.6.2 Tabbed Panes .............................................................................. 518 6.6.3 Desktop Panes and Internal Frames ....................................... 524 6.6.4 Cascading and Tiling ................................................................ 527 6.6.5 Vetoing Property Settings ........................................................ 531

Description:
Core Java®. Volume II—Advanced. Features. Ninth Edition. Cay S. Horstmann. Gary Cornell. Upper Saddle River, NJ • Boston • Indianapolis • San Francisco.
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.