Connecting SQL Server to a Java database

SQL Server can connect to a non-SQL Server database by using a linked server. The linked server mechanism can use a piece of middleware known as an ODBC driver to connect SQL Server to third-party back ends such Oracle, MySQL, and Salesforce.

The equivalent of an ODBC driver for a Java database is a JDBC driver. Microsoft do not provide a linked server interface that supports JDBC. Rather, you need to use the ODBC linked server interface (Microsoft OLE DB Provider for ODBC Drivers) with an ODBC driver than can translate between ODBC and JDBC. For example, the ODBC-JDBC Gateway. The ODBC-JDBC Gateway connects an application that uses ODBC to a database that's accessible through JDBC. To the application, the ODBC-JDBC Gateway is an ODBC driver. To the JDBC driver, the ODBC-JDBC Gateway is a Java application.

Use the ODBC-JDBC Gateway to:

Installing and licensing the ODBC-JDBC Gateway

  1. Download the Windows ODBC-JDBC Gateway.
  2. Install and license the ODBC-JDBC Gateway on the Windows machine where SQL Server and the JDBC driver are installed.

    For installation instructions, refer to the ODBC-JDBC Gateway documentation.

Configuring an ODBC data source

Before you can use the ODBC-JDBC Gateway to connect your ODBC application to JDBC, you need to configure a system ODBC data source. An ODBC data source stores the connection details for the target database.

You configure ODBC data sources in ODBC Data Source Administrator, which is included with Windows. There are two versions of ODBC Data Source Administrator, one 32-bit, one 64-bit. You need to run the 32-bit version if your SQL Server instance is 32-bit. You need to run the 64-bit version if your SQL Server instance is 64-bit.

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')

To run the 32-bit ODBC Data Source Administrator, in the Windows Run dialog box, enter:

%windir%\syswow64\odbcad32.exe

To run the 64-bit ODBC Data Source Administrator, in the Windows Run dialog box, enter:

odbcad32.exe

Use ODBC Data Source Administrator to create your ODBC-JDBC Gateway data source.

Creating an ODBC-JDBC Gateway ODBC data source

  1. Choose the System DSN tab, and then choose Add.
  2. In the Create New Data Source dialog box, choose ODBC-JDBC Gateway, and then choose Finish.
  3. Complete the ODBC-JDBC Gateway DSN Setup dialog box:
    Setting Description
    DSN OJG_DSN
    User Name The user name for your Java back end, if required.
    Password The password for user name.
    Driver Class Refer to your JDBC driver documentation for the value you need to enter here.
    Class Path Use the Add button to browse to your JDBC driver JAR or ZIP file.
    URL The JDBC URL used by your JDBC driver to connect to the Java back end. Refer to your JDBC driver documentation for the syntax that you need to use.
  4. Use the Test button to verify that you can successfully connect to Java back end.

Example: Retrieve JDBC 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, enter OJG.
  4. From the Provider list, choose Microsoft OLE DB Provider for ODBC Drivers.
  5. In the Data source box, enter the name of your ODBC-JDBC Gateway 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 ODBC-JDBC Gateway, it will not have the latest version of the System PATH environment variable. The ODBC-JDBC Gateway 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 ODBC-JDBC Gateway.
    • If you made a mistake when specifying the ODBC-JDBC Gateway 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 JDBC data either by using a:
    • Four-part table name in a distributed query.

      A four-part table name has the format:

      server_name.[database_name].[schema_name].table_name.

      Depending on your Java back end, you may need to omit the database name and schema. For example:

      SELECT * from OJG...my_table
      
    • Pass-through query in an OPENQUERY function. For example:
      SELECT * FROM OPENQUERY(OJG, 'SELECT * FROM my_table')

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