setoptions
Set JDBC or ODBC connection options
Description
sets JDBC or ODBC connection options, where opts = setoptions(opts,Option1,OptionValue1,...,OptionN,OptionValueN)opts is one of the following:
SQLConnectionOptionsobject for JDBC connections.SQLConnectionOptionsobject for ODBC connections.
Examples
Create, configure, test, and save a JDBC data source for a Microsoft® SQL Server® database.
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:
DataSourceName— Name of the data sourceVendor— Database vendor nameJDBCDriverLocation— Full path of the JDBC driver fileDatabaseName— Name of the databaseServer— Name of the database serverPortNumber— Port numberAuthenticationType— Authentication type
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 server dbtb04, port number 54317, and Windows® authentication.
opts = setoptions(opts, ... 'DataSourceName',"SQLServerDataSource", ... 'JDBCDriverLocation',"C:\Drivers\mssql-jdbc-7.0.0.jre8.jar", ... 'DatabaseName',"toystore_doc",'Server',"dbtb04", ... 'PortNumber',54317,'AuthenticationType',"Windows")
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"
The setoptions function sets the DataSourceName, JDBCDriverLocation, DatabaseName, Server, PortNumber, and AuthenticationType properties in the SQLConnectionOptions 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.
saveAsDataSource(opts)
You can connect to the new data source using the database function or the Database Explorer app.
Edit an existing JDBC data source for a Microsoft® SQL Server® database. Set an additional JDBC driver-specific option, and save the data source.
Retrieve the existing SQL Server data source SQLServerDataSource.
datasource = "SQLServerDataSource";
opts = databaseConnectionOptions(datasource)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"
opts is an SQLConnectionOptions object with these properties:
DataSourceName— Name of the data sourceVendor— Database vendor nameJDBCDriverLocation— Full path of the JDBC driver fileDatabaseName— Name of the databaseServer— Name of the database serverPortNumber— Port numberAuthenticationType— Authentication type
Add a JDBC driver-specific connection option by using a name-value pair argument. The option specifies a timeout value for establishing the database connection. opts contains a new section of properties for the additional JDBC connection option.
opts = setoptions(opts,'loginTimeout',"20")
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:
loginTimeout: "20"
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 updated data source.
saveAsDataSource(opts)
Create a data source that connects to a MySQL® server on the Windows® or macOS platform.
On the Windows platform, use the databaseConnectionOptions function to
create a data source that connects to a MySQL server.
opts = databaseConnectionOptions('odbc','mysql')
opts =
SQLConnectionOptions with properties:
DataSourceName: ""
Vendor: "MySQL"
DatabaseName: ""
Server: "localhost"
PortNumber: 3306
ODBCDriver: "MariaDB ODBC 3.1 Driver"Configure the data source by setting the ODBC connection options.
opts = opts.setoptions('DataSourceName','mysql_odbc','DatabaseName',"toy_store","Server","dbtb09"); opts.saveAsDataSource();
Alternatively, use the databaseConnectionOptions function on the
macOS platform to create a data source that connects to a MySQL server.
opts = databaseConnectionOptions("odbc","MySQL")
opts =
SQLConnectionOptions with properties:
DataSourceName: "mysql-server-test"
Vendor: "MySQL"
DatabaseName: "toy_store"
Server: "dbtb09"
PortNumber: 3306
ODBCDriver: "/Applications/MATLAB_R2024a.app/bin/maci64/libmaodbc.dylib"
DriverManager: "unixODBC" Configure the data source by setting the ODBC connection options.
opts = setoptions(opts,"DataSourceName","mysql-server-test", ... "DatabaseName","toy_store","Server","dbtb01")
Input Arguments
Database connection options, specified as one of the following:
SQLConnectionOptionsobject for JDBC connections.SQLConnectionOptionsobject for ODBC connections.
JDBC or ODBC connection options to set, specified as one or more name-value
arguments. Option is a character vector or string scalar that
specifies the name of a connection option. OptionValue is the value
of the connection option, specified as a character vector, string scalar, logical
scalar, or numeric scalar. You can specify any connection option that is a property of
the following objects:
SQLConnectionOptionsfor JDBC connections.SQLConnectionOptionsfor ODBC connections.
Example: "DataSourceName","myDataSource","Server","localhost","PortNumber",3306
configures a JDBC data source named myDataSource that is located on
the local server with the port number 3306.
"DataSourceName","mysql-server-test","DatabaseName","toy_store","Server","dbtb01"
configures an ODBC data source named mysql-server-test that is
located on a server named dbtb01.
Output Arguments
Database connection options, returned as one of the following:
SQLConnectionOptionsobject for JDBC connections.SQLConnectionOptionsobject for ODBC connections.
Version History
Introduced in R2020bSet connection options for ODBC database connections.
See Also
Apps
Objects
Functions
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)