Contents Index Using the UltraLite JdbcDatabase.connect method Connecting to the database using JDBC pdf/preface.pdf

UltraLite User's Guide
  Developing UltraLite Java Applications
    Connecting to and configuring your UltraLite database

Loading and registering the JDBC driver


The UltraLite JdbcDatabase.connect() method discussed in the previous section provides the simplest method of connecting to an UltraLite database. However, you can also establish a connection in the standard JDBC manner, and this section describes how to do so.

UltraLite applications connect to their database using a JDBC driver, which is included in the UltraLite runtime classes (ulrt.jar). You must load and register the JDBC driver in your application before connecting to the database. Use the Class.forName() method to load the driver. This method takes the driver package name as its argument:

Class.forName( "ianywhere.ultralite.jdbc.JdbcDriver");

The JDBC driver automatically registers itself when it is loaded.

Loading multiple drivers 

Although there is typically only one driver registered in each application, you can load multiple drivers in one application. Load each driver using the same methods as above. The DriverManager decides which driver to use when connecting to the database.

getDriver method 

The DriverManager.getDriver(url) method returns the Driver for the specified URL.

Error handling 

To handle the case where the driver cannot be found, catch ClassNotFoundException as follows:

try{
   Class.forName(
      "ianywhere.ultralite.jdbc.JdbcDriver");
} catch(ClassNotFoundException e){
   System.out.println( "Exception: " + e.getMessage() );
   e.printStackTrace();
}

Contents Index Using the UltraLite JdbcDatabase.connect method Connecting to the database using JDBC pdf/preface.pdf