How do I connect JDBC with compiled application?

3 views (last 30 days)
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
  6 Comments
Kojiro Saito
Kojiro Saito on 3 Apr 2019
So, database command is pointing to the right m file.
How did you set up JDBC driver in MATLAB and how do you call compiled exe in Command Prompt?
Adrien Corbin
Adrien Corbin on 4 Apr 2019
Edited: Adrien Corbin on 5 Apr 2019
We are using a console application.
We actually have 2, for testing purpose :
run.m -> run.exe
tampon('myserver',1234, 'mydatabase','Windows', 5)
tampon.m -> tampon.exe
function [] = tampon(server, port, databasename, authType, idEnvironment)
which database
conn = database(databasename, '', '', 'Server', server, 'PortNumber', port, 'Vendor', 'Microsoft SQL Server', 'AuthType', authType);
load('output.mat');
output = output(:,1:5);
datainsert(conn,'dbo.tampon', cellstr(output.Properties.VariableNames), output);
close(conn);
end
We are calling the exe directly for run.exe and tampon.exe with the following command in powershell
.\tampon.exe 'myserver' 1234 'mydatabase' 'Windows' 5
After a few changes with your "which" recommendation we have been able to proceed through missing "database" and "datainsert" references.
However we still can't connect, here is the log.txt output
C:\Program Files\MATLAB\MATLAB Runtime\v96\mcr\toolbox\database\database\database.m
Error using database.jdbc.connection/datainsert (line 35)
Invalid connection.
Error in tampon (line 11)
Error in run (line 2)
Note that the same parameters work in matlab. We might be facing our first problem with JDBC Driver. Where can I specify the driver to use in the Matlab Runtime as mentioned here at step 3.
Thanks for your help

Sign in to comment.

Accepted Answer

Kojiro Saito
Kojiro Saito on 5 Apr 2019
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.
  6 Comments
Michaela Konstantinou
Michaela Konstantinou on 1 Feb 2022
Edited: Michaela Konstantinou on 1 Feb 2022
When I try to use a database command in web app (.ctf) I have compiled including .jar .dll files I get the following error
Error using database (line 59)
2022-02-01 02:16:22 JDBC data source does not contain driver location. Use databaseConnectionOptions to specify JDBC driver location.
Do I have to save the datasources again inside the web app using saveAsDataSource(opts)? If so, I have tried it but when I set the connection options I dont know what value I should map to
'JDBCDriverLocation'
parameter in defining opts as shown in the code below
vendor = 'Microsoft SQL Server';
opts = databaseConnectionOptions("jdbc", vendor);
opts = setoptions(opts, 'DataSourceName' , datasourceName , ...
'DatabaseName' , databaseName , ...
'Server' , 'PRDMTLDB.XXXXX.com', ...
'AuthType' , 'Windows');
If I use the opts withouth defining driverPath (as shown above) I get the below error
Error using database.options.jdbc.sqlserver.SQLConnectionOptions/checkTestIsValid (line 40)
2022-02-01 02:22:22 DataSourceName, JDBCDriverLocation, Server and PortNumber are required parameters.
2022-02-01 02:22:22 Error in database.options.jdbc.sqlserver.SQLConnectionOptions/testConnection (line 97)
I have also restarted the server after the apps deployment (i.e., copy to the web apps folder).
Can you please help me with establishing an SQL connection from a web app?
Kojiro Saito
Kojiro Saito on 1 Feb 2022
By adding SQL Server's JDBC driver (.jar file) to "Files installed for your end user" in Application Compiler, and "Files required for your app to run" in Web App Compiler, the JDBC driver will be included in the package.
Then, call dabase with database(databasename,username,password,Param1,ParamValue1,...,ParamN,ParamValueN) because it specifies JDBC connection.
conn = database(databaseName, userName, password, ...
'Vendor','Microsoft SQL Server','Server','PRDMTLDB.XXXXX.com', ...
'AuthType','Server','PortNumber', 1433);
Hope this helps.

Sign in to comment.

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!