MATLAB: How to connect JDBC with compiled application

database matlabDatabase ToolboxMATLABMATLAB Compiler

Hi,
We are using Matlab to process some data and want to output the result in a database.
To do so, we tried with the Database Toolbox. After some troubleshooting with JDBC Driver path which didn't seem to work out of the box, we have been able to run the script in Matlab this way:
conn = database(databasename, username, password, 'Server', server, 'PortNumber', port, 'Vendor', 'Microsoft SQL Server', 'AuthType', authType);
We are using the Matlab Compiler to produce an exe, the Compiler state that it should support the toolbox here.
When we do so, we still have the following error, which tell us the function does not seem to exists, an error we do not have in Matlab.
We also found this bug, but it is maked as fixed.
What can we do to troubleshoot this issue?
Thanks
For reference here a sample of the full script.
  • writetable were added only for debugging
  • datainsert is deprecated, sqlwrite should be used, but I could would not let me insert data only and was always generating a create table.
function [] = tampon(server, port, databasename, authType, username, password)
writetable(cell2table({'2'}),'log.txt','WriteVariableNames',false)
conn = database(databasename, username, password, 'Server', server, 'PortNumber', port, 'Vendor', 'Microsoft SQL Server', 'AuthType', authType);
writetable(cell2table({'3'}),'log.txt','WriteVariableNames',false)
load('output.mat');
writetable(cell2table({'4'}),'log.txt','WriteVariableNames',false)
output = output(:,1:5);
writetable(cell2table({'5'}),'log.txt','WriteVariableNames',false)
datainsert(conn,'dbo.test', cellstr(output.Properties.VariableNames), output);
writetable(cell2table({'6'}),'log.txt','WriteVariableNames',false)
close(conn);
writetable(cell2table({'7'}),'log.txt','WriteVariableNames',false)
end

Best Answer

Regarding your last comment, from Powershell and Command Prompt, number is passed to compiled MATLAB function as characters, so, converting to double (str2double) is necessary.
function [] = tampon(server, port, databasename, authType, idEnvironment)
which database
if ischar(port)
port = str2double(port);
end
conn = database(databasename, '', '', 'Server', server, 'PortNumber', port, 'Vendor', 'Microsoft SQL Server', 'AuthType', authType)
If you still gets an error, add disp(conn) and confirm JDBC is included properly to compiled exe.
If conn says "JDBC not found error", please try adding "mssql-jdbc-6.2.1.jre8.jar" and "sqljdbc_auth.dll" in "Files required for your application to run" in Application Compiler Window.