| Database Toolbox User's Guide | Search  Help Desk |
| database | Examples See Also |
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 structure to conn. datasourcename is the data source to which you are connecting. You must have previously set up the data source - for instructions, see "Setting Up a Data Source". username and password are the user name and/or password required to connect to the database. If you do not need a user name 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 structure to conn. username and password are the user name and/or password required to connect to the database. If you do not need a user name or a password to connect to the database, use empty strings as the arguments. databaseurl is the JDBC URL structure, 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.
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 command to view information about the connection, and use get to view properties of conn.
The database connection stays open until you close it using the close command. Always close a connection after you finish using it.
Examples
Example 1 - Establish ODBC connection
To connect to an ODBC data source calledPricing, where the database has a user mike and a password bravo, type:
connA = database('Pricing', 'mike', 'bravo')
Example 2 - Establish ODBC connection without username and password
To connect to an ODBC data sourceSampleDB, where a user name and password are not needed, use empty strings in place of those arguments. Type:
connB = database('SampleDB','','')
Example 3 - Establish JDBC connection
In the JDBC connection example below, the database isoracle, the user name 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:.
Use theconn1 = database('oracle','scott','tiger',...'oracle.jdbc.driver.OracleDriver','jdbc:oracle:oci7:')
ping command to see information about the connection - type ping(conn1) and MATLAB returns:
Database Product Name : Oracle
Database Product Version : Oracle7 Server Release 7.3.3.0.0 -
Production Release With the
distributed, replication and parallel
query options PL/SQL Release
2.3.3.0.0 - Production
JDBC Driver Name : Oracle JDBC driver
JDBC Driver Version : 7.3.3.1.3
Max. Database Connections : 0
Current User Name : scott
Database URL : jdbc:oracle:oci7:oracle
Auto Commit Transactions : True
See Also
close, get, logintimeout, ping