Java Reference
In-Depth Information
JDBC was designed to be a rich API. In other words, there are dedicated methods to handle anything
you might want to do. These methods are specified in a set of interface classes. To create a JDBC
driver that can be registered with the DriverManager , all of these interface methods must be
implemented. This is done using implementation base classes.
The implementation base classes implement all the methods defined in the interfaces in a minimal form.
These methods simply throw an exception when called. The JDBC driver classes are simply extensions
of the implementation base classes, which override the methods required to get the job done. The first
few lines of one of these implementation classes is shown in Listing 19-2 to give you the idea.
Listing 19-2: Typical implementation base class
package JavaDatabaseBible.ch19.JDBCImpl;
import java.sql.*;
public class JDBCStatementImpl implements java.sql.Statement {
public JDBCStatementImpl() {
}
public void setFetchSize(int fetchSize) throws SQLException {
throw new SQLException("not supported");
}
public int getFetchSize() throws SQLException {
throw new SQLException("not supported");
}
If you don't feel like typing literally hundreds of methods that only throw exceptions, you can download
the implementation classes from the Web site. Alternatively, if your application doesn't need to register
the driver with the java.sql.DriverManager , you can simply remove the extends clause from the
class definitions.
Our SQL engine is also limited, but it, too, is expandable. The SQL engine handles the basic, generic
parsing of the SQL commands themselves. This code is applicable to any SQL application. Our
application implements only a subset of possible commands, allowing us to process common queries as
well as document creation and updating.
Our XML document handlers extend the basic SQL engine classes to provide XML-specific data access
and update capabilities. Obviously, if you want to create your own storage-medium handlers, perhaps
for use with arrays rather than as XML documents, you can simply plug them in place of ours.
Implementing the JDBC Classes
The inner workings of JDBC have been discussed at some length in earlier chapters — in particular, in
Chapter 4 . The following brief overview discusses how the classes work together.
The XMLDriver class
The function of the DriverManager is to provide basic services for managing JDBC drivers. Drivers
can be loaded either during initialization or on request, using Class.forName() . All drivers contain a
static initializer, as specified in Sun's JDBC API guide, that creates an instance of the driver and
Search WWH ::




Custom Search