Create JDBC Data Source and Set Options Programmatically - MATLAB & Simulink (original) (raw)

This example shows how to create a JDBC data source at the command line, set the JDBC connection options, and save the data source. The example configures a data source for a Microsoft® SQL Server® database.

Create JDBC Data Source

Create an SQL Server data source for a JDBC database connection.

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

opts = SQLConnectionOptions with properties:

          DataSourceName: ""
                  Vendor: "Microsoft SQL Server"

      JDBCDriverLocation: ""
            DatabaseName: ""
                  Server: "localhost"
              PortNumber: 1433
      AuthenticationType: "Server"

opts is an SQLConnectionOptions object with these properties:

Set JDBC Connection Options

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 serverdbtb04, port number 54317, and Windows® authentication. Also, set driver-specific connection options to specify a timeout value for establishing the database connection, and to disable SSL encryption.

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

opts = SQLConnectionOptions with properties:

          DataSourceName: "SQLServerDataSource"
                  Vendor: "Microsoft SQL Server"

      JDBCDriverLocation: "C:\Drivers\mssql-jdbc-7.0.0.jre8.jar"
            DatabaseName: "toystore_doc"
                  Server: "dbtb04"
              PortNumber: 54317
      AuthenticationType: "Windows"

Additional Connection Options:

                 encrypt: "false"
            loginTimeout: "20"

The setoptions function sets theDataSourceName, JDBCDriverLocation,DatabaseName, Server,PortNumber, and AuthenticationType properties in the SQLConnectionOptions object. The driver-specific connection options appear below the other connection options.

Test and Save JDBC Data Source

Test the database connection with a blank user name and password. ThetestConnection function returns the logical1, which indicates the database connection is successful.

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

Save the configured data source.

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

See Also

Objects

Functions