Database Toolbox    
database

Connect to database

Syntax

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.

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.

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 username and password are not needed, use empty strings in place of those arguments. Type

Example 3--Establish JDBC Connection

In this JDBC connection example, the database is oracle, the username is scott, and the password is tiger. The oci7 JDBC driver name is oracle.jdbc.driver.OracleDriver and the URL that specifies the location of the database server is jdbc:oracle:oci7.

The JDBC name and URL take different forms for different databases, as shown in the examples in the following table.

Database
JDBC Driver and Database URL
Oracle oci7 drivers
JDBC driver: oracle.jdbc.driver.OracleDriver

Database URL: jdbc:oracle:oci7:
Oracle oci8 drivers
JDBC driver: oracle.jdbc.driver.OracleDriver

Database URL: jdbc:oracle:oci8:@111.222.333.44:1521:
Oracle thin drivers
JDBC driver: oracle.jdbc.driver.OracleDriver

Database URL: jdbc:oracle:thin:@144.212.123.24:1822:
MySQL
JDBC driver: twz1.jdbc.mysql.jdbcMysqlDriver

Database URL: jdbc:z1MySQL://natasha:3306/metrics
Sybase jConnect
JDBC driver: com.sybase.jdbc.SybDriver

Database URL: jdbc:sybase:Tds:yourmachinename:portnumber/

For the Oracle thin drivers example, in the database URL jdbc:oracle:thin:@144.212.123.24:1822, the target machine that the database server resides on is 144.212.123.24, and the port number is 1822.

See Also
close, dmd, get, isconnection, isreadonly, logintimeout, ping, supports


  crossreference dmd