%% Retrieve Message for JDBC Database Connection Test
% Create and configure a JDBC data source to a Microsoft® SQL Server® database. Test the database connection to the JDBC data source and retrieve the error message.
%
% Create an SQL Server data source for a JDBC database connection.

vendor = "Microsoft SQL Server";
opts = databaseConnectionOptions("jdbc",vendor)

% opts is an SQLConnectionOptions object with these properties:
% DataSourceName — Name of the data source
% Vendor — Database vendor name
% JDBCDriverLocation — Full path of the JDBC driver file
% DatabaseName — Name of the database
% Server — Name of the database server
% PortNumber — Port number
% AuthenticationType — Authentication type
%
% Configure the data source by setting the JDBC connection options for the data source SQLServerDataSource, full path to the JDBC driver file, database name toystore_doc, database server dbtb04, port number 54317, and SQL Server authentication.

opts = setoptions(opts, ...
    'DataSourceName',"SQLServerDataSource", ...
    'JDBCDriverLocation',"C:\Drivers\mssql-jdbc-7.0.0.jre8.jar", ...
    'DatabaseName',"toystore_doc",'Server',"dbtb04", ...
    'PortNumber',54317,'AuthenticationType',"Server")

% The setoptions function sets the DataSourceName, JDBCDriverLocation, DatabaseName, Server, PortNumber, and AuthenticationType properties in the SQLConnectionOptions object.
%
% Test the database connection using an incorrect user name and password. The testConnection function returns the logical 0, which indicates the database connection fails. Retrieve and display the error message for the failed connection.

username = "wronguser";
password = "wrongpassword";
[status,message] = testConnection(opts,username,password)

%
% Copyright 2020 The MathWorks, Inc.