5203ch00_FM.qxd 8/12/05 12:17 PM Page i
JDBC Recipes
A Problem-Solution Approach
Mahmoud Parsian
5203ch00_FM.qxd 8/12/05 12:17 PM Page ii
JDBC Recipes: A Problem-Solution Approach
Copyright © 2005 by Mahmoud Parsian
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN: 1-59059-520-3
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
Lead Editor: Steve Anglin
Development Editor: Jim Sumser
Technical Reviewer: Sumit Pal
Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Tony Davis, Jason Gilmore,
Jonathan Hassell, Chris Mills, Dominic Shakeshaft, Jim Sumser
Associate Publisher: Grace Wong
Project Manager: Beckie Stones
Copy Edit Manager: Nicole LeClerc
Copy Editor: Kim Wimpsett
Assistant Production Director: Kari Brooks-Copony
Production Editor: Katie Stence
Compositor and Artist: Kinetic Publishing Services, LLC
Proofreaders: Liz Welch and Lori Bring
Indexer: Tim Tate
Interior Designer: Van Winkle Design Group
Cover Designer: Kurt Krames
Manufacturing Director: Tom Debolski
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or
visit http://www.springeronline.com.
For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley,
CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visit http://www.apress.com.
The information in this book is distributed on an “as is” basis, without warranty. Although every precaution
has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to
any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly
by the information contained in this work.
The source code for this book is available to readers at http://www.apress.com in the Source Code section.
5203ch00_FM.qxd 8/12/05 12:17 PM Page iii
This book is dedicated to
my love, Behnaz;
my gozal daughter, Maral;
my gibldiz son, Yaseen;
my mother, Monireh, and the memory of my father, Bagher.
5203ch00_FM.qxd 8/12/05 12:17 PM Page iv
5203ch00_FM.qxd 8/12/05 12:17 PM Page v
Contents at a Glance
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
About the Technical Reviewer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
■ CHAPTER 1
Introducing JDBC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
■ CHAPTER 2
Exploring JDBC’s Novel Features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
■ CHAPTER 3
Making Database Connections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
■ CHAPTER 4
Making Database Connections Using DataSource . . . . . . . . . . . . . . . . . . 153
■ CHAPTER 5
Exploring the ResultSet Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
■ CHAPTER 6
Working with Scrollable and Updatable ResultSet Objects . . . . . . . . . . 209
■ CHAPTER 7
Reading and Writing BLOBs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
■ CHAPTER 8
Reading and Writing CLOBs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
■ CHAPTER 9
Working with Date, Time, and Timestamp in JDBC. . . . . . . . . . . . . . . . . . 307
■ CHAPTER 10 Handling Exceptions in JDBC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
■ CHAPTER 11
Exploring the Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
■ CHAPTER 12 Working with the PreparedStatement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
■ CHAPTER 13
Passing Input Parameters to PreparedStatement . . . . . . . . . . . . . . . . . . 493
■ CHAPTER 14
Exploring JDBC Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569
INDEX
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615
v
■
5203ch00_FM.qxd 8/12/05 12:17 PM Page vi
5203ch00_FM.qxd 8/12/05 12:17 PM Page vii
Contents
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
About the Technical Reviewer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
■ CHAPTER 1
Introducing JDBC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1-1. What Is JDBC? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1-2. What Is JDBC’s High-Level Architecture?. . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1-3. What Is JDBC’s Detailed Architecture?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1-4. What Is a Relational Database? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1-5. What Is ODBC? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1-6. What Is a JDBC-ODBC Bridge?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1-7. What Is SQL? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1-8. What Is JDBC Programming? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1-9. What Is the JDBC API (in a Nutshell)?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
1-10. What Are the Core JDBC Classes/Interfaces? . . . . . . . . . . . . . . . . . . . . . . 26
1-11. What Is a JDBC Driver? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
1-12. How Do You Load a JDBC Driver? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
1-13. How Do You Test a JDBC Driver Installation? . . . . . . . . . . . . . . . . . . . . . . . 32
1-14. Where Can You Obtain a JDBC Driver for Your Database
System? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
1-15. What Is a JDBC Driver Type? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
1-16. What Types of JDBC Drivers Exist? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
1-17. What Are the Selection Criteria for JDBC Drivers?. . . . . . . . . . . . . . . . . . . 37
1-18. What Is the URL Syntax for Connecting to a Database? . . . . . . . . . . . . . . 38
1-19. What Is the Mapping Between Java to JDBC SQL Types? . . . . . . . . . . . . . 39
1-20. How Do You Handle JDBC Errors/Exceptions? . . . . . . . . . . . . . . . . . . . . . . 42
■ CHAPTER 2
Exploring JDBC’s Novel Features. . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
2-1. What Is Database Metadata? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
2-2. What Is a Transaction? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
2-3. How Do You Turn On Autocommit Mode?. . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2-4. How Do You Turn Off Autocommit Mode?. . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2-5. How Do You Roll Back a Transaction? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
vii