1183-1186

Previous Table of Contents Next

Page 1183

CHAPTER 53

Web Database Connectivity

IN THIS CHAPTER

  • Writing JDBC Applets and Applications 1185
  • Handling JDBC Exceptions 1197
  • JDBC Debugging 1200
  • Deployment Issues 1201
  • Using RMI 1204
  • Using JavaIDL 1205
  • Applets Versus Java Application and Security Considerations 1207
  • Advanced JDBC Application Design 1209
  • Performance Considerations and Multithreading 1209
  • Writing a JDBC Driver 1211
  • JDBC-Native Interface Mapping 1211
  • JDBC Conformance 1211
  • Using JNI 1212
  • The Future of JDBC 1214

Page 1184

JDBC (Java Database Connectivity) is an application programming interface implemented as a set of Java classes and interfaces. It is designed to let Java applets and applications communicate with relational database engines in a platform-independent manner.

For the many programmers who have developed Open Database Connectivity (ODBC) applications, the JDBC API might seem familiar. In many respects, the JDBC interface resembles a set of object-oriented ODBC wrappers. This similarity is not a coincidence but is nearly unavoidable because both ODBC and JDBC are based on the X/Open SQL Call Level Interface and ANSI SQL-92 standards.

Although they are based on these common standards, JDBC and ODBC have significant differences. The ODBC API is based on a C language implementation, whereas JDBC is based on a Java implementation. A Java-based API gives JDBC the distinct advantage of object orientation. The Java-based API also has the advantage of greater portability because Java code is interpreted at runtime by a virtual machine, but C code must be compiled for a specific platform. On the other hand, most native RDBMS interfaces are C based and rely heavily on pointers, which are not available in the Java language. As a result, many JDBC drivers rely on platform-specific libraries that serve as a bridge between the Java implementations and the native RDBMS-specific library.

JDBC does not necessarily require JDBC-compliant drivers. The JDBC-ODBC bridge is included as part of the JavaSoft JDBC SDK, allowing applications to access ODBC drivers through the JDBC API. The bridge actually serves as a JDBC driver, translating JDBC calls to the appropriate ODBC calls and passing them to the ODBC driver manager for processing. As far as ODBC is concerned , the bridge is just another client application.

As of this writing, there are a handful of pure Java JDBC drivers available from major vendors . These drivers are implemented as separate client and server components . "Pure Java," in this case, applies to the client component, which communicates with the server component via IIOP, HTTP, or, in some cases, a proprietary protocol. The server component can be implemented any number of ways. It can communicate with a DBMS-specific library directly, or it can be a client of JDBC, ODBC, an ORB, and so on. Figure 53.1 illustrates the most common JDBC configurations that have been implemented to date.

This chapter first presents the JDBC API in an implementation-independent context. The specific configurations illustrated in Figure 53.1 and the issues related to them are discussed in a separate section. The final section of this chapter focuses on advanced JDBC design topics, including a brief overview of driver design and development.

Page 1185

Figure 53.1.
JDBC client/server and
three-tiered configura
tions.

Writing JDBC Applets and Applications

A basic understanding of the Java language is assumed in the following descriptions and examples. This section is dedicated to the java.sql package, portions of which rely on other classes in the JDK. Readers unfamiliar with the core Java API should refer to the Java API documentation for information on classes external to the java.sql package. The information and examples presented are based on version 1.1.x of the JDK and 1.2x of the JDBC API.

Page 1186

The following sections are intended to demonstrate the uses of specific JDBC classes and interfaces. Exception handling is omitted from the examples presented in the these sections; JDBC exception handling is covered as a separate topic. For now, assume that each code segment is enclosed in a try block.

Connecting to the Database

Before you can establish a connection to the RDBMS, you must load the appropriate JDBC driver. You can explicitly load a driver using the forName() method of java.lang.Class. To load a driver, pass its fully qualified class name to this method. For example, if you are using an Oracle8 driver from Acme, you can load the driver with the following statement:

 Class.forName("acme.jdbc.oci8.Driver"); 

This makes the driver available to the driver manager, which is used to establish a connection. The getConnection() method of the DriverManager class has three forms:

 static public synchronized Connection getConnection(String url); static public synchronized Connection getConnection(String url,        Properties props); static public synchronized Connection getConnection(String url,        String userid, String password); 

Note that each form returns a Connection interface (implemented by the driver) and accepts a URL (uniform resource locator) as the first argument of the form:

 jdbc:subprotocol:subname 

Consult your driver documentation for the appropriate driver name, URL, and getConnection() form to use. Most JDBC drivers require additional connection properties to identify the specific database instance to access, provide authentication, set session parameters, and so on. For example, most Oracle drivers require a SQL*Net connect string, username, and password. These properties are supplied to the DriverManager as demonstrated in the following code:

 java.util.Properties props = new java.util.Properties(); props.put("host", "ORCL"); props.put("user", "scott"); props.put("password", "tiger"); java.sql.Connection DbConn = DriverManager.getConnection("jdbc:Oracle8", props); 

Driver-independent applications can populate a list of known drivers from a configuration file and use the registerDriver() method of the DriverManager to load the driver selected by the user. You can then use the Driver interface to determine the connection properties required for the selected driver. The getPropertyInfo() method of the Driver interface returns an array of DriverPropertyInfo objects. You can use each of these objects to determine the name of a specific property and whether it is required to connect. The getPropertyInfo() method accepts the database URL and a java.util.Properties reference containing a proposed set of connection properties. The Properties argument is provided because the use of a particular property might cause another property to be required. You can pass an empty Properties object to getPropertyInfo() in most cases. The following code segment demonstrates one possible use of getPropertyInfo() and the array of DriverPropertyInfo objects it returns:

Previous Table of Contents Next


Oracle Unleashed
Oracle Development Unleashed (3rd Edition)
ISBN: 0672315750
EAN: 2147483647
Year: 1997
Pages: 391

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net