Java Database Programming with JDBC:Introduction
Total Page:16
File Type:pdf, Size:1020Kb
Java Database Programming with JDBC by Pratik Patel Coriolis, The Coriolis Group ISBN: 1576100561 Pub Date: 10/01/96 Table of Contents Introduction Welcome to the future of database connectivity. The Java Database Connectivity ( JDBC ) specification is a new basis for developers to interface with data sources. The structure of the JDBC is designed to allow developers to program for a standard interface, and let the low-level JDBC driver for different databases deal with the connection and querying of the databases; hence, the developer does not need to worry about dealing with different SQL flavors for each database. The JDBC is also very flexible—a developer doesn’t necessarily lose features specific to the target database. Best of all, the JDBC is based on the Java language! Getting Software The software that you’ll need to get started with the JDBC is readily available on the Web. The JDBC drivers that you will need to connect to your databases are currently available for most popular databases from a variety of software vendors. The basic package you’ll need is the JDBC API, which consists of the core classes of the JDBC. If you don’t already have a Java development kit, such as Javasoft’s JDK, you’ll need that as well. At the time this manuscript was finished, the examples in the book were tested with the JavaSoft JDK, Symantec Cafe, Microsoft J++, and Borland’s C+ + 5.0 with Java support. You can get the JavaSoft JDK at http://www.javasoft.com. The JDBC API, and the ODBC driver for JDBC (that’s right, you can use the JDBC with your current ODBC drivers!) commonly referred to as the JDBC-ODBC bridge can be downloaded at the JDBC Web site at http://splash.javasoft.com/ jdbc. You’ll also find the documentation for the JDBC API at this Web site. If you want to see some of the original JDBC specification, this can be downloaded from the JDBC Web site as well. Overview of Chapters Chapter 1, JDBC: Databases, The Java Way!, begins with a high-level introduction to the JDBC. You’ll see how modular JDBC drivers fit into the development cycle, as well as where ODBC fits into the JDBC’s overall structure. Chapter 2, SQL 101: An Introduction To SQL, takes a quick stroll through SQL, the language of databases. This chapter is a primer on SQL, and is useful if you need to brush up on your data-speak. It provides a basis of reference for some of the SQL queries performed in the JDBC programs in the book. Chapter 3, Using JDBC Drivers, shows you how to install JDBC drivers, as well as how to handle the installation of the JDBC API base classes. A “quick start” section also prepares you for what’s ahead by giving you a simple, but complete JDBC program. Chapter 4, The Interactive SQL Query Applet, takes you head first into the JDBC by presenting a complete Java applet that uses the JDBC. The applet allows a user to enter SQL queries and run them against a database, and to show the results. Chapter 5, Accessing ODBC Services Using JDBC, takes a look at the JDBC-ODBC bridge in detail. Limitations of the bridge, as well as a complete listing of the features of ODBC available in the JDBC, are presented. Chapter 6, SQL Datatypes In Java And ORM, shows you how to map SQL datatypes into Java, and provides a discussion of some of the special classes available in the JDBC API that facilitate the exchange of data between your Java program and the database. Chapter 7, Working With Query Results, provides a pathway for using results fetched from a SQL query. The complete cycle of querying a database, formatting the results, and displaying or printing them in nice graphs is presented with complete source code. A bar graph and pie chart are dynamically created in an applet using data from a query. Chapter 8, The Multimedia JDBC Application: Icon Store, continues the discussion in Chapter 7 by expanding into the realm of multimedia. Streams that contain binary data, such as images, are the focus of this chapter. We’ll show you how to store and retrieve binary data from a database, using the methods available in the JDBC. Chapter 9, Java and Database Security, reflects on the security consideration you need to ponder before you put your JDBC programs into production. The issue of “applet trusting,” and more, is covered in this chapter. Chapter 10, Writing Database Drivers, takes you into the heart of the JDBC with a thorough discussion of the programming details of JDBC drivers. You’ll even see an actual JDBC driver produced, as our SimpleText JDBC driver is hammered out during the chapter. The full source code for this driver is presented in Appendix B, while the intricacies of writing a JDBC driver are explained in detail in this chapter. Chapter 11, Internet Database Issues: Middleware, details three-tier database systems. A three-tier system is developed in this chapter to give you an idea of the functionality possible with these types of “indirect” database access. The full source code for the developed application server and the client are presented, as well as a sample applet that uses the client to query and obtain results from a database. Chapter 12, The JDBC API, provides you with a reference for all of the methods, variables, classes, exceptions, and interfaces that are the JDBC. Table of Contents Java Database Programming with JDBC by Pratik Patel Coriolis, The Coriolis Group ISBN: 1576100561 Pub Date: 10/01/96 Introduction Chapter 1—JDBC: Databases The Java Way! What Is The JDBC? The JDBC Structure ODBC’s Part In The JDBC Summary Chapter 2—SQL 101 The Relational Model And SQL Understanding The Basics Putting It Into Perspective: Schema And Catalog Introducing Keys Using Multiple Tables And Foreign Keys Data Definition Language Declaring Domains Performing Checks Creating Tables Manipulating Tables Data Maintenance Language Data Query Language Coming Up Next Chapter 3—Using JDBC Drivers Quick Start Guide Installing java.sql.* Registering And Calling JDBC Drivers The sql.drivers Property There’s Always A Class For A Name Just Do It JDBC URL And The Connection Using ODBC Drivers Installing The JDBC-ODBC Bridge Setting Up ODBC Drivers Summary Chapter 4—The Interactive—SQL Applet Your First JDBC Applet The Blueprint Getting A Handle On The JDBC Essentials: The Complete Applet Source Code The Look Of The Applet Handling Events Opening The Connection No Guts, No Glory: Executing Queries And Processing Results Wrapping It Up The HTML File That Calls The Applet The Final Product Coming Up Next Chapter 5—Accessing ODBC Services Using JDBC Bridge Requirements The Bridge Is Great, But... The ODBC URL JDBC To ODBC Calls: A Roadmap Chapter 6—SQL Data Types In Java And ORM Mapping SQL Data To Java ResultSetMetaData Understanding The Object Relation Model Mapping A Table Into A Java Object Summary Chapter 7—Working With Query Results A Basic Java Object For Storing Results Showing The Results Charting Your Data Summary Chapter 8—The IconStore Multimedia JDBC Application IconStore Requirements Building The Database Application Essentials Writing The main Method Establishing The Database Connection Creating The Menu Creating The Lists Handling Events Saving The Image Summary Chapter 9—Java And Database Security Database Server Security Rooting Out The Packet Sniffers Web Server CGI Holes Finding A Solution Applet Security: Can I Trust You? The Applet Security Manager I’m A Certified Applet Summary Chapter 10—Writing Database Drivers The JDBC Driver Project: SimpleText SimpleText SQL Grammar SimpleText File Format The DriverManager JDBC Exception Types JDBC Data Types Character Data: CHAR, VARCHAR, And LONGVARCHAR Exact Numeric Data: NUMERIC And DECIMAL Binary Data: BINARY, VARBINARY, And LONGVARBINARY Boolean Data: BIT Integer Data: TINYINT, SMALLINT, INTEGER, And BIGINT Floating-Point Data: REAL, FLOAT, And DOUBLE Time Data: DATE, TIME, And TIMESTAMP New Data Classes Numeric Date Time Timestamp Native Drivers: You’re Not From Around Here, Are Ya? Implementing Interfaces Tracing Turning On Tracing Writing Tracing Information Checking For Tracing Data Coercion Escape Clauses Date, Time, And Timestamp Scalar Functions LIKE Predicate Escape Characters Outer Joins Procedures The JDBC Interfaces Driver Connection DatabaseMetaData Statement PreparedStatement ResultSet ResultSetMetaData Summary Chapter 11—Internet Database Issues: Middleware Connectivity Issues Involved With Database Access Advantages Of Middleware Disadvantages Of Middleware The Application Server: A Complete Example With Code The Client: A Complete Example With Code Summary Chapter 12—The JDBC API Classes public class Date public class DriverManager public class DriverPropertyInfo public final class Numeric public class Time public class TimeStamp public class Types Interfaces public interface CallableStatement public interface Connection public interface DatabaseMetaData public interface Driver public interface PreparedStatement public interface ResultSet public interface ResultSetMetaData public interface Statement Exceptions public class DataTruncation public class SQLException public class SQLWarning Appendix A Appendix B Appendix C Appendix D Index Java Database Programming with JDBC by Pratik Patel Coriolis, The Coriolis Group ISBN: 1576100561 Pub Date: 10/01/96 Previous Table of Contents Next Chapter 1 JDBC: Databases The Java Way! The Internet has spurred the invention of several new technologies in client/server computing—the most recent of which is Java. Java is two-dimensional: It’s a programming language and also a client/server system in which programs are automatically downloaded and run on the local machine (instead of the server machine). The wide embrace of Java has prompted its quick development. Java includes Java compilers, interpreters, tools, libraries, and integrated development environments (IDEs). Javasoft is leading the way in the development of libraries to extend the functionality and usability of Java as a serious platform for creating applications.