ebook img

Java Data Access: JDBC, JNDI, and JAXP PDF

389 Pages·2002·2.549 MB·English
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 Java Data Access: JDBC, JNDI, and JAXP

Java Data Access—JDBC, JNDI, and JAXP Todd M. Thomas Published by M&T Books An imprint of Hungry Minds, Inc. 909 Third Avenue New York, NY 10022 http://www.hungryminds.com/ Copyright © 2002 Hungry Minds, Inc. All rights reserved. No part of this book, including interior design, cover design, and icons, may be reproduced or transmitted in any form, by any means (electronic, photocopying, recording, or otherwise) without the prior written permission of the publisher. Library of Congress Control Number: 2001092891 ISBN: 0-7645-4864-8 Printed in the United States of America 10 9 8 7 6 5 4 3 2 1 1O/RR/QR/QS/IN Distributed in the United States by Hungry Minds, Inc. Distributed by CDG Books Canada Inc. for Canada; by Transworld Publishers Limited in the United Kingdom; by IDG Norge Books for Norway; by IDG Sweden Books for Sweden; by IDG Books Australia Publishing Corporation Pty. Ltd. for Australia and New Zealand; by TransQuest Publishers Pte Ltd. for Singapore, Malaysia, Thailand, Indonesia, and Hong Kong; by Gotop Information Inc. for Taiwan; by ICG Muse, Inc. for Japan; by Intersoft for South Africa; by Eyrolles for France; by International Thomson Publishing for Germany, Austria, and Switzerland; by Distribuidora Cuspide for Argentina; by LR International for Brazil; by Galileo Libros for Chile; by Ediciones ZETA S.C.R. Ltda. for Peru; by WS Computer Publishing Corporation, Inc., for the Philippines; by Contemporanea de Ediciones for Venezuela; by Express Computer Distributors for the Caribbean and West Indies; by Micronesia Media Distributor, Inc. for Micronesia; by Chips Computadoras S.A. de C.V. for Mexico; by Editorial Norma de Panama S.A. for Panama; by American Bookshops for Finland. For general information on Hungry Minds’ products and services please contact our Customer Care department within the U.S. at 800-762-2974, outside the U.S. at 317-572-3993 or fax 317-572-4002. For sales inquiries and reseller information, including discounts, premium and bulk quantity sales, and foreign-language translations, please contact our Customer Care department at 800-434-3422, fax 317-572-4002 or write to Hungry Minds, Inc., Attn: Customer Care Department, 10475 Crosspoint Boulevard, Indianapolis, IN 46256. For information on licensing foreign or domestic rights, please contact our Sub-Rights Customer Care department at 212-884-5000. For information on using Hungry Minds’ products and services in the classroom or for ordering examination copies, please contact our Educational Sales department at 800-434-2086 or fax 317- 572-4005. For press review copies, author interviews, or other publicity information, please contact our Public Relations department at 317-572-3168 or fax 317-572-4168. For authorization to photocopy items for corporate, personal, or educational use, please contact Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, or fax 978-750-4470. LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND AUTHOR HAVE USED THEIR BEST EFFORTS IN PREPARING THIS BOOK. THE PUBLISHER AND AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS BOOK AND SPECIFICALLY DISCLAIM ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. THERE ARE NO WARRANTIES WHICH EXTEND BEYOND THE DESCRIPTIONS CONTAINED IN THIS PARAGRAPH. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES REPRESENTATIVES OR WRITTEN SALES MATERIALS. THE ACCURACY AND COMPLETENESS OF THE INFORMATION PROVIDED HEREIN AND THE OPINIONS STATED HEREIN ARE NOT GUARANTEED OR WARRANTED TO PRODUCE ANY PARTICULAR RESULTS, AND THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY INDIVIDUAL. NEITHER THE PUBLISHER NOR AUTHOR SHALL BE LIABLE FOR ANY LOSS OF PROFIT OR ANY OTHER COMMERCIAL DAMAGES, INCLUDING BUT NOT LIMITED TO SPECIAL, INCIDENTAL, CONSEQUENTIAL, OR OTHER DAMAGES. Trademarks: Hungry Minds, the Hungry Minds logo, M&T Books, the M&T Books logo, and Professional Mindware are trademarks or registered trademarks of Hungry Minds, Inc., in the United States and other countries and may not be used without written permission. Java and JDBC are trademarks or registered trademarks of Sun Microsystems, Inc. All other trademarks are the property of their respective owners. Hungry Minds, Inc., is not associated with any product or vendor mentioned in this book. Credits Acquisitions Editor Grace Buechlein Project Editor Michael Koch Technical Editor Terry Smith Copy Editor S. B. Kleinman Editorial Manager Mary Beth Wakefield Senior Vice President, Technical Publishing Richard Swadley Vice President and Publisher Joseph B. Wikert Project Coordinator Nancee Reeves Graphics and Production Specialists Sean Decker Melanie DesJardins Laurie Petrone Jill Piscitelli Betty Schulte Jeremey Unger Quality Control Technicians Laura Albert David Faust John Greenough Andy Hollandbeck Angel Perez Proofreading and Indexing TECHBOOKS Production Services Cover Image © Noma/Images.com About the Author Todd M. Thomas is an Oracle Certified DBA and Java developer with more than six years’ experience in the IT industry. He currently works as an independent consultant, software developer, and trainer focusing on Java database and network programming. His work has ranged from building and managing data warehouses to architecting enterprise Java server applications. His most recent assignment was helping develop the network and database interface layers for BlueMoon, Airtuit Inc.’s wireless application gateway. His education includes a B.S. in Industrial Engineering and an M.S. in Engineering Science from the University of Tennessee in Knoxville. About the Contributors Johennie Helton has been an architect, developer, and software consultant on numerous n-tier– distributed systems and products. She has worked with databases and database design and implementation since 1990. Her database design projects include an application to make available automobile inventory online, a material management application for the health-care industry, and an application for customer coupon redemption for the grocery and coupon industries. During her career, her focus has been on creating applications with leading-edge technology, including application modeling, database design and implementation, and using J2EE and Java technologies to provide enterprise solutions to customers. She has a strong background in object-oriented analysis and design as well as in hypermedia systems. She has an M.S. in Computer Science from the University of Colorado. Steve Nobert got his first taste of programming in 1983, when he took a mathematics class in his senior year in high school. The class entailed developing computer programs on an Apple IIe to solve math problems. He hasn’t looked back since. As much as Steve loathed school, he still went on to receive his Associate of Science degree from Northern Virginia Community College in 1988, his Bachelor of Science degree from James Madison University in 1990, and his Master of Science degree from George Mason University in 1996, all of them in Computer Science. He has more than twelve years of professional programming experience involving myriad heterogeneous computing languages, operating systems, and state-of-the-art technologies. Steve’s primary career focus has been developing GUI applications on various flavors of UNIX. In 1996, Steve recommended that an unmaintainable and internationalized cross-platform application be rewritten using Java technology. He taught himself Java to develop the application/applet, and Java technology instantly became his primary career focus and has been so ever since. He has been involved in database technology since 1990. Steve has lived most of his life overseas and then in the northern Virginia area for almost fifteen years. Steve and his wife, Misti, reside in Knoxville, Tennessee. Table of Contents Part I: Getting Started with Java Data Access................................................................................................1 Chapter List..............................................................................................................................................1 Chapter 1: Introducing Java Data Access Development.................................................................................2 In This Chapter........................................................................................................................................2 Taking Stock of Enterprise Data Stores...................................................................................................2 Database systems........................................................................................................................4 Naming and directory services...................................................................................................5 XML services..............................................................................................................................6 Introducing Java Data Access Technologies...........................................................................................6 JDBC 3.0.....................................................................................................................................7 JNDI............................................................................................................................................8 JDO.............................................................................................................................................9 Java XML APIs........................................................................................................................10 Summary................................................................................................................................................10 Chapter 2: A Relational Database Primer......................................................................................................12 In This Chapter......................................................................................................................................12 The RDBMS in a Nutshell.....................................................................................................................12 Understanding data storage.......................................................................................................14 Ensuring data integrity..............................................................................................................15 Reviewing SQL Basics..........................................................................................................................19 Using Data Manipulation Language (DML)............................................................................20 Using Data Definition Language (DDL)..................................................................................25 Summary................................................................................................................................................28 Part II: Understanding JDBC Programming Basics.....................................................................................29 Chapter List............................................................................................................................................29 Chapter 3: Setting Up Your First JDBC Query............................................................................................30 In This Chapter......................................................................................................................................30 Configuring JDBC.................................................................................................................................30 Obtaining JDBC drivers............................................................................................................31 Installing the JDBC driver........................................................................................................31 Examining the Common JDBC Components........................................................................................32 Writing Your First JDBC Application...................................................................................................33 Creating the sample application................................................................................................33 Compiling and running the application....................................................................................41 Troubleshooting the sample application...................................................................................42 Summary................................................................................................................................................43 Chapter 4: Connecting to Databases with JDBC...........................................................................................44 In This Chapter......................................................................................................................................44 Understanding JDBC Drivers................................................................................................................44 What are JDBC drivers?...........................................................................................................46 Using your JDBC driver...........................................................................................................49 Working with Connection Objects.........................................................................................................53 Understanding JDBC URLs......................................................................................................53 i Table of Contents Chapter 4: Connecting to Databases with JDBC Opening connections.................................................................................................................54 Closing JDBC connections.......................................................................................................57 Summary................................................................................................................................................58 Chapter 5: Building JDBC Statements...........................................................................................................59 In This Chapter......................................................................................................................................59 Using JDBC Statements.........................................................................................................................59 Introducing Statement Objects...............................................................................................................60 Creating the Statement object...................................................................................................61 Using the Statement object.......................................................................................................61 JDBC batch processing.............................................................................................................70 JDBC transactions.....................................................................................................................72 Closing the Statement object....................................................................................................76 Working with PreparedStatement Objects.............................................................................................76 Creating the PreparedStatement object.....................................................................................77 Using the PreparedStatement object.........................................................................................77 Working with CallableStatement Objects..............................................................................................83 Creating the CallableStatement object......................................................................................83 Using the CallableStatement object..........................................................................................84 Summary................................................................................................................................................87 Chapter 6: Working with Result Sets.............................................................................................................88 In This Chapter......................................................................................................................................88 What Are JDBC Result Sets?................................................................................................................88 Introducing Result Set Concepts............................................................................................................89 Result set cursors......................................................................................................................89 Result set types.........................................................................................................................90 Retrieving result set data...........................................................................................................91 Using Standard Result Sets....................................................................................................................93 Creating a standard result set....................................................................................................93 Moving data around in a standard result set.............................................................................94 Using Scrollable Result Sets..................................................................................................................96 Creating scrollable result sets...................................................................................................97 Moving around scrollable result sets........................................................................................98 Using Updateable Result Sets..............................................................................................................103 Creating updateable result sets...............................................................................................104 Updating data with updateable result set................................................................................105 Inserting and deleting data with updateable result sets...........................................................108 Summary..............................................................................................................................................109 Chapter 7: Understanding JDBC Data Types..............................................................................................110 In This Chapter....................................................................................................................................110 Java, Databases, and Data Types.........................................................................................................110 Java−to−JDBC Data−Type Mappings.................................................................................................111 JDBC−to−Java Data−Type Mappings.................................................................................................114 Standard SQL data types.........................................................................................................114 Advanced SQL data types.......................................................................................................118 ii Table of Contents Chapter 7: Understanding JDBC Data Types Custom Data Type Mapping................................................................................................................128 Building custom data type maps.............................................................................................128 Using custom mapping...........................................................................................................131 Summary..............................................................................................................................................133 Chapter 8: Mining Database Metadata with JDBC....................................................................................134 In This Chapter....................................................................................................................................134 The JDBC Metadata Interfaces............................................................................................................134 The ResultSetMetaData Interface........................................................................................................135 Creating ResultSetMetaData objects......................................................................................135 Using ResultSetMetaData objects..........................................................................................135 ResultSetMetaData example...................................................................................................136 The DatabaseMetaData Interface.........................................................................................................140 Creating DatabaseMetaData objects.......................................................................................141 Using DatabaseMetaData objects...........................................................................................141 DatabaseMetaData example...................................................................................................144 Summary..............................................................................................................................................149 Part III: Using Java Data Access Design Patterns.......................................................................................150 Chapter List..........................................................................................................................................150 Chapter 9: Understanding Design Patterns.................................................................................................151 In This Chapter....................................................................................................................................151 What Are Design Patterns?..................................................................................................................151 Categories of Design Patterns..............................................................................................................152 Creational patterns..................................................................................................................152 Structural patterns...................................................................................................................154 Behavioral patterns.................................................................................................................154 Java and Design Patterns.....................................................................................................................156 Inheritance..............................................................................................................................156 Composition............................................................................................................................159 Design−pattern implementation guidelines............................................................................161 Summary..............................................................................................................................................162 Chapter 10: Building the Singleton Pattern.................................................................................................163 In This Chapter....................................................................................................................................163 What Is a Singleton Pattern?................................................................................................................163 Structure of the Singleton Pattern........................................................................................................164 Using the Singleton Pattern.................................................................................................................165 Basic Singleton example.........................................................................................................165 Connection manager Singleton example................................................................................168 Summary..............................................................................................................................................172 Chapter 11: Producing Objects with the Factory Method Pattern............................................................174 In This Chapter....................................................................................................................................174 What Is the Factory Method Pattern?..................................................................................................174 Introducing the Factory Method Structure...........................................................................................175 iii Table of Contents Chapter 11: Producing Objects with the Factory Method Pattern Using the Factory Method...................................................................................................................176 Summary..............................................................................................................................................187 Chapter 12: Creating a Façade Pattern........................................................................................................188 In This Chapter....................................................................................................................................188 What Is the Façade Pattern?.................................................................................................................188 Introducing the Structure of the Façade Pattern..................................................................................189 Implementing the Façade Pattern.........................................................................................................189 Summary..............................................................................................................................................202 Part IV: Taking It to the Enterprise.............................................................................................................203 Chapter List..........................................................................................................................................203 Chapter 13: Accessing Enterprise Data with JNDI.....................................................................................204 In This Chapter....................................................................................................................................204 Naming and Directory Services...........................................................................................................204 Naming services......................................................................................................................205 Directory services...................................................................................................................206 Data Access with JNDI........................................................................................................................208 JNDI architecture....................................................................................................................209 JNDI programming.................................................................................................................211 Working with the JNDI LDAP SPI........................................................................................214 Summary..............................................................................................................................................223 Chapter 14: Using Data Sources and Connection Pooling..........................................................................224 In This Chapter....................................................................................................................................224 Working with Java DataSource Objects..............................................................................................224 Using DataSource objects.......................................................................................................225 Looking at DataSource implementations................................................................................226 A DataSource example...........................................................................................................227 Using DataSource objects with JNDI.....................................................................................228 Implementing Connection Pooling......................................................................................................231 Understanding connection−pooling concepts.........................................................................232 A connection−pooling example..............................................................................................232 Summary..............................................................................................................................................234 Chapter 15: Understanding Distributed Transactions................................................................................235 In This Chapter....................................................................................................................................235 Understanding the Basics.....................................................................................................................235 Transaction definition and properties.....................................................................................235 Two−phase commit................................................................................................................236 Transaction−processing performance and availability...........................................................236 Replication..............................................................................................................................237 Understanding Distributed Transactions..............................................................................................238 Understanding the Transaction Monitor.................................................................................238 Understanding the Transaction Service..................................................................................239 Distributed Transactions and Java.......................................................................................................242 iv Table of Contents Chapter 15: Understanding Distributed Transactions EIS and EAI............................................................................................................................243 JMS.........................................................................................................................................244 JTS and JTA............................................................................................................................244 EJBs........................................................................................................................................245 Summary..............................................................................................................................................247 Chapter 16: Working with JDBC Rowsets...................................................................................................248 In This Chapter....................................................................................................................................248 Introducing JDBC Rowsets.................................................................................................................248 Understanding Rowset Concepts.........................................................................................................249 Rowset implementations.........................................................................................................249 Examining the rowset architecture.........................................................................................250 Working with RowSet Objects............................................................................................................251 Setting rowset properties........................................................................................................253 Configuring rowset events......................................................................................................253 Setting rowset connection properties......................................................................................254 Executing SQL commands using rowsets..............................................................................255 Fetching data from a rowset....................................................................................................256 Traversing data in a rowset.....................................................................................................256 Controlling scrollable and updateable properties...................................................................257 Setting transaction levels........................................................................................................257 Cleaning up after a RowSet....................................................................................................258 Using the JdbcRowSet Class...............................................................................................................258 Using the CachedRowSet Class...........................................................................................................260 Serializing a CachedRowSet object........................................................................................261 Updating and inserting disconnected rowset data...................................................................264 Using the WebRowSet Class...............................................................................................................265 Summary..............................................................................................................................................269 Chapter 17: Building Data−centric Web Applications................................................................................271 In This Chapter....................................................................................................................................271 Reviewing Enterprise Web Applications.............................................................................................271 Two−tier Web architecture.....................................................................................................272 MVC design pattern................................................................................................................273 Three−tier Web architecture...................................................................................................273 n−tier Web architecture..........................................................................................................274 J2EE enterprise application framework..................................................................................275 Using JDBC with Servlets...................................................................................................................276 Servlet overview.....................................................................................................................276 Constructing a JDBC servlet...................................................................................................277 Server deployment..................................................................................................................283 Design considerations.............................................................................................................284 Using JDBC with JavaServer Pages....................................................................................................286 JSP overview...........................................................................................................................286 Constructing a JSP page.........................................................................................................287 Using JDBC in JSP pages.......................................................................................................290 Using JSP with JDBC JavaBeans...........................................................................................299 v

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.