| Database Toolbox | ![]() |
Syntax
conn = database('datasourcename', 'username', 'password')
conn = database('databasename', 'username', 'password',
'driver','databaseurl')
Description
conn = database('datasourcename', 'username', 'password')
connects a MATLAB session to a database via an ODBC driver, returning the connection object to conn. The data source to which you are connecting is datasourcename. You must have previously set up the data source - for instructions, see Setting Up a Data Source. username and password are the username and/or password required to connect to the database. If you do not need a username or a password to connect to the database, use empty strings as the arguments.
conn = database('databasename', 'username', 'password', 'driver',
'databaseurl' )
connects a MATLAB session to a database, databasename, via the specified JDBC driver, returning the connection object to conn. The username and/or password required to connect to the database are username and password. If you do not need a username or a password to connect to the database, use empty strings as the arguments. databaseurl is the JDBC URL object, jdbc:subprotocol:subname. The subprotocol is a database type, such as oracle. The subname may contain other information used by driver, such as the location of the database and/or a port number. The subname may take the form //hostname:port/databasename. Find the correct driver name and databaseurl format in the driver manufacturer's documentation.
If database establishes a connection, MATLAB returns information about the connection object.
Instance: 'SampleDB'
UserName: ''
Driver: []
URL: []
Constructor: [1x1
com.mathworks.toolbox.database.databaseConnect]
Message: []
Handle: [1x1 sun.jdbc.odbc.JdbcOdbcConnection]
TimeOut: 0
AutoCommit: 'off'
Type: 'Database Object'
Use logintimeout before you use database to specify the maximum amount of time for which database tries to establish a connection.
You can have multiple database connections open at one time.
After connecting to a database, use the ping function to view status information about the connection, and use dmd, get, and supports to view properties of conn.
The database connection stays open until you close it using the close function. Always close a connection after you finish using it.
Example 1 - Establish ODBC Connection
To connect to an ODBC data source called Pricing, where the database has a user mike and a password bravo, type
conn = database('Pricing', 'mike', 'bravo');
Example 2 - Establish ODBC Connection Without Username and Password
To connect to an ODBC data source SampleDB, where a username and password are not needed, use empty strings in place of those arguments. Type
conn = database('SampleDB','','');
Example 3 - Establish JDBC Connection
In this JDBC connection example, the database is oracle, the username is scott, and the password is tiger. The JDBC driver name is oracle.jdbc.driver.OracleDriver and the URL to the database is jdbc:oracle:oci7:.
conn = database('oracle','scott','tiger',...'oracle.jdbc.driver.OracleDriver','jdbc:oracle:oci7:');
See Also
close, dmd, get, isconnection, isreadonly, logintimeout, ping, supports
| crossreference | dmd | ![]() |