MATLAB: How to get the .net dll to locate the proper JDBC driver

.net dllDatabase Toolboxjdbc connectionjdbc driverMATLAB Compiler

We have a script that was written in Matlab R2015b. The script uses a JDBC driver to connect to SQL Server.
(This driver, sqljdbc_auth.dll, was originally placed into C:\Program Files\MATLAB\R2015b\bin\win64 in order to connect.)
We are compiling the script into a .net DLL so we can call it from a powershell script. When we run the script as a dll and not in Matlab we get this error:
Error using database/fetch (line 22) Unable to find JDBC driver.
What do we need to do to make sure the DLL can find the JDBC driver at runtime?

Best Answer

In order to add JDBC driver with Windows Authetication for SQL Server, you need to add the full path of sqljdbc_auth.dll to javalibrarypath.txt.
In MATLAB,
prefdir
will return the path of preferences folder. By default, it's C:\Users\<user>\AppData\Roaming\MathWorks\MATLAB\R2015b.
Navigate to the preference folder ($prefdir) and edit or create if not exist javalibrarypath.txt.
Add the folder path of sqljdbc_auth.dll (in your case, C:\Program Files\MATLAB\R2015b\bin\win64) to the $prefdir\javalibrarypath.txt.
Next, you need to add the JDBC driver file path of SQL Server to $prefdir\javaclasspath.txt.
In my case, sqljdbc_auth.dll is in "C:\Tools\jdbc\sqljdbc_6.2\enu\auth\x64" and JDBC driver(mssql-jdbc-6.2.1.jre7.jar) is in "C:\Tools\jdbc\sqljdbc_6.2\enu", so these 2 files are as follows.
$prefdir\javalibrarypath.txt
C:\Tools\jdbc\sqljdbc_6.2\enu\auth\x64
$prefdir\javaclasspath.txt
C:\Tools\jdbc\sqljdbc_6.2\enu\mssql-jdbc-6.2.1.jre7.jar
After these files are edited properly, you can compile to deployed MATLAB applications in which SQL Server is used.