Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Search this book:
 
Search the site:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Part II
Exploring JDBC

Chapter 4: Getting Started

Chapter 5: Database Connectivity Step-by-Step

Chapter 6: Fine Tuning

Chapter 7: Advanced Techniques

Chapter 8: the three-tiered Approach

Chapter 9: Design Issues

Part II presents JDBC in a comprehensive tutorial format — its architecture and components and all its features. You’ll learn how to maximize its features and use the most advanced techniques.

After an introduction, you will study database connectivity using JDBC — step-by-step. You will then learn more advanced approaches to interact with a DBMS. Finally, you will study three-tiered architectures and DBMS integration, architecture, and design issues.

Chapter 4
Getting Started With JDBC

In This Chapter

This chapter discusses the mechanisms used by JavaDatabase Conncectivity (JDBC), its programming interfaces, and some typical uses of JDBC.

JDBC is a set of programming interfaces. This chapter discusses which application programming interfaces (APIs) are of interest to the database application developer.

The JDBC Mechanisms

The two major components of JDBC are the JDBC API and the JDBC Driver API. As Figure 4-1 shows, the JDBC API is a programming interface for database applications developers, while the JDBC Driver API is a lower level programming interface for developers of specific drivers. We focus on the JDBC API.


Figure 4-1:  The JDBC APIs.

To write a successful database application with the Java Language Environment and JDBC, certain steps must be taken. These steps are very similar to those taken by C programmers using a Call Level Interface (CLI).

The first step is to create a method for connecting to the database, equivalent of a connection context, associated with a specific database driver. Eventually, parameters are provided to locate a specific database, to allow sign-on as a recognized user giving identification and authentication, and to target a specific database managed by the database management system (DBMS). Once a connection is established, various actions are allowed, including closing it, sending queries, updates, and everything that is called a SQL statement, including requests to execute a precompiled SQL statement and stored procedures. Of course, a mechanism to access the results sets is also provided. Figure 4-2 depicts the basic actions performed on the client and at the database side.


Figure 4-2:  Actions performed on the client and at the DBMS sides.

A mechanism to handle errors is also provided. Exception management addresses this issue in Java by allowing the developer to call specific procedures after an error occurs.

The next section describes JDBC API interfaces in greater detail.

The JDBC Interfaces

The methods discussed in the prior section are expressed as Java interfaces that are implemented by specific database drivers. The JDBC interfaces that database application developers use are:

  java.sql.DriverManager: A class that provides methods to load drivers and to support the creation of database connections using methods expressed in the java.sql.Driver interface.
  java.sql.Connection: Represents a particular connection on which further actions will be allowed.
  java.sql.Statement: Associated to a connection, it allows SQL statements to be sent to the database.
  java.sql.CallableStatement: It has the same role as java.sql.Statement, but in the context of database stored procedures.
  java.sql.PreparedStatement: It also has the same role as java.sql.Statement, but in the context of precompiled SQL.
  java.sql.ResultSet: Allows access to the rows of a previously executed statement.
  java.sql.ResultSetMetaData: Gives information like type and properties of the columns in a result set.
  java.sql.DatabaseMetaData: Provides information about the database as a whole.

Figure 4-3 is a simplified view of the relationship between the driver manager and the connection, statement, and result set objects.


Figure 4-3:  The relationship between the main JDBC interfaces.

Typical Use

Design choices between things that offer different qualities and drawbacks are made based on the targeted user community. To help evaluate the pros and cons of each choice, the next two sections explore different scenarios and show what is the best solution to address their problems.

Applications and Applets

Java’s fame is mainly from its use of applets downloaded from the Internet. While the types of application are virtually unrestricted, there are still a few matters to consider.

The differences between untrusted applets and traditional applications are:

  An untrusted applet cannot access local files nor open arbitrary network connections to remote hosts. An application accesses the local file system according to the permissions granted the user.
  An untrusted applet cannot rely on specific facilities provided by the underlying operating system, such as a local registry, to locate a database. Applications often rely on such facilities; for example, Open Database Connectivity (ODBC) uses an .INI file or the registry, and most proprietary APIs use specific properties files.
  With an untrusted applet, response times may be arbitrary when traffic peaks arise on the Internet.
  Untrusted applets provide no way to estimate the maximum number of simultaneous users.

As Figure 4-4 shows, an applet is highly dependent on components that are not present in traditional scenarios — networks and application servers.


Figure 4-4:  An applet communicating with a server.

These differences are not necessarily serious drawbacks. Indeed, it is neither necessary nor preferable for applets to look in specific files to locate a database. Complicated connection information may be provided, using parameters passed to the applet from within the HTML page, so the users will not have to remember long and cryptic URLs in order to open the JDBC connection.

Because this applet is trusted, for reasons such as it was signed with a cryptographic key or the user decided to trust applets from a specific host, does not mean that it will be allowed to behave in the same way as another application on the user’s machine.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.