Database Toolbox User's Guide
  Go to function:
    Search    Help Desk 
database    Examples   See Also

Connect to database

Syntax

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 called Pricing, where the database has a user mike and a password bravo, type:

Example 2 - Establish ODBC connection without username and password

To connect to an ODBC data source SampleDB, where a user name and password are not needed, use empty strings in place of those arguments. Type:

Example 3 - Establish JDBC connection

In the JDBC connection example below, the database is oracle, 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 the ping command to see information about the connection - type ping(conn1) and MATLAB returns:

See Also

close, get, logintimeout, ping

[ Previous | Help Desk | Next ]