%% 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.

opts = configureJDBCDataSource('Vendor','Microsoft SQL Server')

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

opts = setConnectionOptions(opts, ...
    'DataSourceName','SQLServerDataSource', ...
    'Server','dbtb04','PortNumber',54317, ...
    'JDBCDriverLocation','C:\Drivers\sqljdbc4.jar')

% The setConnectionOptions function sets the DataSourceName, Server, PortNumber, and JDBCDriverLocation properties in the JDBCConnectionOptions 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 2019 The MathWorks, Inc.