setConnectionOptions

Set JDBC connection options

Description

example

opts = setConnectionOptions(opts,Option1,OptionValue1,...,OptionN,OptionValueN) sets JDBC connection options using the JDBCConnectionOptions object opts.

Examples

collapse all

Create, configure, test, and save a JDBC data source for a Microsoft® SQL Server® database.

Create an SQL Server data source.

opts = configureJDBCDataSource('Vendor','Microsoft SQL Server')
opts = 
  JDBCConnectionOptions with properties:

                      Vendor: 'Microsoft SQL Server'
              DataSourceName: ''

                DatabaseName: ''
                      Server: 'localhost'
                  PortNumber: 1433
                    AuthType: 'Server'

          JDBCDriverLocation: ''

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, full path to the JDBC driver file, and Windows® authentication.

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

                      Vendor: 'Microsoft SQL Server'
              DataSourceName: 'SQLServerDataSource'

                DatabaseName: ''
                      Server: 'dbtb04'
                  PortNumber: 54317
                    AuthType: 'Windows'

          JDBCDriverLocation: 'C:\Drivers\sqljdbc4.jar'

The setConnectionOptions function sets the DataSourceName, Server, PortNumber, AuthType, and JDBCDriverLocation properties in the JDBCConnectionOptions object.

Test the database connection with a blank user name and password. The testConnection function returns the logical 1, which indicates the database connection is successful.

username = "";
password = "";
status = testConnection(opts,username,password)
status = logical
   1

Save the configured data source.

saveAsJDBCDataSource(opts)

You can connect to the new data source using the database function or the Database Explorer app.

Using an existing JDBC data source configured for a Microsoft® SQL Server® database, set the database name.

Edit the existing JDBC data source SQLServerDataSource.

opts = configureJDBCDataSource('Datasource','SQLServerDataSource')
opts = 
  JDBCConnectionOptions with properties:

                      Vendor: 'Microsoft SQL Server'
              DataSourceName: 'SQLServerDataSource'

                DatabaseName: ''
                      Server: 'dbtb04'
                  PortNumber: 54317
                    AuthType: 'Windows'

          JDBCDriverLocation: 'C:\Drivers\sqljdbc4.jar'

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

Set the database name to toystore_doc. The DatabaseName property contains the new database name.

opts = setConnectionOptions(opts,'DatabaseName','toystore_doc')
opts = 
  JDBCConnectionOptions with properties:

                      Vendor: 'Microsoft SQL Server'
              DataSourceName: 'SQLServerDataSource'

                DatabaseName: 'toystore_doc'
                      Server: 'dbtb04'
                  PortNumber: 54317
                    AuthType: 'Windows'

          JDBCDriverLocation: 'C:\Drivers\sqljdbc4.jar'

Test the database connection with a blank user name and password. The testConnection function returns the logical 1, which indicates the database connection is successful.

username = "";
password = "";
status = testConnection(opts,username,password)
status = logical
   1

Save the configured data source.

saveAsJDBCDataSource(opts)

Input Arguments

collapse all

JDBC connection options, specified as a JDBCConnectionOptions object.

JDBC connection options to set, specified as one or more name-value pair arguments. Option is a character vector or string scalar that specifies the name of a JDBC connection option. OptionValue specifies the value of the JDBC connection option. OptionValue can be a character vector, string scalar, logical scalar, or numeric scalar. You can specify any JDBC connection option that is a property of the JDBCConnectionOptions object.

Example: 'DataSourceName','myDataSource','Vendor','MySQL','Server','localhost','PortNumber',3306 configures a JDBC data source named myDataSource for a MySQL® database located on the local server with the port number 3306.

Output Arguments

collapse all

JDBC connection options, returned as a JDBCConnectionOptions object.

Alternative Functionality

App

You can set JDBC connection options by using the JDBC Data Source Configuration dialog box in the Database Explorer app. In the Data Source section of the Database Explorer tab, select Configure Data Source > Configure JDBC data source.

Introduced in R2019b