JDBC Developer's Resource: Database Programming on the Internet

JDBC Developer's Resource: Database Programming on the Internet

by Art Taylor

Paperback

$49.95

Overview

Write Java™ code that leverages all your corporate data resources--with JDBC and The JDBC Developer's Resource.


If you need to build Java applications that use existing corporate data, you deserve the best JDBC tutorial and reference available: The JDBC Developer's Resource.


For programmers familiar with Java, this book/CD-ROM package offers comprehensive coverage of Sun's Java Database Connectivity (JDBC) API--and provides complete tools for making the most of JDBC.


Learn all the fundamentals you need to know, including JDBC's interface, methods, and relationship to ODBC. Then, using extensive code examples, discover how to use JDBC in a wide range of applications, in both two-tiered and three-tiered environments.


An extensive tutorial walks you through every step of developing numerous sample applications, demonstrating virtually all the techniques you need to know to program JDBC applications. Examples range from the development of simple JDBC applications to connect to a database and retrieve data, to the creation of a three-tiered RMI program with an applet that makes remote calls to object methods on a server using Java's RMI interface.


As you become familiar with JDBC, you'll depend on The JDBC Developer's Resource even more. Its exclusive "Quick Reference" describes all JDBC methods, the classes they belong to, and the parameters passed to them. A more detailed reference presents every class, its methods and variables, with code samples.


The accompanying CD-ROM contains indispensable tools for JDBC and Java software development, including:



  • A trial version of the exciting new Mojo Java applet development environment.

  • The complete Java RMI and JDBC API.

  • Intersolv's DataDirect ODBC Pack for Microsoft Windows® 95.

  • Sun's complete Java Developer's Kit 1.02.

  • Complete source code for all the books applets and applications.


With The JDBC Developer's Resource, you've got everything you need to combine Java's unprecedented capabilities with your organization's massive data resources. Now that's power.


Product Details

ISBN-13: 9780138423520
Publisher: Prentice Hall Professional Technical Reference
Publication date: 04/11/1997
Series: Prentice Hall PTR Developer's Resource Series
Pages: 715
Product dimensions: 7.05(w) x 9.23(h) x 2.07(d)

Read an Excerpt

PREFACE: Introduction

The Java Language and JDBC

Since its inception in 1995, the Java language has generated intense interest in the computer industry. The Java Database Connectivity (JDBC) standard adds a whole new dimension to this capable and multi-faceted language. Rather than simply being used to "bring life to Web pages," Java with JDBC can now fill the role of database-aware applications for a variety of application needs. Where currently common gateway interface (CGI) applications are required to access databases from within Web browsers, Java applets with JDBC now have the potential to provide this functionality with more direct programming and the potential for improved performance.

Using this API, you can now use Java applications for standard database programming tasks such as reports or updates. With JAVA's flexible network API, you can use Java/JDBC applications for filter programs, to read data from a data stream, and to provide conversion facilities before updating a database.

Java applications written using JDBC are portable both for the hardware platform and the database. You can run a correctly written Java/JDBC program on a variety of platforms against a variety of databases without any code changes. This is possible because JDBC is based on ODBC, which is in turn based on the X/Open CLI specification. The goals of the X/Open group have been met to the extent that database access standards have been accepted by the industry. Access to all major databases is available through ODBC. And JDBC-ODBC bridges are available to provide database access by mapping JDBC calls to their corresponding ODBC calls.Thus, any database that is accessible with an ODBC driver is also accessible with a Java/JDBC driver using the JDBC-ODBC bridge.

JDBC uses the industry standard Structured Query Language (SQL) to communicate with the database. Writing a JDBC application, therefore, requires knowledge of both the Java language and SQL. To provide a refresher, this book includes sections on both of these languages. These chapters are intended to help those who have had moderate exposure to these languages. For those with no experience in SQL, additional training is recommended.


About This Book

This book is designed to provide you with a thorough grounding in the JDBC API. It is divided into two parts: a tutorial and a reference. The tutorial part provides a primer on the Java language and relational databases. This is followed by a set of examples that demonstrate the process of programming database applications using JDBC. This part begins with simple examples and then progresses into more complex applications.

The reference part provides several listings of the methods available in JDBC. A brief, quick reference of the methods is provided, followed by a more detailed description of the methods arranged by JDBC interfaces.


Target Audience

If you use Java and have a need to perform database access, then this book is for you. This book presumes the reader is an experienced programmer, but not necessarily a Java programmer. This book is designed to be a useful resource for both the experienced and inexperienced Java programmer. A programmer just learning the Java language will find the JDBC library is similar in design to the other Java libraries and just as easy to use. This book will provide the guidance and the code samples to easily complete a JDBC application.

Java has intentionally been designed with a syntax similar to C/C++, so a programmer familiar with either of these languages will be able to use Java. And a programmer familiar with object-oriented concepts will be comfortable with JDBC and it's object-oriented design.

If you are one of those individuals very familiar with C but with moderate exposure to C++ or Java, then this book provides a Java overview to present some of the more important Java concepts. Having read these sections, an experienced programmer will have no problem understanding the numerous code samples presented in this book.


Resource

This book is designed to be both a tutorial and a reference combined into a single book: a complete "developer's resource." As a reference book, a quick reference section contains all methods in the class library and a short description of the method. A second reference section lists all JDBC interface descriptions and provides a code sample for virtually all JDBC methods.

This book is designed to be a complete JDBC resource. A tutorial section is provided to demonstrate how to use JDBC with Java. With the belief that a good code sample is worth a 1000 words, numerous code samples provide a "picture" of how to access data using JDBC.

A complete reference section is also included. The entire JDBC class library is covered with code samples provided for most of the methods.

An experienced programmer with solid Java experience could read the sections of the tutorial that cover basic JDBC programming, use the reference section as needed, and quickly develop a working JDBC application. A less-experienced Java programmer could read the Java primer section and the tutorial section, and then use the reference section as a reference to develop a JDBC application.


Conventions Used in This Book

You'll notice as you read this book that there are several conventions used to help make especially useful material easier to read and to locate when you're using this book as a reference during your programming sessions.
Source Code
All code fragments and listings that have been set off from the main text of this book have been formatted in the following way:

... StringBuffer DTString = "The answer is "; int num = 42; DTString = DTString + num; System.out.println( DTString ); ...


As well, coding terms that appear within the main text have been set in a special font for easier reference, like so: "The Java language eliminates this difficulty by extending the capabilities of the continue and break statements."
Icons
Oftentimes, there is information that deserves special attention because it highlights a particularly useful or important point that might otherwise get lost among all of the other important information in the main text. For this reason, a number of special icons have been created to help this information stand out.


About the CD

Several useful tools and programming examples are provided on the CD-ROM enclosed with the book. The tools are as follows:

  • The Java JDK, an essential part of any JDBC development effort, is provided.
  • The JDBC-ODBC bridge from SunSoft.
  • INTERSOLV DataDirect ODBC Pack for Windows 95/Windows NT.
  • The Mojo Visual Java Development Environment for Windows 95.

    Also provided are the majority of the programming examples used in this book. Wherever a program is referenced with a program name, that program is available on the CD-ROM. A special Web page has been created as a front-end to navigating the CD-ROM included with this book. To access that Web page, please load the file named index.html, found in the root directory of the CD-ROM, into your browser. The directory locations of the programs and instructions on running the various applications are accessible from this Web page. Also included on this Web page are links to various JDBC-related sites, as well as the online supplement to this book, explained in the following section.

    The combination of the JDBC-ODBC bridge and the Intersolv DataDirect ODBC drivers allow JDBC applications to be developed under the Windows 95 environment. ODBC drivers are provided for a number of databases, including MicroSoft Access.

    The Mojo Visual Java Development Environment is a robust development environment that provides a window painter and class browser with the ability to drop pre-developed modules directly into the applet being developed. Directions and online help are provided on the CD-ROM.


    About the Web Site

    Finally, this and every book in the Developer's Resource Series is accompanied by a special online Web supplement created especially for each book. You can look for additional information related to the JDBC, as well as other books in the series, at:

    ...

Table of Contents

Introduction xvi
The Java Language and JDBC xvi
About This Book xvii
Target Audience xvii
Resource xviii
Conventions Used in This Book xviii
About the CD xx
About the Web Site xx
Changes to JDBC for JDK 1.1 xxi
Acknowledgments xxi
Part One Java Tutorial 2(206)
A Java Language Primer
5(18)
History of Java
6(2)
Featuress
6(1)
Design
7(1)
C++ and Java
8(1)
Emerging Java Technologies
8(4)
Java Beans
9(1)
Java Database Access Standard (JDBC)
9(1)
JDBC Driver Types
10(2)
The Java Base System
12(1)
Basic Elements of Java
12(5)
Numeric Data Types
13(2)
Array
15(1)
Strings
15(1)
Labels
15(1)
Memory Management in Java
16(1)
Exception Handing
16(1)
C and C++ Features Not Present in Java
17(6)
typedefs,defines, and Pre-processor support
18(1)
Structures and Unions
19(1)
Functions
19(1)
Multiple Inheritance
19(1)
Operator Overloading
19(1)
Automatic Coercion
19(1)
Pointers
20(3)
Relational Database Primer
23(24)
History of the Relational Database
24(7)
Relational Database Concepts
24(4)
Relational Database Terminology
28(3)
Structured Query Language
31(16)
Basic SQL Statements
32(2)
SQL Standards
34(1)
Call Level Interface
35(1)
Transactions, Database Logging and Isolation Levels, and Concurrency
35(7)
SQL Query Optimization
42(2)
Dynamic SQL Execution
44(3)
JDBC Application Programmer Interface (API)
47(32)
SQL Statement Execution with JDBC
48(16)
DriverManager Class
51(1)
The java.sql.Connection Class
52(3)
The java.sql.Statement Class
55(3)
The java.sql.ResultSet Class
58(3)
The java.sql.PreparedStatement Class
61(3)
JDBC Security
64(1)
JDBC Usage
65(14)
Applet
65(3)
Java for General-Purpose Applications
68(1)
CGI Application
68(1)
JDBC Application Design
68(3)
JDBC Data Type Mapping
71(3)
Muti-Threading in Java
74(1)
Transactions
75(1)
Cursors
76(1)
SQL Level
76(3)
JDBC Tutorial
79(70)
A Simple JDBC Application
81(1)
Use of the Prepared Statement
82(1)
Positioned Cursor Update
82(1)
Transaction Modes
82(1)
Java Applet
83(1)
CGI Application
84(1)
Metadata Usage
85(1)
ResultSet Array Example
85(2)
Basic JDBC Programming
85(2)
Basic JDBC Steps
87(10)
Load Driver
87(1)
Create Connection
87(2)
Create statement
89(1)
Execute SQL Statement and Return ResultSet
89(1)
Iterate ResultSet
89(2)
A Dynamic SQL Select Program
91(2)
Load Driver and Get Database Connection
93(1)
Retrieve Table Name from Command Line Argument
93(1)
Build Select Statement
94(1)
Create Statement Object and Execute SQL Statement
94(1)
Create a ResultSetMetaData Object
94(1)
Traverse the ResultSet
95(2)
Prepared Statement
97(8)
Load Driver and Create Connection
98(1)
Create Query String with Parameter and Create PreparedStatement Object
99(1)
Set Parameter Value and Execute Query
99(1)
Loop for 2000 Iterations
100(5)
Positioned Cursor Update
105(5)
Load Database Driver and Create Connection
105(1)
Create DatabaseMetaData Object and Test for Positioned Update Functionality
106(1)
Execute Select Query
106(1)
Get Cursor Name and Execute Update Statement
107(1)
Review Results
107(3)
Transaction Modes
110(5)
Load Driver and Create Connection
110(1)
Set the Auto-Commit Mode
111(1)
Create Statement and Execute DDL and DML
111(1)
Commit Work
111(1)
Create Prepared Statement and Execute Updates
112(1)
Rollback Work and Display Results
113(1)
catch code block
113(2)
CGI Application
115(26)
Load Driver Manager and Create Connection
116(1)
Create Prepared Statement with Parameter
116(1)
Parse CGI Arguments
117(1)
Set Parameters and Execute Query
118(1)
Retrieve Results and Display Formatted Output
119(3)
Metadata Access
122(1)
Retrieve Query from the Command Line
123(1)
Load Driver and Create Connection
124(1)
Create Statement and Execute the Query
124(1)
Retrieve the ResultSet and Determine the Number of Columns
124(1)
Execute Formatting Routine
125(4)
Interate Results Displaying Formatted Data
129(7)
Scrolling ResultSet Array
136(1)
Declare RSArray Object
137(1)
Load DriverManager and Connection
137(1)
Create Statement and Execute
137(1)
Iterate ResultSet Adding to ResultSetArray Buffer
138(1)
Display Results
138(3)
The RSArray Class
141(8)
Class Definition
142(7)
JDBC Applet Example
149(20)
Overview
150(2)
Insert Data
151(1)
Get Data
151(1)
Next Row
151(1)
Previous Row
151(1)
Applet Code
152(17)
Declarations
152(1)
Display Applet Window
153(1)
'Get Date' Button Click
154(1)
'Previous Row' Button Click Event Handler
154(1)
'Next Row' Button Click Event Handler
154(1)
'Insert Data' Button Click Event Handler
154(1)
Declarations
154(1)
Display Applet Window
155(3)
Button Class Declaration
158(1)
Previous Button Click Handler
159(1)
Inert Data Button Click Event
160(1)
NextRow Button Event
160(1)
Get Data Button Event
161(8)
Three-Tiered Programming with JDBC
169(16)
RMI Example: The Client
171(1)
The Middle Tier
171(1)
The Back End
172(1)
Three-Tiered and Multitiered Programming with Java RMI
172(1)
Using JDBC and RMI
173(1)
Three-Tiered RMI Example
174(11)
The RMI/ JDBC Example
175(1)
The RMI Client Code
176(2)
RMI Remote Method Programming (Server)
178(3)
Remote Method Implementation: Main Program Block
181(4)
JDBC 2.0 Revisions and Extensions
185(23)
JDBC 2.0 Enhancements and Changes
186(4)
Result Set Enhancements
188(1)
Batch Updates
188(1)
Advanced Data Types
188(1)
Rowsets
188(1)
JNDI Database Naming
189(1)
Connection Pooling
189(1)
Distributed Transaction Support
189(1)
Other Additions
190(1)
Result Set Enhancements
190(2)
ResultSet Scrolling
191(1)
Using a ResultSet
192(5)
Updating with ResultSets
194(1)
Inserting with ResultSets
194(1)
Visibility of Database Changes
195(1)
Determining Cursor Capabilities
196(1)
Batch Updates with JDBC
197(2)
Object Persistence: Storing Java Objects in the Database
199(2)
New SQL Data Types
201(7)
Distinct and Structured Data Types
202(1)
Retrieving and Storing Large Objects: Blobs and Clobs
203(1)
Retrieving Arrays
204(1)
Retrieving and Storing "refs"
205(1)
Retrieving and Storing Distinct Data Types
206(1)
Retrieving and Storing Structured Types
206(1)
Using Stream I/O to Manipulate Structured Data Types
207(1)
Conlusion
207(1)
Part Two JDBC Reference 208(407)
JDBC Quick Reference
211(46)
JDBC Methods Summary
211(46)
All JDBC Methods
212(38)
Class Variables
250(7)
JDBC Reference
257(358)
Interface BigDecimal
257(1)
Instance Variables
257(1)
BigDecimal Methods
258(12)
Interface BigInteger
270(1)
Instance Variables
270(1)
BigInteger Methods
270(17)
Interface java.sql.CallableStatement
287(2)
CallableStatement Methods
289(10)
Interface java.sql.Connection
299(19)
Instance Variables
300(3)
Connection Methods
303(15)
The java.sql.DatabaseMetaData Interface
318(114)
Instance Variables
318(8)
DatabaseMetaData Methods
326(106)
Class java.sql.DataTruncation
432(4)
DataTruncation Methods
432(4)
Interface java.sql.Date
436(2)
Interface Java.sql.Driver
438(4)
Driver Methods
438(1)
getPropertyInfo
439(3)
Interface java.sql.DriverManager
442(10)
DriverManager Methods
442(10)
Interface java.sql.DriverPropertyInfo
452(1)
Variables
452(1)
Constructor
452(1)
Class java.sql.NullData
453(1)
Variables
453(1)
Constructor
453(1)
Interface java.sql.PreparedStatement
454(34)
PreparedStatement Methods
455(33)
Interface java.sql.ResultSet
488(47)
ResultSet Methods
490(45)
Interface java.sql.ResultSetMetaData
535(28)
ResultSetMetaData Variables
536(1)
ResultSetMetaData Methods
537(26)
Class java.sql.SQLException
563(6)
SQLException Constructor
564(1)
SQLException Methods
565(4)
Class java.sql.SQLWarning
569(4)
SQLWarning Constructor
570(1)
SQLWarning Method
571(2)
Interface java.sql.Statement
573(30)
Statement Methods
574(29)
Interface java.sql.Time
603(2)
Time Constructors
603(1)
Methods
603(2)
Class java.sql.Timestamp
605(4)
Timestamp Constructors
605(1)
Timestamp Methods
605(4)
Class java.sql.Types
609(6)
APPENDIX A Function 615(6)
Math Functions
615(1)
String Functions
616(2)
Date Functions
618(1)
Database Functions
619(2)
APPENDIX B Applet Code Example (Chapter 5) 621(16)
APPENDIX C RMI Code Example (Chapter 6) 637(16)
Index 653

Preface

PREFACE: Introduction

The Java Language and JDBC

Since its inception in 1995, the Java language has generated intense interest in the computer industry. The Java Database Connectivity (JDBC) standard adds a whole new dimension to this capable and multi-faceted language. Rather than simply being used to "bring life to Web pages," Java with JDBC can now fill the role of database-aware applications for a variety of application needs. Where currently common gateway interface (CGI) applications are required to access databases from within Web browsers, Java applets with JDBC now have the potential to provide this functionality with more direct programming and the potential for improved performance.

Using this API, you can now use Java applications for standard database programming tasks such as reports or updates. With JAVA's flexible network API, you can use Java/JDBC applications for filter programs, to read data from a data stream, and to provide conversion facilities before updating a database.

Java applications written using JDBC are portable both for the hardware platform and the database. You can run a correctly written Java/JDBC program on a variety of platforms against a variety of databases without any code changes. This is possible because JDBC is based on ODBC, which is in turn based on the X/Open CLI specification. The goals of the X/Open group have been met to the extent that database access standards have been accepted by the industry. Access to all major databases is available through ODBC. And JDBC-ODBC bridges are available to provide database access by mapping JDBC calls to their corresponding ODBCcalls.Thus, any database that is accessible with an ODBC driver is also accessible with a Java/JDBC driver using the JDBC-ODBC bridge.

JDBC uses the industry standard Structured Query Language (SQL) to communicate with the database. Writing a JDBC application, therefore, requires knowledge of both the Java language and SQL. To provide a refresher, this book includes sections on both of these languages. These chapters are intended to help those who have had moderate exposure to these languages. For those with no experience in SQL, additional training is recommended.


About This Book

This book is designed to provide you with a thorough grounding in the JDBC API. It is divided into two parts: a tutorial and a reference. The tutorial part provides a primer on the Java language and relational databases. This is followed by a set of examples that demonstrate the process of programming database applications using JDBC. This part begins with simple examples and then progresses into more complex applications.

The reference part provides several listings of the methods available in JDBC. A brief, quick reference of the methods is provided, followed by a more detailed description of the methods arranged by JDBC interfaces.


Target Audience

If you use Java and have a need to perform database access, then this book is for you. This book presumes the reader is an experienced programmer, but not necessarily a Java programmer. This book is designed to be a useful resource for both the experienced and inexperienced Java programmer. A programmer just learning the Java language will find the JDBC library is similar in design to the other Java libraries and just as easy to use. This book will provide the guidance and the code samples to easily complete a JDBC application.

Java has intentionally been designed with a syntax similar to C/C++, so a programmer familiar with either of these languages will be able to use Java. And a programmer familiar with object-oriented concepts will be comfortable with JDBC and it's object-oriented design.

If you are one of those individuals very familiar with C but with moderate exposure to C++ or Java, then this book provides a Java overview to present some of the more important Java concepts. Having read these sections, an experienced programmer will have no problem understanding the numerous code samples presented in this book.


Resource

This book is designed to be both a tutorial and a reference combined into a single book: a complete "developer's resource." As a reference book, a quick reference section contains all methods in the class library and a short description of the method. A second reference section lists all JDBC interface descriptions and provides a code sample for virtually all JDBC methods.

This book is designed to be a complete JDBC resource. A tutorial section is provided to demonstrate how to use JDBC with Java. With the belief that a good code sample is worth a 1000 words, numerous code samples provide a "picture" of how to access data using JDBC.

A complete reference section is also included. The entire JDBC class library is covered with code samples provided for most of the methods.

An experienced programmer with solid Java experience could read the sections of the tutorial that cover basic JDBC programming, use the reference section as needed, and quickly develop a working JDBC application. A less-experienced Java programmer could read the Java primer section and the tutorial section, and then use the reference section as a reference to develop a JDBC application.


Conventions Used in This Book

You'll notice as you read this book that there are several conventions used to help make especially useful material easier to read and to locate when you're using this book as a reference during your programming sessions.
Source Code
All code fragments and listings that have been set off from the main text of this book have been formatted in the following way:

... StringBuffer DTString = "The answer is "; int num = 42; DTString = DTString + num; System.out.println( DTString ); ...


As well, coding terms that appear within the main text have been set in a special font for easier reference, like so: "The Java language eliminates this difficulty by extending the capabilities of the continue and break statements."
Icons
Oftentimes, there is information that deserves special attention because it highlights a particularly useful or important point that might otherwise get lost among all of the other important information in the main text. For this reason, a number of special icons have been created to help this information stand out.


About the CD

Several useful tools and programming examples are provided on the CD-ROM enclosed with the book. The tools are as follows:

  • The Java JDK, an essential part of any JDBC development effort, is provided.
  • The JDBC-ODBC bridge from SunSoft.
  • INTERSOLV DataDirect ODBC Pack for Windows 95/Windows NT.
  • The Mojo Visual Java Development Environment for Windows 95.

    Also provided are the majority of the programming examples used in this book. Wherever a program is referenced with a program name, that program is available on the CD-ROM. A special Web page has been created as a front-end to navigating the CD-ROM included with this book. To access that Web page, please load the file named index.html, found in the root directory of the CD-ROM, into your browser. The directory locations of the programs and instructions on running the various applications are accessible from this Web page. Also included on this Web page are links to various JDBC-related sites, as well as the online supplement to this book, explained in the following section.

    The combination of the JDBC-ODBC bridge and the Intersolv DataDirect ODBC drivers allow JDBC applications to be developed under the Windows 95 environment. ODBC drivers are provided for a number of databases, including MicroSoft Access.

    The Mojo Visual Java Development Environment is a robust development environment that provides a window painter and class browser with the ability to drop pre-developed modules directly into the applet being developed. Directions and online help are provided on the CD-ROM.


    About the Web Site

    Finally, this and every book in the Developer's Resource Series is accompanied by a special online Web supplement created especially for each book. You can look for additional information related to the JDBC, as well as other books in the series, at:

    ...

Customer Reviews

Most Helpful Customer Reviews

See All Customer Reviews