Database Toolbox Previous page   Next Page
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. After connecting, use exec to retrieve data.

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. Some sample databaseurl strings are listed in Example 3--Establish JDBC Connection.

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 Examples 
Informix
JDBC driver: com.informix.jdbc.IfxDriver
Database URL: jdbc:informix-sqli://161.144.202.206:3000:
INFORMIXSERVER=stars

MySQL
JDBC driver: twz1.jdbc.mysql.jdbcMysqlDriver
Database URL: jdbc:z1MySQL://natasha:3306/metrics

JDBC driver: com.mysql.jdbc.Driver
Database URL: jdbc:mysql://devmetrics.mrkps.com/testing
Oracle oci7 drivers
JDBC driver: oracle.jdbc.driver.OracleDriver
Database URL: jdbc:oracle:oci7:@rex
Oracle oci8 drivers
JDBC driver: oracle.jdbc.driver.OracleDriver
Database URL: jdbc:oracle:oci8:@111.222.333.44:1521:
Database URL: jdbc:oracle:oci8:@frug
Oracle thin drivers
JDBC driver: oracle.jdbc.driver.OracleDriver
Database URL: jdbc:oracle:thin:@144.212.123.24:1822:
Oracle 10 connections with JDBC (thin drivers)

JDBC driver: oracle.jdbc.driver.OracleDriver
Database URL: jdbc:oracle:thin: (do not specify the target name and port)

Postgre
JDBC driver: org.postgresql.Driver
Database URL: jdbc:postgresql://masd/MOSE
Microsoft SQL Server
JDBC driver: com.microsoft.jdbc.sqlserver.SQLServerDriver
Database URL: jdbc:microsoft:sqlserver://127.0.0.1:1403

JDBC driver: com.inet.tds.TdsDriver
Database URL: jdbc:inetdae:sqlgckprod:1433?database=gck
Sybase SQL Server and SQL Anywhere
JDBC driver: com.sybase.jdbc.SybDriver
Database URL: jdbc:sybase:Tds:yourhostname:yourportnumber/

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.

For Microsoft SQL Server 2000, you may also need to pass the database name, username, and password via the URL. For example,

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


Previous page  crossreference dmd Next page

© 1994-2005 The MathWorks, Inc.