%% Connect to Microsoft SQL Server Using JDBC Driver
% Connect to a Microsoft® SQL Server® database. Then, import data from the database into MATLAB®. Perform simple data analysis, and then close the database connection.
% To create a database connection using a JDBC driver, you must configure a JDBC data source. For more information, see the configureJDBCDataSource function.
% This example assumes that you are connecting to a Microsoft SQL Server Version 11.00.2100 database using the Microsoft SQL Server Driver 11.00.5058.
%
% Create a database connection to the JDBC data source MSSQLServerJDBCAuth. This data source configures a JDBC driver to a Microsoft SQL Server database with Windows® authentication. Specify a blank user name and password.

datasource = "MSSQLServerJDBCAuth";
username = "";
password = "";
conn = database(datasource,username,password)

% conn has an empty Message property, which indicates a successful connection.
% The property sections of the conn object are:
% Database Properties -- Information about the database configuration
% Catalog and Schema Information -- Names of catalogs and schemas in the database
% Database and Driver Information -- Names and versions of the database and driver
%
% Import all data from the table inventoryTable into MATLAB® using the sqlread function. Display the first eight rows of data.

tablename = "inventoryTable";
data = sqlread(conn,tablename);
head(data)

%
% Determine the highest product quantity in the table.

max(data.Quantity)

%
% Close the database connection.

close(conn)

% Copyright 2018 The MathWorks, Inc.