SQLConnectionOptions
Description
Create connection options for a JDBC database connection.
After you create an SQLConnectionOptions
object, set the connection
options, test the connection, and save the data source, you can create a JDBC database
connection using the saved data source. The connection options include the options required to
make a database connection. You can also define additional connection options for a specific
database driver.
Creation
Create an SQLConnectionOptions
object using the databaseConnectionOptions
function.
Properties
All Databases
DataSourceName
— Data source name
string scalar
Data source name, specified as a string scalar. You can use the data source name in
the database
function to create a JDBC
database connection.
Example:
"MSSQLServer"
Data Types: string
Vendor
— Database vendor
string scalar
This property is read-only.
Database vendor, specified as a string scalar. Set this property using the
vendor
input argument in the databaseConnectionOptions
function.
Example:
"Microsoft SQL Server"
Data Types: string
JDBCDriverLocation
— JDBC driver location
string scalar
JDBC driver location, specified as a string scalar. Specify the full path to the JDBC driver file, including the name of the file.
Example:
"C:\drivers\sqljdbc4.jar"
Data Types: string
Common Properties for Microsoft SQL Server, MySQL, Oracle, and PostgreSQL Databases
DatabaseName
— Database name
string scalar
Database name on the server, specified as a string scalar.
Example:
"mydatabase"
Data Types: string
Server
— Database server name or address
"localhost"
(default) | string scalar
Database server name or address, specified as a string scalar.
Data Types: string
PortNumber
— Server port number where the server is listening
numeric scalar
Server port number where the server is listening, specified as a numeric scalar. The default value is based on the database vendor:
Microsoft® SQL Server® — 1433
MySQL® — 3306
Oracle® — 1521
PostgreSQL — 5432
Data Types: double
Microsoft SQL Server Database Only
AuthenticationType
— Authentication type
"Server"
(default) | "Windows"
Authentication type, specified as one of these values:
"Server"
— Microsoft SQL Server authentication"Windows"
— Windows® authentication
Specify the value as a string scalar.
Oracle Database Only
DriverType
— Driver type
"thin"
(default) | "oci"
Driver type, specified as one of these values:
"thin"
— Thin driver"oci"
— Windows authentication or OCI driver
Specify the value as a string scalar.
Other Databases
Driver
— JDBC driver name
string scalar
JDBC driver name, specified as a string scalar that refers to the Java® driver that implements the java.sql.Driver
interface.
For details about the JDBC driver name, consult your database driver documentation.
Example:
org.sqlite.JDBC
Data Types: string
URL
— Database connection URL
string scalar
Database connection URL, specified as a string scalar for the vendor-specific URL. This URL is typically constructed using connection properties such as the server name, port number, and database name.
For details about the database connection URL, consult your database driver documentation.
Example:
jdbc:sqlite:C:\Databases\sqlite.db
Data Types: string
Object Functions
setoptions | Set JDBC or ODBC connection options |
rmoptions | Remove JDBC or ODBC connection options |
reset | Reset JDBC or ODBC connection options to defaults |
testConnection | Test JDBC or ODBC database connection |
saveAsDataSource | Save JDBC or ODBC data source |
Examples
Create JDBC Data Source
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.
Version History
Introduced in R2020b
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.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)