testConnection
Test JDBC or ODBC database connection
Syntax
Description
tests the JDBC or ODBC database connection, a user name, and a password, where
status = testConnection(opts,username,password)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.
Create and configure a JDBC data source to a Microsoft® SQL Server® database. Test the database connection to the JDBC data source and retrieve the error message.
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 SQL Server authentication.
opts = setoptions(opts, ... 'DataSourceName',"SQLServerDataSource", ... 'JDBCDriverLocation',"C:\Drivers\mssql-jdbc-7.0.0.jre8.jar", ... 'DatabaseName',"toystore_doc",'Server',"dbtb04", ... 'PortNumber',54317,'AuthenticationType',"Server")
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: "Server"
The setoptions function sets the DataSourceName, JDBCDriverLocation, DatabaseName, Server, PortNumber, and AuthenticationType properties in the SQLConnectionOptions object.
Test the database connection using an incorrect user name and password. The testConnection function returns the logical 0, which indicates the database connection fails. Retrieve and display the error message for the failed connection.
username = "wronguser"; password = "wrongpassword"; [status,message] = testConnection(opts,username,password)
status = logical
0
message = 'JDBC Driver Error: Login failed for user 'wronguser'. ClientConnectionId:dfe57aed-973f-4c5f-838b-d733a24c40f0'
Input Arguments
Database connection options, specified as one of the following:
SQLConnectionOptionsobject for JDBC connections.SQLConnectionOptionsobject for ODBC connections.
Username required to access the database, specified as a character
vector or string scalar. If no username is required, specify an
empty value "". Provide this username if you
do not store it as part of the data source when using the
Database Explorer app to make a connection.
Password required to access the database, specified as a character vector or string scalar. If
no password is required, specify an empty value
"". Provide this password if you
do not store it as part of the data source when using the
Database Explorer app to make a connection.
Data Types: char | string
Output Arguments
Connection status, returned as a logical true if the connection
test passes or false if the connection test fails.
Error message, returned as a character vector. If the connection test passes, then the error message is an empty character vector. Otherwise, the error message contains text from the failed database connection.
Version History
Introduced in R2020bTest connections for ODBC database connections.
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.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)