Contents | Prev | Next JDBCTM Guide: Getting Started


1 Introduction

This introduction is excerpted from the JDBC book currently in progress at Javasoft. This book, both a tutorial and the definitive reference manual for JDBC, will be published in the spring of 1997 by Addison-Wesley Publishing Company as part of the Java series.

1.1 What Is JDBCTM ?

JDBCTM is a JavaTM API for executing SQL statements. It consists of a set of classes and interfaces written in the Java programming language that makes it easy to send SQL statements to virtually any relational database. In other words, with the JDBC API, it isn't necessary to write one program to access a Sybase database, another program to access an Oracle database, another program to access an Informix database, and so on. One can write a single program using the JDBC API, and the program will be able to send SQL statements to the appropriate database. And, with a program written in the Java programming language, one also doesn't have to worry about writing different programs to run on different platforms. The combination of Java and JDBC lets a programmer write it once and run it anywhere. (We explain more about this later.)

Java, being robust, secure, easy to use, easy to understand, and automatically downloadable on a network, is an excellent language basis for database applications. What is needed is a way for Java applications to talk to a variety of different databases. JDBC is the mechanism for doing this.

JDBC extends what you can do in Java. For example, with Java and the JDBC API, it is possible to publish a web page containing an applet that uses information obtained from a remote database. Or an enterprise can use JDBC to connect all its employees (even if they are using a conglomeration of Windows, Macintosh, and UNIX machines) to one or more internal databases via an intranet. With more and more programmers using the Java programming language, the need for easy database access from Java is continuing to grow.

MIS managers like the combination of Java and JDBC because it makes disseminating information easy and economical. Businesses can continue to use their installed databases and access information easily even if it is stored on different database management systems. Development time for new applications is short. Installation and version control are greatly simplified. A programmer can write an application or an update once, put it on the server, and everybody has access to the latest version. And for businesses selling information services, Java and JDBC offer a better way of getting out information updates to external customers. We will discuss various ways to use JDBC in more detail later.

1.1.1 What Does JDBC Do?

Simply put, JDBC makes it possible to do three things:

  • establish a connection with a database
  • send SQL statements
  • process the results.

    The following code fragment gives a basic example of these three steps:

    Connection con = DriverManager.getConnection (
    						"jdbc:odbc:wombat", "login", "password");
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1");
    while (rs.next()) {
    	int x = getInt("a");
    	String s = getString("b");
    	float f = getFloat("c");
    	}
    

    1.1.2 JDBC Is a Low-level API and a Base for Higher-level API

    JDBC is a "low-level" interface, which means that it is used to invoke (or "call") SQL commands directly. It works very well in this capacity and is easier to use than other database connectivity APIs, but it was designed also to be a base upon which to build higher-level interfaces and tools. A higher-level interface is "user-friendly," using a more understandable or more convenient API that is translated behind the scenes into a low-level interface such as JDBC. At the time of this writing, two kinds of higher-level APIs are under development on top of JDBC:

    1. an embedded SQL for Java (at least one vendor plans to build this), and
    2. a direct mapping of relational database tables to Java classes (JavaSoft and others have announced plans to do this).
    As interest in JDBC has grown, more developers have been working on JDBC-based tools to make building programs easier, as well. Programmers have also been writing applications that make accessing a database easier for the end user. For example, an aplication might present a menu of database tasks from which to choose. After a task is selected, the aplication presents prompts and blanks for filling in information needed to carry out the selected task. With the requested input typed in, the aplication then automatically invokes the necessary SQL commands. With the help of such an aplication, users can perform database tasks even when they have little or no knowledge of SQL syntax.

    1.1.3 JDBC and ODBC

    At this point, Microsoft's ODBC (Open DataBase Connectivity) API is probably the most widely used interface for accessing relational databases. It offers the ability to connect to almost all databases on almost all platforms. So why not just use ODBC from Java?

    The answer is that you can use ODBC from Java, but this is best done with the help of JDBC in the form of the JDBC-ODBC Bridge. which we will cover shortly. The question now becomes, "Why do you need JDBC?" There are several answers to this question:

    1. ODBC is not appropriate for direct use from Java because it uses a C interface. Calls from Java to native C code have a number of drawbacks in the security, implementation, robustness, and automatic portability of applications.
    2. A literal translation of the ODBC C API into a Java API would not be desirable. For example, Java has no pointers, and ODBC makes copious use of them, including the notoriously error-prone generic pointer "void *". You can think of JDBC as ODBC translated into an object-oriented interface that is natural for Java programmers.
    3. ODBC is hard to learn. It mixes simple and advanced features together, and it has complex options even for simple queries. JDBC, on the other hand, was designed for a wide range of programmers and keeps simple things simple.
    4. A Java API like JDBC is needed in order to enable an "all-Java" solution. When ODBC is used, the ODBC driver manager and drivers must be manually installed on every client machine. When the JDBC driver is written completely in Java, however, JDBC code is automatically installable, portable, and secure on all Java platforms from network computers to mainframes.
    In summary, the JDBC API is a natural Java interface to the basic SQL abstractions and concepts. It builds on ODBC rather than starting from scratch, so programmers familiar with ODBC will find it very easy to learn JDBC. JDBC retains the basic design features of ODBC; in fact, both interfaces are based on the X/Open SQL CLI (Call Level Interface). The big difference is that JDBC builds on and reinforces the style and virtues of Java, and, of course, it is easy to use.

    1.1.4 Two-tier and Three-tier Models

    The JDBC API supports both two-tier and three-tier models for database access.

    In the two-tier model, a Java applet or application talks directly to the database. This requires a JDBC driver that can communicate with the particular database management system being accessed. A user's SQL statements are delivered to the database, and the results of those statements are sent back to the user. The database may be located on another machine to which the user is connected via a network. This is referred to as a client/server configuration, with the user's machine as the client, and the machine housing the database as the server. The network can be an intranet, which, for example, connects employees within a corporation, or it can be the Internet.

    In the three-tier model, commands are sent to a "middle tier" of services, which then send SQL statements to the database. The database processes the SQL statements and sends the results back to the middle tier, which then sends them to the user. MIS directors find the three-tier model very attractive because the middle tier makes it possible to maintain control over access and the kinds of updates that can be made to corporate data. Another advantage is that when there is a middle tier, the user can employ an easy-to-use higher-level API which is translated by the middle tier into the appropriate low-level calls. Also, in many cases the three-tier architecture can provide performance advantages.

    Until now the middle tier has typically been written in languages such as C or C++, which offer fast performance. However, with the introduction of optimizing compilers translating Java bytecode into efficient machine-specific code, it is becoming practical to implement the middle tier in Java. This is a big plus, making it possible to take advantage of Java's robustness, multi-threading, and security features.

    1.1.5 SQL Conformance

    Structured Query Language (SQL) is the standard language for accessing relational databases. One area of difficulty is that although most DBMSs (DataBase Management Systems) use a standard form of SQL for basic functionality, they do not conform to the more recently-defined standard SQL syntax or semantics for more advanced functionality. For example, not all databases support stored procedures or outer joins, and those that do are not consistent with each other. It is hoped that the portion of SQL that is truly standard will expand to include more and more functionality. In the meantime, however, the JDBC API must support SQL as it is.

    One way the JDBC API deals with this problem is to allow any query string to be passed through to an underlying DBMS driver. This means that an application is free to use as much SQL functionality as desired, but it runs the risk of receiving an error on some DBMSs. In fact, an application query need not even be SQL, or it may be a specialized derivative of SQL designed for specific DBMSs (for document or image queries, for example).

    A second way JDBC deals with problems of SQL conformance is to provide ODBC-style escape clauses, which are discussed in SQL Escape Syntax in Statement Objects on page 27. This escape syntax allows a programmer to use SQL functionality from within a JDBC program.

    For complex applications, JDBC deals with SQL conformance in a third way. It provides descriptive information about the DBMS by means of DatabaseMetaData so that applications can adapt to the requirements and capabilities of each DBMS.

    Because the JDBC API will be used as a base API for developing higher-level database access tools and APIs, it also has to address the problem of conformance for anything built on it. The designation "JDBC COMPLIANTTM" was created to set a standard level of JDBC functionality on which users can rely. In order to use this designation, a driver must support at least ANSI SQL-2 Entry Level. (ANSI SQL-2 refers to the standards adopted by the American National Standards Institute in 1992. Entry Level refers to a specific list of SQL capabilities.) Driver developers can ascertain that their drivers meet these standards by using the test suite available with the JDBC API.

    The "JDBC COMPLIANTTM" designation indicates that a vendor's JDBC implementation has passed the conformance tests provided by JavaSoft. These conformance tests check for the existence of all of the classes and methods defined in the JDBC API, and check as much as possible that the SQL Entry Level functionality is available. Such tests are not exhaustive, of course, and JavaSoft is not currently branding vendor implementations, but this compliance definition provides some degree of confidence in a JDBC implementation. With wider and wider acceptance of the JDBC API by database vendors, connectivity vendors, Internet service vendors, and application writers, JDBC is quickly becoming the standard for Java database access.

    The JDBC API is a natural choice for Java developers because it offers easy database access for Java applications and applets. Because JDBC brings together Java and databases, the remainder of this chapter gives a brief overview of each.

    1.2 JDBC Products

    At the time of this writing, a number of JDBC-based products have already been deployed or are under development. Some description of these products will put JDBC in perspective. Of course, the information in this section will quickly become dated, so the reader should consult the JDBC pages on JavaSoft's web site for the latest information. The current JDBC web page is:

    http://splash.javasoft.com/jdbc/
    

    For more information on database products, or if the address above does not work, the JavaSoft web page can be found at:

    http://java.sun.com/
    

    1.2.1 JavaSoft Framework

    JavaSoft provides three JDBC product components as part of the Java Developer's Kit (JDK):

    The JDBC driver manager is the backbone of the JDBC architecture. It actually is quite small and simple; its primary function is to connect Java applications to the correct JDBC driver and then get out of the way.

    The JDBC driver test suite provides some confidence that JDBC drivers will run your program. It tests that a JDBC driver implements all of the JDBC classes and methods and that it provides the Entry Level SQL functionality required for JDBC compliance.

    The JDBC-ODBC bridge allows ODBC drivers to be used as JDBC drivers. It was implemented as a way to get JDBC off the ground quickly, and long term will provide a way to access some of the less popular DBMSs if JDBC drivers are not implemented.

    1.2.2 JDBC Driver Types

    The JDBC drivers that we are aware of at this time fit into one of four categories:

    1. JDBC-ODBC bridge plus ODBC driver: The JavaSoft bridge product provides JDBC access via ODBC drivers. Note that ODBC binary code and in many cases database client code must be loaded on each client machine that uses this driver. As a result, this kind of driver is most appropriate on a corporate network where client installations are not a major problem, or for application server code written in Java in a 3-tier architecture.
    2. Native-API partly-Java driver: This kind of driver converts JDBC calls into calls on the client API for Oracle, Sybase, Informix, DB2, or other DBMS. Note that, like the bridge driver, this style of driver requires that some binary code be loaded on each client machine.
    3. JDBC-Net all-Java driver: This driver translates JDBC calls into a DBMS-independent net protocol which is then translated to a DBMS protocol by a server. This net server middleware is able to connect its all-Java clients to many different databases. The specific protocol used depends on the vendor. In general, this is the most flexible JDBC alternative. It is likely that all vendors of this solution will provide products suitable for Intranet use. In order for these products to also support Internet access, they must handle the additional requirements for security, access through firewalls, etc., that the Web imposes. Several vendors are adding JDBC drivers to their existing database middleware products.
    4. Native-protocol all-Java driver: This kind of driver converts JDBC calls into the network protcol used by DBMSs directly. This allows a direct call from the client machine to the DBMS server and is a practical solution for Intranet access. Since many of these protocols are proprietary, the database vendors themselves will be the primary source. Several database vendors have these in progress.
    Eventually, we expect that driver categories 3 and 4 will be the preferred way to access databases from JDBC. Driver categories 1 and 2 are interim solutions where direct all-Java drivers are not yet available. Category 4 is in some sense the ideal; however, there are many cases where Category 3 may be preferable: e.g., where a thin DBMS-independent client is desired, or if a DBMS-independent protocol is standardized and implemented directly by many DBMS vendors.

    1.2.3 Obtaining JDBC Drivers

    At the time of this writing, there are dozens of drivers in Category 1: ODBC drivers that can be used with JavaSoft's bridge. There are currently about a dozen Category 2 drivers built on top of native APIs for DBMSs. There are a few Category 3 drivers. There are very few Category 4 drivers currently since it takes at least a few months to write native protocol drivers in Java. We expect, however, that there will be Category 4 drivers for all the major DBMSs sometime in 1997.

    To get the latest information on drivers, check the JDBC web page mentioned earlier. The first vendors with Category 3 drivers available were SCO, Open Horizon, Visigenic, and WebLogic. The leading vendor in ODBC drivers is Intersolv; JavaSoft did the JDBC-ODBC Bridge and JDBC Driver Test Suite work with Intersolv.

    1.2.4 Other Products

    Various application development tools are underway that will connect to JDBC. Watch the JavaSoft pages for updates.

    JavaSoft or a standards group may attempt to standardize on a network protocol that is DBMS-independent. In that case JavaSoft could bundle the "client side" implementing the protocol with the JDK (Java Developer's Kit), and various vendors could provide the server side:



    Contents | Prev | Next
    jdbc@wombat.sun.com or jdbc-odbc@wombat.sun.com
    Copyright © 1996 Sun Microsystems, Inc. All rights reserved.