769

Click here to load reader

J2ME - The Complete Reference.pdf

  • Upload
    phamanh

  • View
    371

  • Download
    46

Embed Size (px)

Citation preview

  • J2ME:The Complete Reference

    James Keogh

    McGraw-Hill/OsborneNew York Chicago San Francisco

    Lisbon London Madrid Mexico CityMilan New Delhi San Juan

    Seoul Singapore Sydney Toronto

    Complete Reference / J2ME: TCR / Keogh / 222710-9Blind Folio i

    P:\010Comp\CompRef8\710-9\fm.vpFriday, February 07, 2003 1:49:46 PM

    Color profile: Generic CMYK printer profileComposite Default screen

  • McGraw-Hill/Osborne2600 Tenth StreetBerkeley, California 94710U.S.A.

    To arrange bulk purchase discounts for sales promotions, premiums, or fund-raisers,please contact McGraw-Hill/Osborne at the above address. For information ontranslations or book distributors outside the U.S.A., please see the InternationalContact Information page immediately following the index of this book.

    J2ME: The Complete Reference

    Copyright 2003 by The McGraw-Hill Companies. All rights reserved. Printed in theUnited States of America. Except as permitted under the Copyright Act of 1976, no partof this publication may be reproduced or distributed in any form or by any means,or stored in a database or retrieval system, without the prior written permission ofpublisher, with the exception that the program listings may be entered, stored, andexecuted in a computer system, but they may not be reproduced for publication.

    1234567890 CUS CUS 019876543

    ISBN 0-07-222710-9

    PublisherBrandon A. Nordin

    Vice President & Associate PublisherScott Rogers

    Editorial DirectorWendy Rinaldi

    Project EditorMark Karmendy

    Acquisitions CoordinatorAthena Honore

    Technical EditorAmar Mehta

    Copy EditorJudith Brown

    ProofreaderClaire Splan

    IndexerJack Lewis

    Computer DesignersApollo Publishing Services,Lucie Ericksen, Tara A. Davis

    IllustratorsMichael Mueller, Lyssa Wald,Melinda Moore Lytle

    Series DesignPeter F. Hancik

    This book was composed with Corel VENTURA Publisher.

    Information has been obtained by McGraw-Hill/Osborne from sources believed to be reliable. However, because of thepossibility of human or mechanical error by our sources, McGraw-Hill/Osborne, or others, McGraw-Hill/Osborne doesnot guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissionsor the results obtained from the use of such information.

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Front MatterBlind Folio FM:ii

    P:\010Comp\CompRef8\710-9\fm.vpFriday, February 07, 2003 1:49:47 PM

    Color profile: Generic CMYK printer profileComposite Default screen

  • This book is dedicated to Anne, Sandy,Joanne, Amber-Leigh Christine, and Graaf,

    without whose help and supportthis book couldnt be written.

    Complete Reference / J2ME: TCR / Keogh / 222710-9Blind Folio iii

    Complete Reference / J2ME: TCR / Keogh / 222710-9Blind Folio iii

    P:\010Comp\CompRef8\710-9\fm.vpFriday, February 07, 2003 1:49:47 PM

    Color profile: Generic CMYK printer profileComposite Default screen

  • About the Author

    Jim Keogh teaches courses on Java ApplicationDevelopment, including J2EE, at ColumbiaUniversity and is a member of the JavaCommunity Process Program. He developedthe first e-commerce track at Columbia andbecame its first chairperson. Jim spent morethan a decade developing advanced systemsfor major Wall Street firms. Jim introducedPC Programming nationally in his PopularElectronics Magazine column in 1982, four yearsafter Apple Computer started in a garage. Hewas also a team member who built one of thefirst Windows applications by a Wall Streetfirm, featured by Bill Gates in 1986. Jim is theauthor of 55 books, including his most recentbook, J2EE: The Complete Reference. He is also afaculty member in the Graduate School, SaintPeters College, New Jersey City, NJ.

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Front MatterBlind Folio FM:iv

    P:\010Comp\CompRef8\710-9\fm.vpFriday, February 07, 2003 1:49:47 PM

    Color profile: Generic CMYK printer profileComposite Default screen

  • Contents

    Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv

    Part I

    J2ME Basics

    1 J2ME Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3Java 2 Micro Edition and the World of Java . . . . . . . . . . . . . . . . . . . . 4

    Enter Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5Java Virtual Machine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6J2EE and J2SE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6The Birth of J2EE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8Back to the Future: J2ME . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

    Inside J2ME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9How J2ME Is Organized . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

    J2ME and Wireless Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14What J2ME Isnt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15Other Java Platforms for Small Computing Devices . . . . . . . . . . . . 16

    v

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Front MatterFM:v

    P:\010Comp\CompRef8\710-9\fm.vpFriday, February 07, 2003 1:49:47 PM

    Color profile: Generic CMYK printer profileComposite Default screen

  • 2 Small Computing Technology . . . . . . . . . . . . . . . . . . . . . 17Wireless Technology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

    Radio Transmission . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20Limitations of Radio Transmissions . . . . . . . . . . . . . . . . . . . 20

    Radio Data Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22Data Packets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

    Microwave Technology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24Satellite Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

    Mobile Radio Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25Cellular Telephone Networks . . . . . . . . . . . . . . . . . . . . . . . . 26Digital Wireless Transmissions . . . . . . . . . . . . . . . . . . . . . . . 28Cell Phones and Text Input . . . . . . . . . . . . . . . . . . . . . . . . . . 28

    Messaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29Personal Digital Assistants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30Mobile Power . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31Set-Top Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

    Inside Look at a Set-Top Box . . . . . . . . . . . . . . . . . . . . . . . . . 33Smart Cards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

    3 J2ME Architecture and Development Environment . . . 35J2ME Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36Small Computing Device Requirements . . . . . . . . . . . . . . . . . . . . . . 37Run-Time Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

    Inside the Java Archive File . . . . . . . . . . . . . . . . . . . . . . . . . . 38Inside the Java Application Descriptor File . . . . . . . . . . . . . 40

    MIDlet Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43User Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44Device Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

    Java Language for J2ME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45J2ME Software Development Kits . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48Hello World J2ME Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50

    Compiling Hello World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53Running Hello World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54Deploying Hello World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54What to Do When Your MIDlet Doesnt

    Work Properly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56Multiple MIDlets in a MIDlet Suite . . . . . . . . . . . . . . . . . . . . . . . . . . . 57J2ME Wireless Toolkit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60

    Building and Running a Project . . . . . . . . . . . . . . . . . . . . . . 61Hello World Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64MIDlets on the Internet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

    vi J 2 M E : T h e C o m p l e t e R e f e r e n c e

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Front Matter

    P:\010Comp\CompRef8\710-9\fm.vpFriday, February 07, 2003 1:49:47 PM

    Color profile: Generic CMYK printer profileComposite Default screen

  • 4 J2ME Best Practices and Patterns . . . . . . . . . . . . . . . . . . . 71The Reality of Working in a J2ME World . . . . . . . . . . . . . . . . . . . . . . 72Best Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

    Keep Applications Simple . . . . . . . . . . . . . . . . . . . . . . . . . . . 73Keep Applications Small . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74Limit the Use of Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75Off-Load Computations to the Server . . . . . . . . . . . . . . . . . 76Manage Your Applications Use of a

    Network Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77Simplify the User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . 79Use Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81Dont Concatenate Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . 81Avoid Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82Thread Group Class Workaround . . . . . . . . . . . . . . . . . . . . . 83Upload Code from the Web Server . . . . . . . . . . . . . . . . . . . . 83Reading Settings from JAD Files . . . . . . . . . . . . . . . . . . . . . . 84Populating Drop-down Boxes . . . . . . . . . . . . . . . . . . . . . . . . 85Minimize Network Traffic . . . . . . . . . . . . . . . . . . . . . . . . . . . 86Dealing with Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86Automatic Data Synchronization . . . . . . . . . . . . . . . . . . . . . 88Updating Data that Has Changed . . . . . . . . . . . . . . . . . . . . . 89Be Careful of the Content of the startApp() Method . . . . . 90

    Part II

    J2ME User Interface

    5 Commands, Items, and Event Processing . . . . . . . . . . . . 95J2ME User Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96Display Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96The Palm OS Emulator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102Command Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

    CommandListener . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105Item Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

    Item Listener . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118

    Throwing a MIDletStateChangeException . . . . . . . . . . . . . 120Quick Reference Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125

    6 High-Level Display: Screens . . . . . . . . . . . . . . . . . . . . . . . 129Screen Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130

    C o n t e n t s vii

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Front Matter

    P:\010Comp\CompRef8\710-9\fm.vpFriday, February 07, 2003 1:49:47 PM

    Color profile: Generic CMYK printer profileComposite Default screen

  • Alert Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131Alert Sound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138

    Form Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139Item Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143

    ChoiceGroup Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144DateField Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154Gauge Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158StringItem Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166TextField Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170ImageItem Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178

    List Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182Creating an Instance of a List Class . . . . . . . . . . . . . . . . . . . 184

    TextBox Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194Creating an Instance of a TextBox Class . . . . . . . . . . . . . . . . 195

    Ticker Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199Quick Reference Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203

    7 Low-Level Display: Canvas . . . . . . . . . . . . . . . . . . . . . . . 213The Canvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214

    The Layout of a Canvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215Proportional Coordinates . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216The Pen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217Painting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217showNotify() and hideNotify() . . . . . . . . . . . . . . . . . . . . . . . 219

    User Interactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219Working with Key Codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220Working with Game Actions . . . . . . . . . . . . . . . . . . . . . . . . . 227Working with Pointer Devices . . . . . . . . . . . . . . . . . . . . . . . . 233

    Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239Stroke Style and Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243Rectangles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243Arcs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266Repositioning Text and Images . . . . . . . . . . . . . . . . . . . . . . . 274

    Clipping Regions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279Creating a Clipping Region . . . . . . . . . . . . . . . . . . . . . . . . . . 280

    Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284Quick Reference Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286

    viii J 2 M E : T h e C o m p l e t e R e f e r e n c e

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Front Matter

    P:\010Comp\CompRef8\710-9\fm.vpFriday, February 07, 2003 1:49:47 PM

    Color profile: Generic CMYK printer profileComposite Default screen

  • Part III

    J2ME Data Management

    8 Record Management System . . . . . . . . . . . . . . . . . . . . . . 295Record Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296

    The Record Store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296Record Store Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297Setting Up a Record Store . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298

    Writing and Reading Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302Creating a New Record and Reading an

    Existing Record . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303Writing and Reading Mixed Data Types . . . . . . . . . . . . . . . 309

    Record Enumeration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316Reading a Record of a Simple Data Type

    into a RecordEnumeration . . . . . . . . . . . . . . . . . . . . . . . . . 319Reading a Mixed Data Type Record into a

    RecordEnumeration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324Sorting Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330

    Sorting Single Data Type Records in aRecordEnumeration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332

    Sorting Mixed Data Type Records in aRecordEnumeration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337

    Searching Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345Searching Single Data Type Records . . . . . . . . . . . . . . . . . . 345Searching Mixed Data Type Records . . . . . . . . . . . . . . . . . . 352

    RecordListener . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359Quick Reference Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360

    9 J2ME Database Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . 365Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367

    Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367Database Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368

    Identifying Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369Decomposing Attributes to Data . . . . . . . . . . . . . . . . . . . . . . 372Defining Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375Normalizing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377Grouping Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378Creating Primary Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380Functional Dependency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382Transitive Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382

    C o n t e n t s ix

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Front Matter

    P:\010Comp\CompRef8\710-9\fm.vpFriday, February 07, 2003 1:49:47 PM

    Color profile: Generic CMYK printer profileComposite Default screen

  • x J 2 M E : T h e C o m p l e t e R e f e r e n c e

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Front Matter

    Foreign Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383Referential Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384

    The Art of Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385An Index in Motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386Drawbacks of Using an Index . . . . . . . . . . . . . . . . . . . . . . . . 386Clustered Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387Derived Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388Selective Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388Exact Matches and Partial Matches . . . . . . . . . . . . . . . . . . . . 389Searching for Phonetic Matches . . . . . . . . . . . . . . . . . . . . . . 389

    10 JDBC Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391The Concept of JDBC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392JDBC Driver Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393

    Type 1 JDBC to ODBC Driver . . . . . . . . . . . . . . . . . . . . . . . . 393Type 2 Java/Native Code Driver . . . . . . . . . . . . . . . . . . . . . 394Type 3 JDBC Driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394Type 4 JDBC Driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394

    JDBC Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394Overview of the JDBC Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394

    Load the JDBC Driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395Connect to the DBMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395Create and Execute an SQL Statement . . . . . . . . . . . . . . . . . 395Process Data Returned by the DBMS . . . . . . . . . . . . . . . . . . 396Terminate the Connection to the DBMS . . . . . . . . . . . . . . . . 397

    Database Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398The Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398Timeout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401Connection Pool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402

    Statement Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403The Statement Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403PreparedStatement Object . . . . . . . . . . . . . . . . . . . . . . . . . . . 406CallableStatement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407

    ResultSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409Reading the ResultSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410Scrollable ResultSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411Specify Number of Rows to Return . . . . . . . . . . . . . . . . . . . 414Updatable ResultSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415

    Transaction Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419Savepoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421Batch Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422Keeping ResultSet Objects Open . . . . . . . . . . . . . . . . . . . . . . 424

    P:\010Comp\CompRef8\710-9\fm.vpFriday, February 07, 2003 1:49:47 PM

    Color profile: Generic CMYK printer profileComposite Default screen

  • C o n t e n t s xi

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Front Matter

    RowSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424Autogenerated Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426

    Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426ResultSet Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426

    Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428Quick Reference Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429

    11 JDBC and Embedded SQL . . . . . . . . . . . . . . . . . . . . . . . . 453Model Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454

    Model A Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455Model B Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457

    Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460Create a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460Drop a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462

    Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463Create an Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463Drop an Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465

    Inserting Data into Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465Insert a Row . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466Insert the System Date into a Column . . . . . . . . . . . . . . . . . 466Insert the System Time into a Column . . . . . . . . . . . . . . . . . 467Insert a Timestamp into a Column . . . . . . . . . . . . . . . . . . . . 467

    Selecting Data from a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468Select All Data from a Table . . . . . . . . . . . . . . . . . . . . . . . . . . 469Request One Column . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470Request Multiple Columns . . . . . . . . . . . . . . . . . . . . . . . . . . 471Request Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472Request Rows and Columns . . . . . . . . . . . . . . . . . . . . . . . . . 472AND, OR, and NOT Clauses . . . . . . . . . . . . . . . . . . . . . . . . . 473Join Multiple Compound Expressions . . . . . . . . . . . . . . . . . 474Equal and Not Equal Operators . . . . . . . . . . . . . . . . . . . . . . 475Less Than and Greater Than Operators . . . . . . . . . . . . . . . . 477Less Than or Equal to and Greater Than or Equal To . . . . 478Between Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479LIKE Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480IS NULL Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480DISTINCT Modifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481IN Modifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482

    Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483Number of Columns in ResultSet . . . . . . . . . . . . . . . . . . . . . 483Data Type of Column . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484

    P:\010Comp\CompRef8\710-9\fm.vpFriday, February 07, 2003 1:49:47 PM

    Color profile: Generic CMYK printer profileComposite Default screen

  • Name of Column . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484Column Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484

    Updating Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485Update Row and Column . . . . . . . . . . . . . . . . . . . . . . . . . . . 485Update Multiple Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486

    Deleting Data from a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490Delete a Row from a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . 490

    Joining Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491Join Two Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494Parent-Child Join . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495Multiple Comparison Join . . . . . . . . . . . . . . . . . . . . . . . . . . . 496Multitable Join . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497Create a Column Name Qualifier . . . . . . . . . . . . . . . . . . . . . 498Create a Table Alias . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499Inner and Outer Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500

    Calculating Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504SUM() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506AVG() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507MIN() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507MAX() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508COUNT() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508Count All Rows in a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . 509Retrieve Multiple Counts . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509Calculate a Subset of Rows . . . . . . . . . . . . . . . . . . . . . . . . . . 510NULLs and Duplicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511Calculate Without Using Built-in Functions . . . . . . . . . . . . 511

    Grouping and Ordering Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512GROUP BY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513Group Multiple Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514Conditional Grouping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515Working with NULL Columns . . . . . . . . . . . . . . . . . . . . . . . 516Sorting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516Sorting on Derived Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518

    Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519Create a Subquery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520Conditional Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521

    VIEWs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525Rules for Using VIEWs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525Create a VIEW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527Select Columns to Appear in the VIEW . . . . . . . . . . . . . . . . 528Create a Horizontal VIEW . . . . . . . . . . . . . . . . . . . . . . . . . . . 528Create a Multitable VIEW . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529Group and Sort VIEWs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529Modify a VIEW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530

    xii J 2 M E : T h e C o m p l e t e R e f e r e n c e

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Front Matter

    P:\010Comp\CompRef8\710-9\fm.vpFriday, February 07, 2003 1:49:48 PM

    Color profile: Generic CMYK printer profileComposite Default screen

  • C o n t e n t s xiii

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Front Matter

    Part IV

    J2ME Personal Information Manager Profile

    12 Personal Information Manager . . . . . . . . . . . . . . . . . . . . 535PIM Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536The Contact Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538The Event Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539The To Do Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541A Model PIM Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541Quick Reference Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557

    Part V

    J2ME Networking and Web Services

    13 Generic Connection Framework . . . . . . . . . . . . . . . . . . . 575The Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576

    Connection and Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577Hypertext Transfer Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580

    Creating an HTTP Connection . . . . . . . . . . . . . . . . . . . . . . . . 580Reading Data from an HTTP Connection . . . . . . . . . . . . . . 582The File Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589Socket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592

    Communication Management Using HTTP Commands . . . . . . . . . 597HttpConnection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598

    Session Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 608Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610

    Transmit as a Background Process . . . . . . . . . . . . . . . . . . . . . . . . . . . 610Quick Reference Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614

    14 Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617Web Services Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618

    The Tier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618Clients, Resources, and Components . . . . . . . . . . . . . . . . . . 620Accessing Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621

    J2EE Multi-Tier Web Services Architecture . . . . . . . . . . . . . . . . . . . . 621Client Tier Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624

    Classification of Clients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624Web Tier Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625Enterprise JavaBeans Tier Implementation . . . . . . . . . . . . . . . . . . . . 626Enterprise Information Systems Tier Implementation . . . . . . . . . . . 627

    P:\010Comp\CompRef8\710-9\fm.vpFriday, February 07, 2003 1:49:48 PM

    Color profile: Generic CMYK printer profileComposite Default screen

  • xiv J 2 M E : T h e C o m p l e t e R e f e r e n c e

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Front Matter

    Inside WSDL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629The WSDL Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629Types Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632Message Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632portType Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633Binding Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634Port Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635Service Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635

    J2ME MIDlets and Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 636JAX-RPC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 637Holder Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 637

    Remote Method Invocation Concept . . . . . . . . . . . . . . . . . . . . . . . . . 639Remote Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639

    SOAP Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639SOAP Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 640The SOAP Message and Delivery Structure . . . . . . . . . . . . 640

    WSDL and SOAP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641SOAP One-Way Transmission Primitive . . . . . . . . . . . . . . . 642SOAP Request-Response Transmission Primitive . . . . . . . 643SOAP Binding Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645SOAP Operation Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645SOAP Body Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 646SOAP Fault Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 646SOAP Header Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 646SOAP Address Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647

    WSDL and HTTP Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647Quick Reference Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649

    Appendix: Quick Reference Guide . . . . . . . . . . . . . . . . . 653

    Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721

    P:\010Comp\CompRef8\710-9\fm.vpFriday, February 07, 2003 1:49:48 PM

    Color profile: Generic CMYK printer profileComposite Default screen

  • Introduction

    Java technology has evolved from a programming language designed to createmachine-independent embedded systems into a robust, vendor-independent,machine-independent, server-side technology, enabling the corporate communityto realize the full potential of web-centric applications.

    Java began with the release of the Java Development Kit (JDK). It was obvious fromthe start that Java was on a fast track to becoming a solution to the problems ofmany corporate systems. More interface and libraries were extended in the JDK asthe corporate world demandedand receivedapplication programming interfaces(API) that addressed real-world situations.

    JDK API extensions fully integrated into the JDK with the release of the Java 2Standard Edition (J2SE) of the JDK. J2SE contains all the APIs needed to build industrialstrength Java applications. However, the corporate world felt J2SE lacked the strengthrequired for developing enterprise-wide applications and for servicing the needs ofdevelopers of mobile and embedded systems.

    Again the corporate community pushed Sun Microsystems, Inc. to revise Javatechnology to address needs of an enterprise. Sun Microsystems, Inc. then launchedthe Java Community Program (JCP) that brought together corporate users, vendors,and technologists to develop a standard for enterprise Java APIs. The result is the

    xv

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Front MatterFM:xv

    P:\010Comp\CompRef8\710-9\fm.vpFriday, February 07, 2003 1:49:48 PM

    Color profile: Generic CMYK printer profileComposite Default screen

  • xvi J 2 M E : T h e C o m p l e t e R e f e r e n c e

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Front Matter

    Java 2 Platform Enterprise Edition, commonly referred to as Java 2 Enterprise Edition(J2EE), and the Java 2 Micro Edition (J2ME).

    Enterprise systems traditionally are designed using the client/server model, whereclient-side systems request processing from service-side systems. However, enterprisesystems were undergoing their own evolution. A new model called Web services graduallyreplaced the client/server model in corporations.

    Application programmers assembled applications from an assortment of processingcomponents called Web services. Each Web service was independent from other Webservices and independent from applications. A client-side application communicateswith a middle-tier, server-side application, which in turns interacts with the necessaryWeb services that are also located on the server side.

    With the adoption of the Web services model in corporations, the JCP realized thatJ2ME must also go through another evolutionary cycle. With the introduction of newspecifications, the Java community has merged J2ME technology with Web servicestechnology.

    In addition to the acceptance of Web services, corporations are also seeking to mergemobile technology such as Personal Digital Assistants and cellular phones withcorporate mainstream applications. J2ME, with the new PIM API, enables developersto create sophisticated, wireless applications that have direct access to native PDAdatabases. This enables corporate executives to use corporations PDA systems tointeract with data mantained by PDA native applications.

    Whats InsideThis book covers in detail all aspect of J2ME, Web services, PDA, and cellular phoneapplication development. The book is divided into these five parts:

    Part I: J2ME Basics

    Part II: J2ME User Interface

    Part III: J2ME Data Management

    Part IV: J2ME Personal Information Manager Profile

    Part V: J2ME Networking and Web Services

    Part I: J2ME BasicsThe new web-centric corporation is changing the way in which it delivers highly efficient,enterprise-wide distributive systems to meet the round-the-clock instantaneousdemand expected by thousands of concurrent usersanywhere at any time. The oldway of building enterprise systems wont solve todays corporate IT requirements.

    P:\010Comp\CompRef8\710-9\fm.vpFriday, February 07, 2003 1:49:48 PM

    Color profile: Generic CMYK printer profileComposite Default screen

  • I n t r o d u c t i o n xvii

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Front Matter

    Technologists at Sun Microsystems, Inc. and the Java Community Program rewrotethe way developers build large-scale, web-centric distributive systems by using Java 2Enterprise Edition (J2EE), and Java 2 Micro Edition (J2ME). J2EE addresses complexserver-side issues faced by programmers who develop these systems while J2MEaddresses the need to create mobile and embedded components that makes thesesystems accessible from other than the desktop.

    Part I of this book introduces you to basic concepts used in J2ME technology andin Web services technology. These concepts focus on four areas of interest, beginningwith an overview of J2ME that defines J2ME and illustrates J2MEs role in theevolutionary process of computer programming.

    The next area of interest examines the J2ME architecture. It is here where you rollup your sleeves and get your hands into the guts of J2ME to investigate how J2MEworks within the Web services infrastructure.

    At first glance, you might feel overwhelmed by the power of J2ME. However, thatfeeling is short-lived because the third area of interest in Part I discusses J2ME bestpractices, showing you commonly used design principles used by J2ME programmersto build advanced J2ME Web centric distributive systems.

    Part I concludes with a look at J2ME design patterns used to solve commonprogramming problems that crop up during the development of a J2ME application.After reading Part I youll have a solid basis for learning how to build your own J2MEapplications.

    Part II: J2ME User InterfaceNearly every J2ME application that you develop requires a way for a user to interactwith it unless the application is an embedded closed system. An embedded closedsystem such as those that control an automobiles engine doesnt require input fromthe user but instead receives input from electro-mechanical devices.

    A user interface for a J2ME application is similar to yet different from a user interfacethat you find on a desktop application. They are similar in that both display optionsavailable to the user and then receive and process the option selected by the user.However, a J2ME user interface is less sophisticated than those found on a desktopapplication because of the limited resources (i.e., screen size) that are available on aJ2ME device (i.e., cellular phone).

    In Part II youll learn database concepts of the J2ME user interface. Youll alsoexplore the details of building a J2ME user interface for your application.

    Part III: J2ME Data ManagementAt the center of nearly every J2ME application is a repository of information that isaccessed and manipulated by both service-side components, such as Web services,and client-side applications. A repository is a database management system thatstores, retrieves, and maintains the integrity of information stored in its databases.

    P:\010Comp\CompRef8\710-9\fm.vpFriday, February 07, 2003 1:49:48 PM

    Color profile: Generic CMYK printer profileComposite Default screen

  • xviii J 2 M E : T h e C o m p l e t e R e f e r e n c e

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Front Matter

    A J2ME application uses Java data objects, JDBC, and other technology that isnecessary to interact with a database management system to provide information tothe J2ME application.

    In Part III youll learn database concepts in relation to Java data objects. Youll alsoexplore the details of JDBC, which is used to connect to and interact with popularand some not so populardatabase management systems. And youll also learn howto create and send requests for information and integrate the results of a request intoyour J2ME application.

    Part IV: J2ME Personal Information Manager ProfileMany corporations have practically made PDAs the de facto standard as a mobilecommunicator, especially since PDA and cell phone technologies have merged, causinga blur between PDAs and cell phones. That is, a PDA can be used as a cell phone andcell phones have incorporated PDA applications.

    Until recently, J2ME applications lacked the capability to interact with native PDAdatabases such as those used to store calendar, to-do list, and address information.

    The Java Community Process released a new Personal Information Manager (PIM)API, which is used to develop sophisticated J2ME applications. This enables J2MEapplications to interact with the J2ME devices personal information database, whichis used by the devices address book, notepad, and calendar applications.

    In Part IV of this book youll explore this API and learn how to implement it in yourJ2ME application.

    Part V: J2ME Networking and Web ServicesThe glue that enables J2ME applications to interact with external applications, includingserver-side components, is networking capabilities. In Part V youll learn how toimplement routines that take advantage of a J2ME devices network features to opencommunications with other applications using a hard-wire or wireless networkconnection.

    Youll also learn how to utilize Web services to expand the horizon of your J2MEapplication. Web services is a web of services where services are software buildingblocks that are available on a network from which programmers can efficiently createlarge-scale distributive systems.

    You wont learn how to create Web services, but you will learn how to utilize themto increase the functionality of your J2ME application beyond the limited resourcesfound on a J2ME device. In Part V, youll also learn about Service Oriented ArchitectureProtocol (SOAP), Universal Description, Discovery, and Web Services DescriptionLanguage (WSDL), and how to implement them in your J2ME application.

    P:\010Comp\CompRef8\710-9\fm.vpFriday, February 07, 2003 1:49:48 PM

    Color profile: Generic CMYK printer profileComposite Default screen

  • A Book for All ProgrammersJ2ME: The Complete Reference is designed for all Java programmers, regardless of theirexperience level. It does assume, however, that a reader is able to create at least aruntime Java program. If you are just learning Java, this book will make an excellentcompanion to any Java tutorial and serve as a source of answers to your specificquestions. Experienced Java, J2EE, and J2ME pros will find the coverage of the manynew Web services features.

    Dont Forget: Code on the WebRemember, the source code for all of the programs in this book is available free ofcharge on the Web at http://www.osborne.com. Downloading this code prevents youfrom having to type in the examples.

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Front Matter

    I n t r o d u c t i o n xix

    P:\010Comp\CompRef8\710-9\fm.vpFriday, February 07, 2003 1:49:48 PM

    Color profile: Generic CMYK printer profileComposite Default screen

  • Complete Reference / J2ME: TCR / Keogh / 222710-9Blind Folio xx

    P:\010Comp\CompRef8\710-9\fm.vpFriday, February 07, 2003 1:49:48 PM

    Color profile: Generic CMYK printer profileComposite Default screen

    This page intentionally left blank

  • Part IJ2ME Basics

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Chapter 1Blind Folio 1

    P:\010Comp\CompRef8\710-9\ch01.vpThursday, February 06, 2003 10:52:09 AM

    Color profile: Generic CMYK printer profileComposite Default screen

  • Complete Reference / J2ME: TCR / Keogh / 222710-9Blind Folio 2

    P:\010Comp\CompRef8\710-9\ch01.vpThursday, February 06, 2003 10:52:09 AM

    Color profile: Generic CMYK printer profileComposite Default screen

    This page intentionally left blank

  • Chapter 1J2ME Overview

    3

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Chapter 1

    P:\010Comp\CompRef8\710-9\ch01.vpThursday, February 06, 2003 10:52:10 AM

    Color profile: Generic CMYK printer profileComposite Default screen

  • The term computer conjures many images, such as desktop and laptop computersand servers stored in some highly protected remote location. And while theseimages accurately portray a computer, there are many more computers that lackthe familiar computer shape but contain the same basic components found in desktopand laptop computers. Cell phones, digital set-top boxes for cable television, car navigationsystems, pagers, and personal digital assistants are all computers. And computers arealso used to control the operation of automobiles, industrial equipment, and householdappliances. This new breed of computers, referred to as small computing devices, isdistinguishable from more traditional computers by their reduced resource availability.Resources such as memory, permanent storage, and power are plentiful in traditionalcomputers but are precious in small computing devices.

    Along with the new breed of computers came a new platform, on which developerscan build and implement programs to control small computing devices. The platform iscalled Java 2 Micro Edition (J2ME). Youll be introduced to J2ME in this chapter.

    Java 2 Micro Edition and the World of JavaThe computer revolution of the 1970s increased the demand for sophisticated computersoftware to take advantage of the ever-increasing capacity of computers to process data.The C programming language became the linchpin that enabled programmers to buildsoftware that was just as robust as the computer it ran on.

    As the 1980s approached, programmers were witnessing another spurt in the evolutionof programming language. Computer technology advanced beyond the capabilities ofthe C programming language. The problem wasnt new. It occurred previously andcaused the demise of generations of programming languages. The problem was thatprograms were becoming too complicated to design, write, and manage to keep up withthe capabilities of computers. It was around this time that a design concept basedon Simula 67 and Smalltalk (from the late 1960s) moved programming to the nextevolutionary step. This was the period when object-oriented programming (OOP), andwith it a new programming language called C++, took programmers by storm.

    In 1979, Bjarne Stroustrup of Bell Laboratories in New Jersey enhanced the Cprogramming language to include object-oriented features. He called the language C++.(The ++ is the incremental operator in the C programming language.) C++ is truly anenhancement of the C programming language, and it began as a preprocessor languagethat was translated into C syntax before the program was processed by the compiler.

    Stroustrup built on the concept of a class (taken from Simula 67 and Smalltalk), fromwhich instances of objects are created. A class contains data members and memberfunctions that define an objects data and functionality. He also introduced the conceptof inheritance, which enabled a class to inherit some or all data members and memberfunctions from one or more other classesall of which complements the concepts ofobject-oriented programming. By 1988, ANSI officials standardized Stroustrups C++specification.

    4 J 2 M E : T h e C o m p l e t e R e f e r e n c e

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Chapter 1

    P:\010Comp\CompRef8\710-9\ch01.vpThursday, February 06, 2003 10:52:10 AM

    Color profile: Generic CMYK printer profileComposite Default screen

  • Enter JavaJust as C++ was becoming the language of choice for building industrial-strengthapplications, another growth spurt in the evolution of programming language wasbudding, fertilized by the latest disruptive technologythe World Wide Web. TheInternet had been a well-kept secret for decades before the National Science Foundation(who oversaw the Internet) removed barriers that prevented commercialization. Until 1991when it was opened to commerce, the Internet was the almost exclusive domain ofgovernment agencies and the academic community. Once the barrier to commercializationwas lifted, the World Wide Webone of several services offered on the Internetbecame a virtual community center where visitors could get free information aboutpractically anything and browse through thousands of virtual stores.

    Browsers power the World Wide Web. A browser translates ASCII text files writtenin HTML into an interactive display that can be interpreted on any machine. As long asthe browser is compatible with the correct version of HTML and HTTP implementation,any computer running the browser can use the same HTML document without havingto modify it for a particular type of computer, which was something unheard of at thetime. Programs written in C or C++ are machine dependent and cannot run on a differentmachine unless the program is recompiled.

    The success of the Internet gave renewed focus to developing a machine-independentprogramming language. And the same year the Internet was commercialized, fivetechnologists at Sun Microsystems set out to do just that. James Gosling, Patrick Naughton,Chris Warth, Ed Frank, and Mike Sheridan spent 18 months developing the programminglanguage they called Oak, which was renamed Java when this new language made itsdebut in 1995. Java went through numerous iterations between 1991 and 1995, duringwhich time many other technologists at Sun made substantial contributions to thelanguage. These included Bill Joy, Arthur van Hoff, Jonathan Payne, Frank Yelin, andTim Lindholm.

    Although Java is closely associated with the Internet, it was developed as a languagefor programming software that could be embedded into electronic devices regardless ofthe type of CPU used by the device. This is known as the EmbeddedJava platform andis in continuous use today for closed systems.

    The Java team from Sun succeeded in creating a portable programming language,something that had eluded programmers since computers were first programmed. Theirsuccess, however, was far beyond their wildest dreams. The same concept used to makeJava programs portable to electronic devices also could be used to make Java programsrun on computers running Microsoft Windows, UNIX, and Macintosh.

    Timing was perfect. The Internet/intranet had whetted corporate Americas appetitefor cost-effective, portable programs that could replace mission-critical applicationswithin the corporation. And Java had proven itself as a programming language usedto successfully develop machine-independent applications.

    It was in the mid-1990s when the team from Sun realized that Java could be easilyadapted to develop software for the Internet/intranet. And toward the turn of the century,

    C h a p t e r 1 : J 2 M E O v e r v i e w 5

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Chapter 1

    J2M

    EB

    ASIC

    S

    P:\010Comp\CompRef8\710-9\ch01.vpThursday, February 06, 2003 10:52:10 AM

    Color profile: Generic CMYK printer profileComposite Default screen

  • many corporations embraced Java and began replacing legacy applicationsmany ofwhich were written in C and C++with Java Internet/intranet-enabled applications.In keeping with the genealogical philosophy that only the dominant genes are passedon to the next generation, the Java development team at Sun incorporated the best ofSmalltalk (automatic garbage collection) and C++ into Java and left out features of C++that were inefficient and not programmer friendly. The team also created new featuresthat gave Java the dynamics necessary for Internet-based programming.

    Java Virtual MachineWriting Java programs is similar to writing C++ programs in that the programmer writessource code that contains instructions into an editor, or in an integrated developmentenvironment, and then the source code is compiled. However, thats where Java and C++part ways. The compiling and linking process of a C++ program results in an executablethat can be run on an appropriate machine. In contrast, the Java compiler converts Javasource code into bytecode that is executed by the Java Virtual Machine (JVM).

    Machine-specific instructions are not included in bytecode. Instead, they already residein the JVM, which is machine specific. This means that the bytecode might contain fewerinstructions that need to be translated than a comparable C++ program.

    Although the Java compiler generates bytecode that must be interpreted by the JVMat run time, the number of instructions that need translation are usually minimal andhave already been optimized by the Java compiler.

    J2EE and J2SEJava itself has undergone an evolution that rivals the evolution of programming languagesin general. Originally designed for programs that control electronic devices, Java madewaves in the Internet development community by providing a means to give intelligenceto passive web pages. The Java development teams design has made Java the programminglanguage of choice for programming enterprise-wide, web-centric applications.

    Information technology departments had always sought ways to create cost-effectivecomputer applications. One approach is client/server architecture that uses a two-tierarchitecture in which client-side software requests services from server-side software.For example, software running on the client captures a request for information froma user and then formats the request into a query that is sent over the network to thedatabase server for processing. The database server then transmits the requested datato the client, where software presents data to the user (Figure 1-1).

    Increasingly, back-end systems and infrastructure grew as information technologydepartments streamlined operations to deliver information and technology services tothe desktop. Client/server architecture exploded from a two-tier architecture to a multi-tier web services architecture in which a clients request to server-side software generatesrequests to special software called a web service (Figure 1-2). This is very similar to askinga travel agent to arrange for your vacation. The travel agent contacts hotels, airlines, thecar rental company, restaurants, and other vendors that are necessary to fulfill your request.

    6 J 2 M E : T h e C o m p l e t e R e f e r e n c e

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Chapter 1

    P:\010Comp\CompRef8\710-9\ch01.vpThursday, February 06, 2003 10:52:10 AM

    Color profile: Generic CMYK printer profileComposite Default screen

  • C h a p t e r 1 : J 2 M E O v e r v i e w 7

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Chapter 1

    J2M

    EB

    ASIC

    S

    Figure 1-1. In client/server architecture, client-side software sends requeststo server-side software for processing.

    Figure 1-2. Multi-tier web services architecture uses server-side software to receiverequests from client-side software that is processed by web services.

    P:\010Comp\CompRef8\710-9\ch01.vpThursday, February 06, 2003 10:52:11 AM

    Color profile: Generic CMYK printer profileComposite Default screen

  • Although multi-tier architecture provides services efficiently, it also complicates thedesign, creation, debugging, distribution, and maintenance of an application because aprogrammer must be assured that all tiers work together. However, the Java developmentteam enhanced the capabilities of Java to dramatically reduce the complexity of developinga multi-tier application.

    The team grouped features of Java into three editions, each having a softwaredevelopment kit (SDK). The original edition of Java, called the Java 2 Standard Edition(J2SE), consists of application programming interfaces (APIs) needed to build aJava application or applet. The Java 2 Micro Edition (J2ME) contains the API used to createapplications for small computing devices, including wireless Java applications. And theJava 2 Enterprise Edition (J2EE), an embellished version of the J2SE to accommodaten-tier architecture, has the API to build applications for multi-tier architecture.

    The Birth of J2EEJava is an evolving programming language that began with the release of the JavaDevelopment Kit (JDK). During this evolutionary process, the Java development teamincluded more interfaces and libraries as programmers demanded new APIs. These newfeatures were called extensionsAPIs that were add-ons to the JDK. Sun Microsystemsincorporated these extensions into a new Java development kit called J2SE.

    Information technology departments of corporations look toward web-centricapplications as a way to economize while offering streamlined services to employeesand customers. An increased emphasis was placed on server-side programming and ondevelopment of vendor-independent APIs to access server-side systems. Sun respondedby creating the Java Community Process Program that invited corporate users, vendors,and technologists to develop a standard for enterprise Java APIs. The Java CommunityProcess Program effort resulted in J2EE.

    J2EE is a combination of several technologies that offer a cohesiveness to bondtogether server-side systems and services to produce an industrial-strength scalableenvironment within which web-centric applications can thrive. A critical ingredientin the development of J2EE is the collaborative environment fostered by Sun, withinwhich vendors and technologists come together in the Java Community Process Programto create and implement Java-based technologies.

    Back to the Future: J2MERemember that Java began as a programming language to create programs for embeddedsystemsmicrocomputers found in consumer and industrial products such as thoseused to control automobiles and appliances. The development team at Sun worked onJava in the early 1990s to address the programming needs of the fledgling embeddedcomputer market, but that effort was sidetracked by more compelling opportunitiespresented by the Internet.

    As those opportunities were addressed, a new breed of portable communicationsdevices opened other opportunities at the turn of the century. Cell phones expanded

    8 J 2 M E : T h e C o m p l e t e R e f e r e n c e

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Chapter 1

    P:\010Comp\CompRef8\710-9\ch01.vpThursday, February 06, 2003 10:52:12 AM

    Color profile: Generic CMYK printer profileComposite Default screen

  • from voice communications devices to voice and text communications devices. Pocketelectronic telephone directories evolved into personal digital assistants. Chipmakerswere releasing new products at this time that were designed to transfer computing powerfrom a desktop computer into mobile small computers that controlled gas pumps, cabletelevision boxes, and an assortment of other appliances.

    The time was right for the next evolution of Java. However, instead of beefing up Javawith additional APIs, the team at Sun, along with the Java Community Process Program,dismantled both the Java programming language and the Java Virtual Machine. Theystripped down Java APIs and the JVM to the minimum coding required to provideintelligence to embedded systems and microcomputer devices. This was necessarybecause of resource constraints imposed upon the hardware design of these devices. Theresult of their efforts is J2ME. J2ME is a reduced version of the Java API and Java VirtualMachine that is designed to operate within the sparse resources available in the newbreed of embedded computers and microcomputers.

    Inside J2MEJ2ME made its debut at the JavaOne Developers Conference in mid-1999 and is targetedto developers of intelligent wireless devices and small computing devices who need toincorporate cross-platform functionality in their products.

    Consumers of mobile and small computing devices have high performanceexpectations for these devices. They demand quick response time, compatibility withcompanion services, and full-featured applications in a small computing device.Consumers expect the same software and capabilities found on their desktop and laptopcomputers to be available on their cell phones and personal digital assistants.

    To meet these expectations, developers have to rethink the way they build computersystems. Developers need to harness the power of existing front-end and back-endsoftware found on business computers and transfer this power onto small, mobile, andwireless computing devices. J2ME enables this transformation to occur with minimalmodifications, assuming that applications are scalable in design so that an applicationcan be custom-fitted to resources available on a small computing device.

    Developers seeking to build applications that run on cell phones, personal digitalassistants, and various consumer and industrial appliances must strike a balance betweena thick client and a thin client. A thick client is front-end software that contains the logicto handle a sizable amount of data processing for the system (Figure 1-3). A thin client isfront-end software that depends on back-end software for much of the system processing(Figure 1-4).

    Developers must determine the minimum client processing that will meet the endusers expectations of quick response time that is feasible within the limited resourcesavailable on the small computing device. Youll learn how to make this decision inChapter 4.

    C h a p t e r 1 : J 2 M E O v e r v i e w 9

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Chapter 1

    J2M

    EB

    ASIC

    S

    P:\010Comp\CompRef8\710-9\ch01.vpThursday, February 06, 2003 10:52:12 AM

    Color profile: Generic CMYK printer profileComposite Default screen

  • 10 J 2 M E : T h e C o m p l e t e R e f e r e n c e

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Chapter 1

    Figure 1-3. Thick client applications handle most processing locally.

    Figure 1-4. Thin client applications rely on server-side software for nearly allprocessing.

    P:\010Comp\CompRef8\710-9\ch01.vpThursday, February 06, 2003 10:52:13 AM

    Color profile: Generic CMYK printer profileComposite Default screen

  • Lets say that a wireless small computing device is used to transact orders on thefloor of a stock exchange. The wireless device has software to handle user interactionssuch as displaying an electronic form on the screen, collecting user input, processing theinput, and displaying results of the processing on the screen. The order form is displayedon the screen, and the user enters information into the order form using various inputconventions commonly found in small wireless devices. The device collects the orderinformation and then processes the order using a combination of software on the wirelessdevice and software running on a back-end system that receives the order througha wireless connection.

    Processing on the wireless device might involve two steps: First the software performsa simple validation process to assure that all fields on the form contain information. Nextthe order is transmitted to the back-end system. The back-end system handles adjustingaccount balances and other steps involved in processing the order. A confirmation noticeis returned by the back-end system to the wireless device, which displays the confirmationnotice on the screen (Figure 1-5).

    A key benefit of using J2ME is that J2ME is compatible with all Java-enabled devices.A Java-enabled device is any computer that runs the Java Virtual Machine. Ericsson,

    C h a p t e r 1 : J 2 M E O v e r v i e w 11

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Chapter 1

    J2M

    EB

    ASIC

    S

    Figure 1-5. A J2ME application is a balance between local and server-sideprocessing.

    P:\010Comp\CompRef8\710-9\ch01.vpThursday, February 06, 2003 10:52:14 AM

    Color profile: Generic CMYK printer profileComposite Default screen

  • Motorola, Nextel, Nokia, Panasonic, and RIM all have Java-enabled devices. In addition,J2ME maintains the powerful security features found in the Java language and enableswireless and small computing devices to access resources that are within an organizationsfirewall.

    How J2ME Is OrganizedTraditional computing devices use fairly standard hardware configurations such asa display, keyboard, mouse, and large amounts of memory and permanent storage.However, the new breed of computing devices lacks hardware configuration continuityamong devices. Some devices dont have a display, permanent storage, keyboard,or mouse. And memory availability is inconsistent among small computing devices.

    The lack of uniform hardware configuration among the small computing devices posesa formidable challenge for the Java Community Process Program, which is charged withdeveloping standards for the JVM and the J2ME for small computing devices.

    J2ME must service many different kinds of small computing devices, including screen-phones, digital set-top boxes used for cable television, cell phones, and personal digitalassistants. The challenge for the Java Community Process Program is to develop a Javastandard that can be implemented on small computing devices that have nonstandardhardware configurations.

    The Java Community Process Program has used a twofold approach to addressingthe needs of small computing devices. First, they defined the Java run-time environmentand core classes that operate on each device. This is referred to as the configuration. Aconfiguration defines the Java Virtual Machine for a particular small computing device.There are two configurations, one for handheld devices and the other for plug-in devices.Next, the Java Community Process Program defined a profile for categories of smallcomputing devices. A profile consists of classes that enable developers to implementfeatures found on a related group of small computing devices.

    J2ME ConfigurationsThere are two configurations for J2ME as of this writing. These are Connected LimitedDevice Configuration (CLDC) and the Connected Device Configuration (CDC). The CLDCis designed for 16-bit or 32-bit small computing devices with limited amounts of memory.CLDC devices usually have between 160KB and 512KB of available memory and arebattery powered. They also use an inconsistent, small-bandwidth network wirelessconnection and may not have a user interface. CLDC devices use the KJava VirtualMachine (KVM) implementation, which is a stripped-down version of the JVM. CLDCdevices include pagers, personal digital assistants, cell phones, dedicated terminals, andhandheld consumer devices with between 128KB and 512KB of memory.

    CDC devices use a 32-bit architecture, have at least two megabytes of memoryavailable, and implement a complete functional JVM. CDC devices include digital set-topboxes, home appliances, navigation systems, point-of-sale terminals, and smart phones.

    12 J 2 M E : T h e C o m p l e t e R e f e r e n c e

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Chapter 1

    P:\010Comp\CompRef8\710-9\ch01.vpThursday, February 06, 2003 10:52:14 AM

    Color profile: Generic CMYK printer profileComposite Default screen

  • J2ME ProfilesA profile consists of Java classes that enable implementation of features for either aparticular small computing device or for a class of small computing devices. Smallcomputing technology continues to evolve, and with that, there is an ongoing processof defining J2ME profiles. Seven profiles have been defined as of this writing. These arethe Foundation Profile, Game Profile, Mobile Information Device Profile, PDA Profile,Personal Profile, Personal Basis Profile, and RMI Profile.

    The Foundation Profile is used with the CDC configuration and is the core fornearly all other profiles used with the CDC configuration because the FoundationProfile contains core Java classes.

    The Game Profile is also used with the CDC configuration and contains thenecessary classes for developing game applications for any small computingdevice that uses the CDC configuration.

    The Mobile Information Device Profile (MIDP) is used with the CLDCconfiguration and contains classes that provide local storage, a user interface,and networking capabilities to an application that runs on a mobile computingdevice such as Palm OS devices. MIDP is used with wireless Java applications.

    The PDA Profile (PDAP) is used with the CLDC configuration and contains classesthat utilize sophisticated resources found on personal digital assistants. Thesefeatures include better displays and larger memory than similar resources foundon MIDP mobile devices (such as cell phones).

    The Personal Profile is used with the CDC configuration and the Foundation Profileand contains classes to implement a complex user interface. The FoundationProfile provides core classes, and the Personal Profiles provide classes to implementa sophisticated user interface, which is a user interface that is capable of displayingmultiple windows at a time.

    The Personal Basis Profile is similar to the Personal Profile in that it is used withthe CDC configuration and the Foundation Profile. However, the Personal BasisProfile provides classes to implement a simple user interface, which is a userinterface that is capable of displaying one window at a time.

    The RMI Profile is used with the CDC configuration and the Foundation Profileto provide Remote Method Invocation classes to the core classes contained in theFoundation Profile.

    There will likely be many profiles as the proliferation of small computing devicescontinues. Industry groups within the Java Community Process Program (java.sun.com/aboutjava/communityprocess) define profiles. Each group establishes the standardprofile used by small computing devices manufactured by that industry.

    A CDC profile is defined by expanding upon core Java classes found in the FoundationProfile with classes specifically targeted to a class of small computing device. These

    C h a p t e r 1 : J 2 M E O v e r v i e w 13

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Chapter 1

    J2M

    EB

    ASIC

    S

    P:\010Comp\CompRef8\710-9\ch01.vpThursday, February 06, 2003 10:52:14 AM

    Color profile: Generic CMYK printer profileComposite Default screen

  • device-specific classes are contained in a new profile that enables developers to createindustrial-strength applications for those devices. However, if the Foundation Profileis specific to CDC, not all profiles are expanded upon the core classes found in theFoundation Profile.

    Keep in mind that applications can access a small computing devices software andhardware features only if the necessary classes to do so are contained in the JVM and inthe profile used by the developer.

    J2ME and Wireless DevicesWith the dramatic increase and sophistication of mobile communications devices such ascell phones came demand for applications that can run on those devices. Consumers andcorporations want to expand mobile communications devices from voice communicationsto applications traditionally found on laptops and PCs. They want to send and receiveemail, store and retrieve personal information, perform sophisticated calculations, andplay games.

    Developers, mobile communications device manufacturers, and mobile networkproviders are anxious to fill this need, but there is a serious hurdle: mobile communicationsdevices utilize a number of different application platforms and operating systems. Withouttweaking the code, an application written for one device cannot run on another device.Mobile communications devices lack a standard application platform and operatingsystem, which has made developing applications for mobile communications devicesa risky economic venture for developers.

    The lack of standards is nothing new to computing or to any developing technology.Traditionally, manufacturers of hardware devices try to corner the market and enforce theirown proprietary standard as the de facto standard for the industry. Usually one upstartsucceeds, as in the case of Microsoft. Other times, industry leaders form a consortium,such as the Java Community Process Program, to collectively develop a standard.

    The Wireless Application Protocol (WAP) forum became the initial industry groupthat set out to create standards for wireless technology. Ericsson, Motorola, Nokia, andUnwired Planet formed the WAP forum in 1997, and it has since grown to include nearlyall mobile device manufacturers, mobile network providers, and developers. The WAPforum created mobile communications device standards referred to as the WAP standard.The WAP standard is an enhancement of HTML, XML, and TCP/IP. One element of thisstandard is the Wireless Markup Language specification, which consists of a blend ofHTML and XML and is used by developers to create documents that can be displayed bya microbrowser. A microbrowser is a diminutive web browser that operates on a mobilecommunications device.

    The WAP standard also includes specifications for a Wireless Telephony ApplicationInterface (WTAI) specification and the WMLScript specification. WTAI is used to createan interface for applications that run on a mobile communications device. WMLScriptis a stripped-down version of JavaScript.

    14 J 2 M E : T h e C o m p l e t e R e f e r e n c e

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Chapter 1

    P:\010Comp\CompRef8\710-9\ch01.vpThursday, February 06, 2003 10:52:14 AM

    Color profile: Generic CMYK printer profileComposite Default screen

  • While the WAP forum provided the framework within which developers can buildmobile communications device applications, they still had to overcome a commonhurdle found in every rapidly developing technology. The sophistication of mobilecommunications devices, phenomenal growth of the market, and high demand forindustrial-strength mobile communications applications out-paced the ability to defineand implement new mobile communications device standards.

    Many sophisticated applications designed for mobile communications devices requirethe device to process information beyond the capabilities of the WAP specification. J2MEprovided the standard to fill this gap. For example, a sales representative wants to checkavailable flights and hotel accommodations, purchase an airline ticket, book the hoteland car rental, and then send the itinerary to a client, all while sitting in a taxi in traffic.The sales representative also wants the itinerary stored on the mobile communicationsdevice and retrieved during the trip.

    J2ME applications referred to as a MIDlet can run on practically any mobilecommunications device that implements a JVM and MIDP. This encourages developersto invest time and money in building applications for mobile communications deviceswithout the risk that the application is device dependent. However, J2ME isnt seen asa replacement for the WAP specification because both are complementary technologies.Developers whose applications are light-client based continue to use WML andWMLScript. Developers turn to J2ME for heavier clients that require sophisticatedprocessing on the mobile communications device.

    What J2ME IsntThe hype about any technology can cause misperceptions about the capabilities of anevolving technology, and J2ME isnt immune to such misunderstandings. Therefore, it isimportant to understand the limitations of J2ME.

    Although J2ME is J2SE without some classes, developers shouldnt assume thatexisting Java applications would run in the J2ME environment without requiringmodification to the code. The write-once-run-anywhere philosophy of Java is a bitoverstated when it comes to J2ME because of resource constraints imposed by smallcomputing devices.

    Some J2SE applications require classes that are not available in J2ME. Likewise,resources required by the J2SE application may not be available on the small computingdevice. This means that developers must expect to test existing J2SE applications in theJ2ME environment and probably pare down the application to run using limited resources.

    Another misconception about J2ME is the Java Virtual Machine implementation onthe small computing device. Small computing devices use one of two Java Virtual Machineimplementations. Devices that use the CDC configuration use the full Java VirtualMachine implementation, while devices that use the CLDC configuration use the KJavaVirtual Machine implementation.

    C h a p t e r 1 : J 2 M E O v e r v i e w 15

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Chapter 1

    J2M

    EB

    ASIC

    S

    P:\010Comp\CompRef8\710-9\ch01.vpThursday, February 06, 2003 10:52:14 AM

    Color profile: Generic CMYK printer profileComposite Default screen

  • A MIDlet is not invoked the same way as a J2SE application is invoked because manysmall computing devices dont have a command prompt. MIDlets are controlled byapplication management software (AMS). The manufacturer of a small computing deviceprovides AMS, although third-party vendors might also create AMS. AMS interacts withnative operations of a small computing device and controls the life cycle of a MIDlet.The life cycle consists of installation and upgrades as well as version managementand uninstalling the application. Likewise, AMS is responsible for starting, managingexecution, and stopping the MIDlet.

    Other Java Platforms forSmall Computing DevicesJ2ME isnt the only Java platform designed for small computing devices. Other JavaplatformsEmbeddedJava, JavaCard, and PersonalJavapredate J2ME.

    EmbeddedJava is the Java platform used for small computing devices that arededicated to one purpose and have a 32-bit processor and 512KB of ROM and RAM.EmbeddedJava is based on JDK 1.1 and is being replaced by the CDLC configuration. Formore information about EmbeddedJava, visit java.sun.com/products/embeddedjava.

    JavaCard is the Java platform used for smart cards, the smallest computing devicethat supports Java. The JavaCard VM runs on small computing devices that have16KB of nonvolatile memory and 512 bytes of volatile memory. However, unlike theEmbeddedJava platform, there isnt any movement to replace JavaCard with J2ME becauseof the resource constraints of the current generation of smart cards. Future smart cardgenerations will probably have great resources available and be compatible with theCDLC configuration. You can find more information about JavaCard in java.sun.com/products/javacard.

    PersonalJava is the Java platform used for small computing devices that have amaximum of 2MB of ROM and a minimum of 1MB of RAM, such as large PDAs andmobile communications devices. PersonalJava uses JDK 1.1.8 and the JVM and will bereplaced by the CDC configuration and the Personal Basis Profile and Personal Profile.More information about PersonalJava is available at java.sun.com/products/personaljava.

    16 J 2 M E : T h e C o m p l e t e R e f e r e n c e

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Chapter 1

    P:\010Comp\CompRef8\710-9\ch01.vpThursday, February 06, 2003 10:52:15 AM

    Color profile: Generic CMYK printer profileComposite Default screen

  • Chapter 2Small ComputingTechnology

    17

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Chapter 2

    P:\010Comp\CompRef8\710-9\ch02.vpThursday, February 06, 2003 11:06:44 AM

    Color profile: Generic CMYK printer profileComposite Default screen

  • Beam me up Scotty is a famous line from Star Trek. Captain Kirk made thisrequest using a clamshell-shaped communicator. Granted there has yet to beanyone transported on light waves, but clamshell-shaped communicators areused everyday. We call it a cellular telephone.

    Today we can speak with anyone, anywhere, anytime. And tomorrow well read anybook, shop in any store, check up on our kids and our house, pay our bills, and do moreby using small computing devices and mobile communications devices. These deviceshave already changed our lives. A friend of mine is responsible for building alliancesamong corporate executives and investment bankers. Very few people know where myfriend physically worksand no one really cares. Calls are directed to a cellular telephoneand roll over to voice mail when there is no answer. Emails are retrieved using a laptopthat is sometimes connected to the email server through a traditional telephone line andother times linked using a wireless connection. My friends office is where my friend is.

    This is all made possible by software developers exploiting features of small computingand mobile computing devices. Before you learn to build those applications, you shouldbecome familiar with the technology that makes this possible. In this chapter youllexplore the technology used in small computing devices and mobile computing devices.

    Wireless TechnologyWireless technology that is used in small computing devices and mobile communicationsdevices is the same radio technology Guglielmo Marconi used to provide an alternativecommunication means to the telegraph and the telephone.

    Radio technology is based on the wave phenomenon. A wave is a characteristic ofvibrating molecules, which you see whenever you move a knife up and down in the stillwater of a dishpan (Figure 2-1). The force of the knife against the surface of the watercauses water molecules to vibrate and form a wave along the surface of the water.

    The force used to propel the knife determines the wave height. The greater the force,the higher the wave and the greater the distance the wave travels across the surface of thewater. The number of times the knife is moved up and down in the water determinesthe frequency of the wave. Each time the knife is plunged into the water another waveis generated, causing a rippling effect across the waters surface.

    18 J 2 M E : T h e C o m p l e t e R e f e r e n c e

    Complete Reference / J2ME: TCR / Keogh / 222710-9 / Chapter 2

    Figure 2-1. Moving a knife up and down in water causes the formation of a wave.

    P:\010Comp\CompRef8\710-9\ch02.vpThursday, February 06, 2003 11:06:44 AM

    Color profile: Generic CMYK printer profileComposite Default screen

  • Waves are measured in two ways: by the wave height and by the wave frequency. Thewave height is referred to as the waves amplitude, and t