Oreilly-Database-Programming-With
Total Page:16
File Type:pdf, Size:1020Kb
Preface ............................................................................... 2 Audience ......................................................................... 3 Using This Book.............................................................. 3 Software and Versions.................................................... 4 Conventions Used in This Book...................................... 4 Comments and Questions .............................................. 4 About the Philosophers................................................... 5 Acknowledgments........................................................... 7 Feedback for the Author ................................................. 7 Part I: The JDBC API......................................................... 7 Chapter 1. Java in the Enterprise.................................... 7 The Enterprise ................................................................ 8 Java as a Tool for Enterprise Development.................... 10 The Database ................................................................. 12 Database Programming with Java.................................. 14 Chapter 2. Relational Databases and SQL ..................... 16 What Is a Relational Database? ..................................... 16 Databases and Database Engines................................... 17 An Introduction to SQL ................................................... 19 A Note on SQL Versions................................................. 22 Chapter 3. Introduction to JDBC ..................................... 22 What Is JDBC? ............................................................... 23 Connecting to the Database ........................................... 29 Connection Troubles........................................................ 30 Basic Database Access .................................................. 33 SQL Datatypes and Java Datatypes............................... 37 Scrollable Result Sets..................................................... 38 The JDBC Support Classes ............................................ 42 A Database Servlet ......................................................... 43 Chapter 4. Advanced JDBC ............................................. 47 Prepared SQL................................................................. 47 What Kind of Statement to Use? ..................................... 50 Batch Processing ............................................................ 51 Updatable Result Sets .................................................... 54 Advanced Datatypes....................................................... 58 Meta-Data ....................................................................... 63 Chapter 5. The JDBC Optional Package ......................... 74 Data Sources .................................................................. 74 Connection Pooling......................................................... 76 Rowsets .......................................................................... 77 Distributed Transactions ................................................. 79 Part II: Applied JDBC........................................................ 81 Chapter 6. Other Enterprise APIs.................................... 81 Java Naming and Directory Interface ............................. 81 Remote Method Invocation............................................. 83 Object Serialization ......................................................... 89 Enterprise JavaBeans..................................................... 90 Chapter 7. Distributed Application Architecture............ 97 Architecture..................................................................... 97 Design Patterns .............................................................. 106 The Banking Application ................................................. 110 Chapter 8. Distributed Component Models .................... 111 Kinds of Distributed Components ................................... 112 Security ........................................................................... 119 Transactions ................................................................... 125 Lookups and Searches ................................................... 130 Entity Relationships ........................................................ 131 Chapter 9. Persistence ..................................................... 139 Database Transactions ................................................... 139 Mementos and Delegates............................................... 147 JDBC Persistence........................................................... 148 Searches......................................................................... 150 Chapter 10. The User Interface ........................................ 156 Swing at a Glance........................................................... 157 Models for Database Applications .................................. 159 Distributed Listeners ....................................................... 166 Worker Threads .............................................................. 167 Part III: Reference ............................................................. 169 Chapter 11. JDBC Reference ........................................... 169 Reference ....................................................................... 170 Chapter 12. The JDBC Optional Package Reference..... 227 Reference ....................................................................... 228 Colophon ........................................................................... 249 JDBC and Java 2nd edition Database Programming with JDBC and Java, Second Edition Copyright © 2000 O'Reilly & Associates, Inc. All rights reserved. Printed in the United States of America. Published by O'Reilly & Associates, Inc., 101 Morris Street, Sebastopol, CA 95472. Nutshell Handbook, the Nutshell Handbook logo, and the O'Reilly logo are registered trademarks, and The Java™ Series is a trademark of O'Reilly & Associates, Inc. Java and all Java-based trademarks and logos are trademarks or registered trademarks of Sun Microsystems, Inc., in the United States and other countries. O'Reilly & Associates, Inc. is independent of Sun Microsystems. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O'Reilly & Associates, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. The O'Reilly logo is a registered trademark of O'Reilly & Associates, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O'Reilly & Associates, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. The use of the image of jacks in association with the topic of Java and JDBC is a trademark of O'Reilly & Associates, Inc. While every precaution has been taken in the preparation of this book, the publisher assumes no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. Preface It is never too late to become reasonable and wise; but if the insight comes late, there is always more difficulty in starting the change. — Immanuel Kant, Prolegomena to Any Future Metaphysics I began writing the first edition of this book in May 1996 as Java™ celebrated one of its first major rites of passage, the inaugural JavaOne conference. The conference's underlying theme was Java's transition from an applet language to a hard-core computing environment. In the time since that conference, that promise has become a reality. This book captures a small piece of that reality: Java as a language for enterprise computing. Enterprise computing, a vague term used mostly to sell business systems development products, traditionally refers to the mission-critical systems on which a business depends. It almost always includes a database. At the heart of Java's enterprise computing philosophy is the Java 2 Enterprise Edition (J2EE) platform and its two platforms by APIs: Enterprise JavaBeans (EJB) and Java Database Connectivity (JDBC). Older languages require third-party APIs to provide this kind of support. Java, on the other hand, includes these features in the central Java enterprise distribution that you will find on every Java platform. As a developer, you can write distributed applications that run against relational databases and know that those applications will run on any system on which you deploy them. What exactly are these APIs? JDBC—the basic component of this book—enables you to write applications that access relational databases without any thought as to which particular database you page 2 JDBC and Java 2nd edition are using. If you have ever had experience programming to more than one database API, you will definitely appreciate this aspect of Java. When you write a Java database program, that same program will run against Oracle, MySQL, Sybase, Ingres, Informix, mSQL, PostgreSQL, or any other database that supports this API. EJB, on the other hand, gives real meaning to the expression "the network is the computer." If you have written Internet applications in the past, you have probably been faced with the challenge of writing TCP/IP or UDP/IP sockets. While socket programming in Java is not nearly as hard as it is in other programming languages, the task of writing sockets