About InterClient Drivers

From InterBase

Go Up to Developing InterClient Programs


This section describes how to load the InterClient driver and how to explicitly create the InterClient driver.

Loading the InterClient Driver

The InterClient driver must be loaded before your application can attempt to connect to an InterBase database. To explicitly load the InterClient driver with the DriverManager, include the following line in your program before using the driver to establish a database connection:

Class.forName("interbase.interclient.Driver");

The first time the Java interpreter sees a reference to interbase.interclient.Driver, it loads the InterClient driver. When the driver is loaded, it automatically creates an instance of itself, but there is no handle for it that lets you access that driver directly by name. This driver is anonymous; you do not need to reference it explicitly to make a database connection. You can make a database connection simply by using the java.sql.DriverManager class.

It is the responsibility of each newly loaded driver to register itself with the DriverManager; the programmer is not required to register the driver explicitly. After the driver is registered, the DriverManager can use it to make database connections.

Explicitly Creating the InterClient Driver

When writing a client program, you can interact either with the DriverManager class or with a database driver object directly. To reference an InterClient driver directly, you must use the java.sql.Driver class to explicitly create an instance of the driver. This instance is in addition to the anonymous one that's created automatically when the InterClient driver is loaded:

java.sql.Driver driver = new interbase.interclient.Driver();

Now you can reference the driver classes and methods with driver.XXX(). If all you need to do is connect to the database and execute SQL statements, you do not need to create a driver object explicitly; the DriverManager handles everything for you. However, there are a few cases when you need to reference the driver by name. These include:

  • Getting information about the driver itself, such as a version number.
  • Tailoring a driver for debugging purposes. For more information, see Debugging your Application.

The DriverManager sees a driver as only one of many standard JDBC drivers that can be loaded. If you need to create a connection to another type of database in the future, you need only to load the new driver with forName() or declare another driver explicitly with

java.sql.Driver driver = new XXX.Driver

Using java.sql.driver Methods

The java.sql.Driver class has different methods than java.sql.DriverManager. If you want to use any of the java.sql.Driver methods, you need to create an explicit driver object. The following are a few of the driver methods:

  • getMajorVersion() gets the driver's major version number.
  • getMinorVersion() gets the driver's minor version number.

The example below shows how to interact with the database by referencing the driver directly:

//create the InterClient driver object as a JDBC driver
java.sql.Driver driver = new interbase.interclient.Driver();
//get the connection object
java.sql.Connection connection = driver.connect(dbURL, properties);
//reference driver to get the driver version number
java.sql.String version = driver.getMajorVersion() + driver.getMinorVersion();
System.out.print("You're using driver", + version");
Important:
If your application ever needs to access non-InterBase databases, do not define a driver object as a type interbase.interclient.Driver as follows:
interbase.interclient.Driver driver = new interbase.interclient.Driver();

This method creates a driver object that is an instance of the interbase.interclient.Driver class, not a generic instance of the java.sql.Driver class. It is not appropriate for a database-independent client program because it hard-codes the InterClient driver into your source code, together with all of the classes and methods that are specific to the InterClient driver. Such applications could access only InterBase databases.


Advance To: