Thur, 11 June 2020
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 backends 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 via 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:
- Use a linked server to work with JDBC data, just as if you were working with data stored in a SQL Server table.
- Integrate JDBC data with SQL Server by using SQL Server Integration Services (SSIS).
- Download the Windows ODBC-JDBC Gateway.
Install and license the ODBC-JDBC Gateway on the Windows machine where SQL Server / the JDBC driver are installed.
For installation instructions, see the ODBC-JDBC Gateway documentation.
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 Administrator, which is included with Windows. There are two versions of ODBC 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:
To run the 32-bit ODBC Administrator, in the Windows Run dialog box, type:
To run the 64-bit ODBC Administrator, in the Windows Run dialog box, type:
Use ODBC Administrator to create your ODBC-JDBC Gateway data source.
Creating a ODBC-JDBC Gateway ODBC Data Source
- Choose the System DSN tab, and then choose Add.
- In the Create New Data Source dialog box, choose ODBC-JDBC Gateway, and then choose Finish.
Complete the ODBC-JDBC Gateway DSN Setup dialog box:
Setting Description DSN OJG_DSN User Name The user name for your Java backend, 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 / ZIP file. URL The JDBC URL used by your JDBC driver to connect to the Java backend. Refer to your JDBC driver documentation for the syntax that you need to use.
- Use the Test button to verify that you can successfully connect to Java backend.
Example: Retrieve JDBC Data by using a Linked Server
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.
- Right-click Server Objects. From the pop-up menu choose New > Linked Server.
- In the Linked server box, type "OJG".
- From the Provider list, choose Microsoft OLE DB Provider for ODBC Drivers.
In the Data source box, type 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.
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:
Depending on your Java backend you 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.
- Four part table name in a distributed query.