Main Content

Perform Database Queries in MATLAB Online Hosted by Your Organization

Intended Audience

MATLAB® end users interacting with databases from within MATLAB Online Server™ using Database Toolbox™. For IT administrators, to enable Database Toolbox for end users, see Integrate MATLAB Online Server with Database Toolbox.

If your organization hosts MATLAB Online™ using MATLAB Online Server, then you can use database drivers to query your desired database from MATLAB Online.

Prerequisites

To connect to a database from MATLAB Online, your server administrator must specify additional configurations to enable communication between MATLAB Online and the database. To see if the database you want to connect to is already enabled for your installation, contact your administrator.

Connect to Database

To connect to your database, upload the driver for your database:

  1. Get your database driver. Common JDBC Drivers are available at Driver Installation.

  2. In MATLAB Online, create a folder for the drivers. For example: MyDatabaseDrivers.

  3. Upload the JAR or ZIP file containing your database driver to that folder.

  4. Verify that your folder and file are on the MATLAB Online path. Right-click the folder, select Add to Path, and then choose Selected Folders and Subfolders. For more details about the MATLAB search path, see What Is the MATLAB Search Path?

Query Database

For details on querying your database, see the Database Toolbox documentation. The following example MATLAB code for MySQL can help you get started.

% Configure the database vendor type.
opts = configureJDBCDataSource("Vendor", "MySQL");

% Set database connection options.
% Replace < > values with your desired settings.
% 
% * <MySQL-Data-Base> is a user-friendly name for your database setup. 
% * <database.mycompany.com> is the server name of the database.
% * <portnumber> is the port number of the database.
% * </path/to/driver.jar> is the location you uploaded your driver to.
%   For example: /home/username/mysql-connector-java-8.0.18.jar 
%
opts = setConnectionOptions(opts, ...
    "DataSourceName", "<MySQL-Database-Name>", ...
    "Server", "<database.mycompany.com>", ...
    "PortNumber", <portnumber>, ...
    "JDBCDriverLocation", "</path/to/driver.jar>");

% Configure with your database username and password.
username = '<database_username>'; 
password = '<database_password>'; 

% Validate the connectivity from your MATLAB session to the 
% database. If status = 1, the connection was successful.
[status, message] = testConnection(opts, username, password);

% Save your JDBC configuration as your <MySQL-Database-Name> value.
saveAsJDBCDataSource(opts);

% Establish a connection to your database.
conn = database('<MySQL-Database-Name>', username, password);

% Construct SQL statement for your <database.table> schema.
statement = 'select * from <database.table> limit 10';
data = select(conn, statement);

When querying the database, if you encounter any of the errors in the following table, apply the suggested solution.

ErrorPossible Solution
requires Database ToolboxWhile running any commands, if you receive this error message, contact your administrator to request that they enable Database Toolbox for your environment.
JDBC data source does not contain driver locationValidate that your driver is available on your MATLAB Online search path.
JDBC Driver Error: Communications link failureCheck with your administrator that the database server and port have been configured for MATLAB Online Server.

If you encounter any other difficulties querying the database, see Troubleshooting in Database Toolbox (Database Toolbox).

Related Topics