Skip to content

Connect with DriverManager

Mark Rotteveel edited this page Jul 24, 2015 · 2 revisions

java.sql.DriverManager is the first and most simple way to access a JDBC driver. A type 4 JDBC driver does not require any special configuration. You make it available to your application via the classpath as any regular JDBC driver. Type 2 and Embedded Server JDBC drivers require a JNI link to access client or embedded server libraries. This puts additional requirements on the configuration.

Obtaining a connection via DriverManager

java.sql.DriverManager is the first and the most simple way to access drivers using JDBC driver. Starting with Java 6 (JDBC 4.0) the DriverManager will automatically load JDBC drivers that are on the classpath (provided the driver provides an entry in /META-INF/services/java.sql.Driver), removing the need for specifically loading a driver.

You can connect to the database as follows.

Using default configuration:

import java.sql.*;
...
Connection connection = DriverManager.getConnection(
   "jdbc:firebirdsql://localhost:3050/c:/database/employee.fdb", 
   "SYSDBA",
   "masterkey");

Using custom connection parameters:

import java.util.*;
import java.sql.*;
...
Properties props = new Properties();
props.setProperty("user", "SYSDBA");
props.setProperty("password", "masterkey");
props.setProperty("encoding", "UTF8");
 
Connection connection = DriverManager.getConnection(
   "jdbc:firebirdsql://localhost:3050/c:/database/employee.fdb", 
   props);

You can find more information on JDBC in the JDBC Tutorial

Alternative methods of loading a Driver

In some cases (and Java 5 or earlier) you may need to explicitly load a driver. JDBC supports the following methods.

jdbc.drivers property

You can include a list of JDBC driver classes to load in the jdbc.drivers startup JVM property, for example the JVM startup may look like below

java -cp jaybird-full-2.2.2.jar;myapplication.jar 
    -Djdbc.drivers=org.firebirdsql.jdbc.FBDriver org.my.Application

Class.forName(...)

Alternatively you can tell the Driver to register itself in the java.sql.DriverManager class. This is done by triggering the static class initializer by explicitly loading the driver class:

Class.forName("org.firebirdsql.jdbc.FBDriver");

Configuring Type 2 driver

TODO: Requires additional info

The Type 2 JDBC driver requires the following components to be installed on the client computer

  • Firebird client (fbclient.dll and corresponding files and registry entries);
  • Jaybird JNI library, jaybird22(_x64).dll on Windows and libjaybird22(_x64).so on Linux.

The Jaybird JNI library should be accessible to JVM. This means that it must be either located in path(%PATH% on Windows or $PATH on Linux) or the directory where the library is located must be specified in java.library.path startup property of the JVM. The Firebird client library should be accessible through PATH environment variable (java.library.path is not taken into the account).

Configuring embedded driver

The same requirements as above, however the library in this case should be fbembed.dll on Windows or libfbembed.so on Linux.

You should NOT rename fbembed.dll into fbclient.dll, as suggested in README for Firebird Embedded. This step is needed only for drivers and database access components that do not support Firebird Embedded directly. This is not the case with Jaybird.