How do I connect Microsoft SQL Server to Apache Derby?

The Apache Derby ODBC driver lets you connect Microsoft SQL Server to Apache Derby and:

The Apache Derby ODBC driver is available to download from the Easysoft web site:

  1. Download the Windows Apache Derby ODBC driver.
  2. Install and license the Apache Derby ODBC driver on the machine where SQL Server is installed.

    For installation instructions, refer to the Apache Derby ODBC driver documentation.

Before you can use the Apache Derby ODBC driver to connect SQL Server to Apache Derby, you need to configure an ODBC data source. An ODBC data source stores the connection details for the target database (in this case, Apache Derby) and the ODBC driver that's required to connect to it (in this case, the Apache Derby ODBC driver).

You can configure a user ODBC data source, which is only available to the user who creates it, or a system ODBC data source, which is available to all users on the machine. You configure ODBC data sources in ODBC Data Source Administrator, which is included with Windows.

There are two versions of ODBC Data Source Administrator on this platform. The version of ODBC Data Source Administrator that you need to run depends on whether you have a 32-bit or a 64-bit version of SQL Server. To find out which version of SQL Server you have, connect to your SQL Server instance, and then run this SQL statement:

select SERVERPROPERTY('edition')

If you have the 64-bit version of SQL Server and want to use a linked server with the Apache Derby ODBC driver, you need to run 64-bit version of ODBC Data Source Administrator. To do this, in the Windows Run dialog box, enter:

%windir%\system32\odbcad32.exe

If you have the 32-bit version of SQL Server or want to use SSIS with the Apache Derby ODBC driver, you need to run 32-bit version of ODBC Data Source Administrator. To do this, in the Windows Run dialog box, enter:

%windir%\syswow64\odbcad32.exe

Use ODBC Data Source Administrator to create your Apache Derby ODBC driver data source:

  1. Do one of the following:
    • To create a user data source, in the User DSN tab, choose Add.

      –Or–

    • To create a system data source, choose the System DSN tab, and then choose Add.
  2. In the Create New Data Source dialog box, choose Easysoft ODBC-Derby Driver, and then choose Finish.
  3. Complete the Easysoft ODBC-Derby Driver DSN Setup dialog box:
    Setting Value
    DSN Apache Derby
    Database The Apache Derby database you want to connect to.
    User Name If you need to specify a user name to access your Apache Derby database, enter it here. Otherwise, leave this field blank.
    Password The password for the Apache Derby user name, if applicable.
  4. Use the Test button to verify that you can successfully connect to Apache Derby.

You can now connect SQL Server to Apache Derby.

Example: Retrieving Apache Derby data by using a linked server

  1. In Microsoft SQL Server Management Studio, connect to the SQL Server instance you want to create the linked server against.

    You need to log on with an account that is a member of the SQL Server sysadmin fixed server role to create a linked server.

  2. Right-click Server Objects. From the pop-up menu choose New > Linked Server.
  3. In the Linked server box, type Derby.
  4. From the Provider list, choose Microsoft OLE DB Provider for ODBC Drivers.
  5. In the Data source box, type the name of your Apache Derby ODBC data source, and then choose OK.

    SQL Server verifies the linked server by testing the connection.

    • If you get the error "Specified driver could not be loaded due to system error 126: The specified module could not be found," choose Yes when prompted whether to keep the linked server. You need to restart your SQL Server instance before you can use the linked server. If SQL Server was already running when you installed the Apache Derby ODBC driver, it won't have the latest version of the System Path environment variable. The Apache Derby ODBC driver Setup program adds entries for the driver to the System Path. Restarting the instance makes these changes available to SQL Server, allowing it to load the Apache Derby ODBC driver.
    • If you made a mistake when specifying the Apache Derby ODBC data source, you get the error "Data source name not found and no default driver specified. If you get this error, choose No when prompted whether to keep the linked server and edit the value in the Data source box.
  6. You can query your Apache Derby data either by using a:
    • Pass-through query in an OPENQUERY function. For example:
      SELECT * FROM OPENQUERY(DERBY, 'SELECT * FROM FIRSTTABLE')
      

      SQL Server sends pass-through queries as uninterpreted query strings to the Apache Derby ODBC driver. This means that SQL Server does not apply any kind of logic to the query or try to estimate what that query will do.

Example: Integrating Apache Derby with SQL Server by using SQL Server Integration Services (SSIS)

These instructions assume that:

  1. Create a .csv file named countries.csv with the following contents:
    COUNTRY,COUNTRY_ISO_CODE,REGION
    Afghanistan,AF,Asia
    Albania,AL,Europe
    Algeria,DZ,North Africa
    American Samoa,AS,Pacific Islands
    Angola,AO,Africa
    Argentina,AR,South America
    Armenia,AM,Europe
    Australia,AU,Australia and New Zealand
    Austria,AT,Europe
    Azerbaijan,AZ,Central Asia
  2. In Visual Studio, create a new Integration Services Project.
  3. Drag a Data Flow Task from the Toolbox to the Control Flow tab.
  4. Choose the Data Flow tab.
  5. Drag a Flat File Source from the Toolbox to the Data Flow tab, and then press Return.

    Flat File Source is under the Other Sources list.

  6. In the Flat File Source Editor, choose New.
  7. In the Flat File Connection Manager Editor dialog box, browse for your .csv file.
  8. Choose Preview, and then choose OK to exit the dialog boxes.
  9. Drag an ODBC Destination from the Toolbox to the Data Flow tab, and then press Return.

    ODBC Destination is in the Other Destinations list.

  10. Select the Flat File Source. Drag the blue arrow over to the ODBC Destination.
  11. Select the ODBC Destination, and then press Return.
  12. In the ODBC Destination dialog box, choose New.
  13. In the Configure ODBC Connection Manager dialog box, choose New.
  14. In the Connection Manager dialog box, choose your Apache Derby ODBC data source, and then use the OK button to return to the ODBC Source dialog box.
  15. In the Name of the table or the view list, choose Countries.
  16. Choose Mappings. Use the OK button to exit the ODBC Destination dialog boxes.
  17. Choose the Start button to insert the records from the .csv file into Apache Derby.