Welcome Guest!
Create Account | Login
Locator+ Code:

FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

email article
printer friendly
get the code

Selecting JDBC Drivers
Give your plug-in the ability to select and load JDBC drivers to support a variety of databases
by Kevin Jones

April 6, 2006

In a previous installment we set up a plug-in to display JDBC requests by including the JDBC driver's JAR files in the plug-in JAR file. Doing so obviously raises an issue because one of the aims of the plug-in is to be generic. Here we will examine how to load JDBC drivers and how to use Standard Widget Toolkit (SWT) to select the JAR files containing those drivers.


JDBC drivers are contained in JAR files. For example, the Microsoft SQL driver classes that I've been using so far are contained in three JARs: msbase.jar, mssqlserver.jar, and msutil.jar. Currently these files are included in the plug-in's JAR file and therefore on the plug-in's classpath. For the plug-in to be useful we need to be able to dynamically load the driver code. The first part of this challenge is to allow the user to select the JARs that will later be added to the classpath.

We start by adding another section to the plug-in's user interface (UI) to list the JARs that we'll use. The UI will contain three buttons: one to add JAR files, one to remove a single JAR file, and one to remove all JAR files (see Figure 1). The Remove and Remove All button implementations are straightforward; we won't spend time on those implementations here (of course, all of the code is available at www.javapro.com, where you'll find those implementations). Instead, let's look at the Add implementation.

Standard Opening
The code for the Add widgetSelected handler uses the SWT FileDialog class to display a dialog box for file selection. This dialog will use the operating system's underlying Open dialog and fits in with the standard user experience. To create the FileDialog you need to provide a couple of parameters: the shell the dialog belongs to and a set of flags to control the dialog's UI. The code looks like this:

// _add is a reference to the 
// 'Add' button in the panel
  new SelectionListener()
  public void widgetSelected(
    SelectionEvent arg0)
    FileDialog fileDialog = 
      new FileDialog(

This code will create an Open (SWT.OPEN) dialog parented by Eclipse. It will also allow you to choose multiple files (SWT.MULTI), which is exactly what we need. Once we have the dialog object, we want to display it, but before we do that we have to tell the dialog which files to display. The dialog has two methods: setFilterNames() and setFilterExtensions(). The setFilterExtensions() method is used by the dialog to filter the files being displayed; the setFilterNames() method is used by the dialog to allow the user to choose the filter the dialog will use. We call the methods this way:

    new String[] { 
    "JAR Files (*.jar)", 
    "Zip Files (*.zip)", 
    "All Files (*.*" });

    new String[] { "*.jar", 
    "*.*" });

Back to top

Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTPOnline Home